Repository navigation
saves: report excessive JSAV JSON nesting as an input error - #3565
Conversation
|
At 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
left a comment
There was a problem hiding this comment.
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.
|
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. |
|
Rechecked at |
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.
A properly framed JSAV containing excessively nested JSON currently lets
RecursionErrorescape fromJSONDecoder.raw_decode(). Theinfo,verify,diff, andextractcommands 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
RecursionErrorto 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:
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.