Repository navigation
Conversation
parse() passed the caller's start straight to Parser(s, pos=start), and
the generated parser only compares pos against self.end, so a position
below zero or past the length got through. A negative offset became
ordinary Python negative indexing, so parse('012', start=-2) read off
the end of the string, wrapped round to the front and returned 12012;
a start past the end let _err_str index msg[errpos] out of range and
raised IndexError out of parse(), loads() and load().
Check start against the input length next to the existing empty-string
guard and return the usual error tuple, so loads() and load() report it
as the ValueError they document.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
parse()hands the caller'sstartstraight toParser(s, pos=start), and the generated parser only ever comparesposagainstself.end, so a position below zero or past the length gets through. A negative offset then becomes ordinary Python negative indexing:json5.parse('012', start=-2)reads'1'and'2'off the end of the string, wraps round to the front and returns12012, a value the document never contained, andjson5.parse('{a: 1}', start=-1)reports a position of-1that the loop in theparse()docstring would feed back in as the nextstart. Going past the end is inconsistent rather than wrong:start == len(s)gives a normalUnexpected end of input, while one more than that lets_err_strindexmsg[errpos]out of range, so anIndexErrorescapesparse(), which documents that errors come back in the returned tuple, and escapesloads()/load(), which documentValueErroras what they raise. I ran into it while checking what the documented multi-value loop does with the positions it hands back. The check sits next to the existing empty-string guard because that is the single place astartreaches the parser, soloads()andload()surface it as theValueErrorthey already promise.