Skip to content

Fix #3008: reject trailing data after top-level null#3028

Closed
ErnestHysa wants to merge 1 commit into
google:mainfrom
ErnestHysa:fix/3008-trailing-data-after-top-level-null
Closed

Fix #3008: reject trailing data after top-level null#3028
ErnestHysa wants to merge 1 commit into
google:mainfrom
ErnestHysa:fix/3008-trailing-data-after-top-level-null

Conversation

@ErnestHysa

Copy link
Copy Markdown

Fixes #3008.

Both Gson.fromJson and JsonParser.parseReader accepted trailing data after a top-level null because the trailing-data check was skipped when the parsed element was null.

Changes

  • Gson.java assertFullConsumption(): Removed the obj != null guard so trailing-data validation runs even when the parsed object is null.
  • JsonParser.java parseReader(): Removed the !element.isJsonNull() guard so trailing-data validation runs unconditionally.

Now null extra correctly throws JsonSyntaxException: Did not consume the entire document (or the equivalent from Gson.fromJson).

Both Gson.fromJson and JsonParser.parseReader accepted trailing data
after a top-level null because the trailing-data check was skipped when
the parsed element was null.

Changed assertFullConsumption() in Gson.java to always check for
END_DOCUMENT (removing the 'obj != null' guard), and changed the check
in JsonParser.parseReader() to unconditionally verify END_DOCUMENT
(removing the '!element.isJsonNull()' guard).
@google-cla

google-cla Bot commented May 31, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Marcono1234

Copy link
Copy Markdown
Contributor

Can you please explain what the purpose of this PR here is, or the motivation?

#3008 is a pull request not an issue, and it already tries to fix the issue while also handling corner cases and extending test coverage. It just hasn't been merged yet.

@ErnestHysa

Copy link
Copy Markdown
Author

@Marcono1234 Fair point — I missed #3008 before opening this. Looking at it now, the patch is essentially the same change to the two call sites, plus it handles the EOFException corner case (empty/whitespace-only input) and adds test coverage. My #3028 is a strict subset of #3008 with no added value, so I'm closing this PR in favor of #3008. Sorry for the noise.

@ErnestHysa ErnestHysa closed this Jun 5, 2026
@ErnestHysa ErnestHysa deleted the fix/3008-trailing-data-after-top-level-null branch June 5, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants