Skip to content

saves: report excessive JSAV JSON nesting as an input error - #3565

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Kosmabb8:codex/jsav-json-nesting
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Kosmabb8:codex/jsav-json-nesting

Conversation

@Kosmabb8

@Kosmabb8 Kosmabb8 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

A properly framed JSAV containing excessively nested JSON currently lets RecursionError escape from JSONDecoder.raw_decode(). The info, verify, diff, and extract commands then print tracebacks and exit 1. Normalize that decoder failure to the existing JSAV validation error, so each command reports one error line and exits 2 before changing exports.

Claim: #3381 (comment) (new JSAV body nesting error handling item; follow-up to merged #2603).

This is separate from #2779's editable pack index validation. The change adds only RecursionError to the existing JSON-body decoder exception tuple; entry bounds and UTF-8 byte-offset/document-end calculations stay intact.

Validation on Windows 11, portable CPython 3.12.10, CPU only:

  • Negative control against main f847748: the parser regression errors and all four real CLI command subcases fail (1 error, 4 failures).
  • python -W error::ResourceWarning -I tests/test_saves.py -q: 50 tests, 49 passed and 1 existing POSIX permission skip.
  • python -I tests/test_saves_import_args.py: 1 passed.
  • python -I tests/test_save_keys.py -q: 33 passed.
  • python -I tests/test_text_write_line_endings.py: 2 passed.
  • python -m compileall -q make_game.py tools tests: passed.
  • git diff --check: passed.

The depth-100000 fixture is hand-built JSON inside a container generated by the real build_jsav(). Parser and subprocess CLI checks verify the validation contract; CLI checks also verify the source, prior index and prior block bytes remain intact with no extra exports.

Synthetic input only; no game files, GPU/driver validation or C++ runtime changes. AI-assisted with Codex.

Portability correction: Linux CPython 3.12 accepts 6,000 nested arrays, as the reviewer reproduced. Both regression fixtures now use 100,000 levels, which exceeded the decoder limit in the reviewer's Linux probe and also passes the Windows error-path checks. Windows CPython 3.12.10: JSAV branch 50 tests (1 POSIX skip), pack-index branch 40 tests, and the combined save branches 60 tests (3 platform skips), all otherwise passing. A native Linux recheck of the updated fixtures is pending. AI-assisted.

@ray3242

ray3242 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

At 5c4bce0a, the new excessive-nesting tests in tests/test_saves.py:554-576 assume that 6,000 nested arrays exceed the JSON decoder's recursion limit. On Linux with Python 3.12, which the CI standalone loop uses, json.loads accepts that depth, so the tests fail:

$ python3.12 -I -m pytest -q tests/test_saves.py
AssertionError: ValueError not raised
AssertionError: 0 != 2
5 failed, 49 passed, 19 subtests passed          (head and merge with main; main: 48 passed)
depth   6000: accepted on main, head and merge
depth 100000: main RecursionError, head/merge ValueError

The new handling itself works once decoding really overflows. Only the fixture depth is too shallow, and the limit depends on the platform and stack size. Building the fixture deep enough to overflow everywhere (e.g. 100,000+), or deriving the depth from a probe, would keep the test portable. I didn't run it on Windows.

AI-assisted (Devin); Linux, Python 3.12.16, no GPU or game files.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows at 5c4bce0, info, verify, diff (both argument orders) and extract returned 2 with a single diagnostic for excessive nesting. Source and pre-existing exports were preserved. An initially absent extraction destination was created empty. All four commands succeeded on a valid synthetic save, which packed byte-identically. Two save suites: 82 passed, one POSIX-only skip. AI-assisted CPU validation.

@Kosmabb8

Copy link
Copy Markdown
Contributor Author

Thanks for the Linux reproduction. Fixed the test fixture in bcc39e5: both parser and real-CLI tests now use 100,000 nested arrays, matching the depth your Linux probe confirmed overflows. The production exception handling is unchanged. I also corrected the matching editable-index fixtures on #2779 (acffe1b), which used the same too-shallow depth. Windows CPython 3.12.10: this branch's 50 tests pass with 1 existing POSIX skip; all combined save changes run 60 tests with 3 platform skips and no failures. The two updated branches merge cleanly with the other open account PRs. Native Linux recheck of the new fixture is welcome; no Linux result is claimed locally. AI-assisted.

@ray3242

ray3242 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Rechecked at bcc39e59 on Linux, Python 3.12.16: python -I tests/test_saves.py 50/50 OK, so the 100,000-deep fixtures now overflow the decoder where 6,000 did not. My point is resolved. CPU only. AI-assisted (Devin).

@ItsNotRexed
ItsNotRexed merged commit 5105216 into ItsNotRexed:main Nov 23, 2026
2 of 4 checks passed
sovljanskii added a commit to sovljanskii/Leonida that referenced this pull request Nov 23, 2026
tests/test_saves.py failed 7 subtests on main (3faac55) under Python 3.14
and 3.15, and passed on 3.12. The nesting guards from ItsNotRexed#3565 relied on json
raising RecursionError, and how deep the decoder goes depends on the version.
Measured as the deepest array nesting that decodes: 9,996 levels on 3.12.16,
116,219 on 3.14.10 and 130,746 on 3.15.2. So from 3.14 on, the 100,000-level
fixtures were accepted:
- info, verify and diff exited 0 on the deep JSAV body;
- extract died with a RecursionError traceback in json.dump(indent=1);
- pack read the deep index.json and reported "index must be an object"
  instead of naming the invalid JSON.

The indenting encoder that extract writes with stops at 994 levels on every
one of these versions. So on 3.12 too, a body between 994 and 9,996 levels
passed info and gave a traceback from extract. extract's ASAV payload path
only caught ValueError, so a deep plain-text payload ended in a traceback
on any version.

saves.py now measures the decoded value without recursion (json_depth) and
refuses anything deeper than MAX_JSON_DEPTH = 512, well below the encoder's
limit:
- a JSAV body: the same "JSAV body is not valid UTF-8 JSON" error, exit 2,
  for info, verify, diff and extract;
- pack's index.json: the same "invalid JSON" error;
- an ASAV payload: the existing "did not parse as JSON" note. payload.bin is
  kept, payload.json is not written, and 3.12's own RecursionError is caught
  there too.

json_depth takes about 130 ms on a 6.4 MB body.

Tests: a body exactly at the limit is read and extracted, and one level over
is refused by all four read commands with a prior export left alone; ASAV
payloads at the limit, one over it, and at 20,000 levels; json_depth on
scalars, mixed values and a 100,000-level value. tests/test_saves.py: 66
tests OK on Python 3.12.16, 3.14.10 and 3.15.2 (macOS, Apple M4). Without the
saves.py change, the new tests fail and the 7 old subtests fail again on
3.15.
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.

4 participants