Skip to content

reject out-of-range start offsets in parse() - #169

Open
iefa-m wants to merge 1 commit into
dpranke:mainfrom
iefa-m:parse-start-bounds
Open

iefa-m wants to merge 1 commit into
dpranke:mainfrom
iefa-m:parse-start-bounds

Conversation

@iefa-m

@iefa-m iefa-m commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

parse() hands the caller's start straight to Parser(s, pos=start), and the generated parser only ever compares pos against self.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 returns 12012, a value the document never contained, and json5.parse('{a: 1}', start=-1) reports a position of -1 that the loop in the parse() docstring would feed back in as the next start. Going past the end is inconsistent rather than wrong: start == len(s) gives a normal Unexpected end of input, while one more than that lets _err_str index msg[errpos] out of range, so an IndexError escapes parse(), which documents that errors come back in the returned tuple, and escapes loads()/load(), which document ValueError as 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 a start reaches the parser, so loads() and load() surface it as the ValueError they already promise.

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.
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.

1 participant