Fix #3008: reject trailing data after top-level null#3028
Conversation
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).
|
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. |
|
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. |
|
@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. |
Fixes #3008.
Both
Gson.fromJsonandJsonParser.parseReaderaccepted trailing data after a top-level null because the trailing-data check was skipped when the parsed element was null.Changes
assertFullConsumption(): Removed theobj != nullguard so trailing-data validation runs even when the parsed object is null.parseReader(): Removed the!element.isJsonNull()guard so trailing-data validation runs unconditionally.Now
null extracorrectly throwsJsonSyntaxException: Did not consume the entire document(or the equivalent fromGson.fromJson).