Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JsonReader advancing before throwing exception due to malformed JSON #1743

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Marcono1234
Copy link
Collaborator

Fixes #1735

Previously the JsonReader methods doPeek() (internal method called for example by peek()), nextString() and skipValue() advanced in the stream before throwing an exception due to malformed JSON.
This allowed them to "skip" malformed JSON making the JSON valid again for subsequent calls, which is likely undesired.

This pull request changes it so that if malformed JSON is encountered all subsequent method calls will throw the same exception.
Additionally it introduces more JsonScope constants because it was not possible with the existing ones to handle cases where a variable amount of characters is skipped, e.g. whitespaces between : after an object property name and before the property value.

Previously JsonReader.doPeek() would have already advanced or modified the
state of the reader when malformed JSON was encountered and an exception
was thrown. Therefore repeatedly calling methods which peek would result
in inconsistent exceptions and could even make the JSON "valid" again by
simply advancing past the malformed JSON.

Now the state of the reader is only modified when valid JSON is encountered.
Note that there are a few remaining cases where this issue still occurs,
which will be fixed in subsequent commits.

This change required adding more JsconScope constants because if there is
enough space (or a long comment) between tokens the skipped content could
not have been stored in the buffer, e.g. `{"a":   ...     :1}`.
Here a value is expected after the `:`. However there are a lot of
whitespaces followed by a second `:` (i.e. malformed JSON).
With the previous JsonScopes it would not have been possible to represent
this state correctly. Therefore a subsequent peek would have consumed the
second `:` making the JSON "valid" again.
…scape

Previously JsonReader.nextString() and skipValue() consumed the '\' of an
escape sequence before throwing an exception for invalid escape sequences.
Therefore a subsequent method call would have read a "valid" string.

Now the stream position is only advanced when the escape sequence is valid.
@google-cla google-cla bot added the cla: yes label Jul 24, 2020
yuedaxia76 pushed a commit to yuedaxia76/gson that referenced this pull request Jul 27, 2020
For a malformed unicode escape sequence the exception message thrown be the
method erroneously only included the first 4 chars which is `\uXX`, missing
the last 2 hex chars.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JsonReader.peek advances when exception is thrown
1 participant