extmod/modjson: Detect unterminated composite entities. #17148
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes the JSON parser raise an exception when handling objects or arrays whose declaration is incomplete, as in missing the closing marker (brace or bracket) and if the missing marker would have been the last non-whitespace character in the incoming string.
Since CPython's JSON parser would raise an exception in such a case, unlike MicroPython's, this PR aligns MicroPython's behaviour with CPython.
This commit fixes issue #17141.
Testing
This was tested with the Unix port, passing all JSON-related tests (existing and new).
Trade-offs and Alternatives
There may be some code out there that relies on this peculiar behaviour, and thus this PR may introduce an incompatibility across MicroPython versions.
On the other hand, the code wouldn't have worked in CPython in the first place as an incomplete JSON object is not meant to be parseable by
json.loadorjson.loadsanyway since they expect a fully formed JSON object to be available (either from a string or from a file descriptor).