Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

gh-162758: Reject a negative idx in the pure Python JSON scanner - #162759

Open
thejesh23 wants to merge 1 commit into
python:mainfrom
thejesh23:fix-json-py-scanner-negative-idx
Open

thejesh23 wants to merge 1 commit into
python:mainfrom
thejesh23:fix-json-py-scanner-negative-idx

Conversation

@thejesh23

@thejesh23 thejesh23 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

The pure Python scanner now raises ValueError for a negative idx, like the C scanner.

The new test cases fail for Python without the fix and pass for C. test_json passes.

Copy link
Copy Markdown

Additional Windows verification, prepared and executed with Codex assistance.

On Windows 11 25H2 (26200.9974), AMD64, CPython 3.13.14, the isolated pure-Python decoder reproduces gh-162758: raw_decode('"a"', -3) returns ('', 1), and raw_decode('1', -1) returns (1, 1). The C decoder raises ValueError('idx cannot be negative') for both.

Applied the same two-line idx < 0 guard from this PR to a local copy of the 3.13.14 Python scanner. Results:

  • All 46 tests from the official v3.13.14 test_json.test_decode and test_json.test_fail modules passed before and after the patch, including both Python and C cases; no skips or errors. The isolated test_json.pyjson.scanner was updated and its default decoder recreated; test assertions were unchanged.
  • 120 document/offset combinations matched the C decoder exactly after patching (returned values or exception type/message). Inputs included strings, numbers, arrays, objects, literals, malformed/empty documents, and offsets before, within and beyond each document.
  • Six explicit negative-offset cases, including empty input and -50000, all raised the same ValueError/message as C after patching.

This verifies the proposed scanner guard adapted to 3.13.14 on Windows, not a build of PR head 753dd86 on main. The complete CPython suite was not run; installed Python was unchanged. Credit to the original reporter and thejesh23 for the report and proposed fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants