Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions json5/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ def parse(
if not s:
return None, 'Empty strings are not legal JSON5', 0
start = start or 0
if not 0 <= start <= len(s):
return None, f'Invalid start position {start}', 0
parser = Parser(s, '<string>', pos=start)
ast, err, pos = parser.parse(
global_vars={'_strict': strict, '_consume_trailing': consume_trailing}
Expand Down
10 changes: 10 additions & 0 deletions tests/lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,16 @@ def test_parse_empty_input(self):
(None, 'Empty strings are not legal JSON5', 0),
)

def test_parse_invalid_start(self):
# A negative start indexes the string from the end and a start past
# the end indexes off it; neither is an offset into the document.
for source, start in (('012', -2), ('012', 4)):
with self.subTest(source=source, start=start):
self.assertEqual(
json5.parse(source, start=start),
(None, f'Invalid start position {start}', 0),
)

def test_parse(self):
self.check('4', 4, err=None, pos=1)
self.check('4 ', 4, err=None, pos=2)
Expand Down