Skip to content

saves: validate editable pack indexes before rebuilding - #2779

Merged
ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
Kosmabb8:codex/saves-pack-index
Nov 23, 2026
Merged

ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
Kosmabb8:codex/saves-pack-index

Conversation

@Kosmabb8

@Kosmabb8 Kosmabb8 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

An edited or damaged save-pack index.json could raise uncaught TypeError/KeyError and print a traceback. For example, a null header exited 1 instead of using the CLI's clear input-error path. Validate the index/header/entry shapes and the fields consumed by the packer before reading blocks or rebuilding; errors now name the index and invalid field and return exit code 2.

Validation covers uint32 version/flag/hash fields and a code-block count within the entry count. Extra metadata, existing hexadecimal spellings and a minimal empty index remain accepted. Existing output is untouched on invalid input.

Claim: #2516 (comment) (new editable-index validation item). Independent of #1438's destination checks, #2603's binary-container bounds and #2688's atomic publication/link policy; no dependency on them.

Tested on Windows 11 Pro, CPython 3.12.10, synthetic files only:

  • Initial regressions on 49475796 produced 8 failures/39 errors, including CLI tracebacks and accepted boolean fields.
  • python -W error::ResourceWarning -I tests/test_saves.py -q: 38 passed. New coverage includes 43 malformed structural/type/range variants, four CLI cases, JSON syntax/encoding diagnostics, output preservation and valid byte-exact/empty-index round trips.
  • test_saves_import_args.py: 1 passed; test_save_keys.py: 24 passed.
  • Focused compileall and diff checks passed.
  • Local integration with saves: reject JSAV blocks outside the JSON body #2603 and latest saves: preserve packed output when writing fails #2688 merged cleanly: 55 save tests ran (52 passed, 3 existing platform skips), plus import-argument and save-key checks passed.

No game files, GPU/driver validation or C++ runtime changes. AI-assisted with Codex.

Review correction: excessive JSON nesting now produces the same filename-specific validation error (CLI exit 2, no traceback) and preserves an existing output. Two depth-100000 regression tests failed before the fix. Updated Windows CPython 3.12.10 validation: 40 save, 1 import-argument and 24 save-key tests passed; compilation/diff checks passed. AI-assisted.

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.

@tupacfan04

Copy link
Copy Markdown
Contributor

Code read of c2ac2f3a (I didn't run it). Looks right. Every field build_jsav consumes is now type- and range-checked before any block is read: version, code_blocks ≤ len(entries), each entry's file, flags and hash as uint32. type(...) is not int correctly refuses true/false too. Collecting hashes during validation also means the hex is parsed once. One thing still open: file isn't confined to src (../ or an absolute path is accepted). That's harmless for a local tool, but cheap to refuse if you want edited indexes to stay self-contained.

AI-assisted review (Claude Code).

@Kosmabb8

Copy link
Copy Markdown
Contributor Author

Keeping the existing local-file reference behavior in this PR: it validates the editable index's consumed fields, without introducing a self-contained-folder policy. Both relative ../external.json and absolute references still pack a synthetic block byte-for-byte in a focused check on c2ac2f3. Rejecting those paths would be a separate compatibility change; no containment guarantee is claimed here. AI-assisted with Codex.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Linux (WSL2 Ubuntu, Python 3.12) and native Windows 11 (Python 3.13.1), CPU only: pass. Merges cleanly with main. tests/test_saves.py: Linux 38 tests OK, Windows 38 tests OK, 0 skips on both. Negative control (tools/saves.py restored to main, Linux): fails (10 failures, 39 errors), so the tests exercise the new validation. Trial merge with #2688 (also edits tools/saves.py and tests/test_saves.py) is clean and the combined test file passes on Linux (50 tests, 1 skip from the other PR).

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

Verified on the PR head: python -I tests/test_saves.py -> 38 tests OK; test_saves_import_args.py -> 1 OK; test_save_keys.py -> 24 OK. On base 4947579 the same suite fails 10 + 39 errors (the expected tracebacks and accepted bools), so the validation is exercised. Both invalid-input tests leave existing output byte-identical. Synthetic data and TemporaryDirectory only; no game data or absolute paths.

Non-blocking: an index nested ~50k arrays deep still raises RecursionError from json.loads, which prints a traceback instead of the clean exit-2 path; catching it with ValueError would make that guarantee total.

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested on Windows 11 / Python 3.13.0, merged onto current origin/main: tests/test_saves.py 38/38, test_saves_import_args.py 1/1, test_save_keys.py 30/30. Reverting only tools/saves.py gives 10 failures / 39 errors, so the regression is real (the body says 8/39, measured on its interpreter). Errors name index.json and exit 2 with no traceback; existing output is preserved; merge clean.

Approve.

AI-assisted review, no game files.

@Kosmabb8

Copy link
Copy Markdown
Contributor Author

Addressed the malformed deeply nested JSON finding in 89dc62f. The JSON decoder's RecursionError now follows the existing filename-specific validation-error path. Added parser and subprocess CLI regressions at depth 6000: exit 2, no traceback, previous destination bytes unchanged. Both tests failed before the correction. Windows CPython 3.12.10: 40 save tests, 1 import-argument test and 24 save-key tests passed; compilation and diff checks passed. Synthetic data only; AI-assisted.

@Kosmabb8

Copy link
Copy Markdown
Contributor Author

Portability follow-up in acffe1b: the Linux review of #3565 showed CPython 3.12 can accept 6,000 nested arrays, so I raised both matching pack-index fixtures here to 100,000 (the reviewer's Linux probe overflowed there). This prevents the test from accidentally reaching the different root-shape validation path. Production handling is unchanged. Windows CPython 3.12.10: 40 save tests pass; combined save branches 60 tests pass with 3 existing platform skips. Native Linux recheck remains pending. AI-assisted.

dev-ewann pushed a commit to dev-ewann/Leonida that referenced this pull request Nov 22, 2026
@ItsNotRexed
ItsNotRexed merged commit f0a4b10 into ItsNotRexed:main Nov 23, 2026
2 of 4 checks passed
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.

5 participants