script: extract main(argv) and guard malformed RPF9/PSIN inputs in script_state_probe with unit suite #2344

Merged
Ammmae0x merged 1 commit from upstream-pr/2352 into main 2026-11-25 14:07:54 +00:00
Ammmae0x commented 2026-11-25 13:19:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T13:19:29Z.


Change

Extract main(argv: list[str] | None = None) -> int in tools/script_state_probe.py, harden malformed RPF9/PSIN input handling, and add a pure-Python unit and CLI validation suite (tests/script_state_probe_check.py, registered as script_state_probe_checks in CMakeLists.txt):

  • Forward argv to parser.parse_args(argv) and return exit code 0 on success (if __name__ == "__main__": raise SystemExit(main())).
  • Guard 0-byte archives via os.fstat(stream.fileno()).st_size == 0 before mmap.mmap in inspect() so empty files raise ValueError("Empty archive; expected an RPF9 archive") instead of an unhandled mmap error.
  • Catch (OSError, ValueError, struct.error, zlib.error) in main() via parser.exit(1, ...) so missing paths, directories, empty files, bad magic, malformed RPF9/PSIN trees, and decode errors exit cleanly with code 1 and zero raw tracebacks.

Verification

Verified on commit b38bef1f (based on upstream/main 214ed093):

  • out/pe-env/Scripts/python.exe -B tests/script_state_probe_check.py -v -> Ran 7 tests in 0.251s / OK (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/script_state_check.py -> Ran 22 tests in 0.849s / OK (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py -> form coverage checks passed (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py -> Self-test passed. Checked 19 Markdown files and 457 links. (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py -> Self-test passed. Checked 1642 tracked source, script and build/CI files: no bidirectional controls. (exit 0)

Evidence and follow-up

  • All test fixtures in tests/script_state_probe_check.py are synthetic in-memory RPF9 archives with 16-byte-aligned zlib-compressed PSIN/LSCH/LENM/LSNS/LGID/LRSC/CHKS records.
  • Unit and negative controls cover joaat(), literal(), verified_camera(), tree_arrays(), inspect_tree(), inspect() (single witness vs ambiguous multi-candidate), missing/directory/0-byte archives (verifying mmap.mmap is never called on 0-byte files), bad magic, corrupt zlib, invalid CHKS length/total, bad LRSC magic/directory sizes, bad root ID, invalid instance marker, truncated schema members, duplicate/non-label state IDs, and struct.error/zlib.error CLI exit handling.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6605408123 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2352). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T13:19:29Z. --- ## Change Extract `main(argv: list[str] | None = None) -> int` in `tools/script_state_probe.py`, harden malformed RPF9/PSIN input handling, and add a pure-Python unit and CLI validation suite (`tests/script_state_probe_check.py`, registered as `script_state_probe_checks` in `CMakeLists.txt`): - Forward `argv` to `parser.parse_args(argv)` and return exit code `0` on success (`if __name__ == "__main__": raise SystemExit(main())`). - Guard 0-byte archives via `os.fstat(stream.fileno()).st_size == 0` before `mmap.mmap` in `inspect()` so empty files raise `ValueError("Empty archive; expected an RPF9 archive")` instead of an unhandled `mmap` error. - Catch `(OSError, ValueError, struct.error, zlib.error)` in `main()` via `parser.exit(1, ...)` so missing paths, directories, empty files, bad magic, malformed RPF9/PSIN trees, and decode errors exit cleanly with code `1` and zero raw tracebacks. ## Verification Verified on commit `b38bef1f` (based on `upstream/main` `214ed093`): - `out/pe-env/Scripts/python.exe -B tests/script_state_probe_check.py -v` -> `Ran 7 tests in 0.251s` / `OK` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/script_state_check.py` -> `Ran 22 tests in 0.849s` / `OK` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` -> `form coverage checks passed` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` -> `Self-test passed. Checked 19 Markdown files and 457 links.` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` -> `Self-test passed. Checked 1642 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) ## Evidence and follow-up - All test fixtures in `tests/script_state_probe_check.py` are synthetic in-memory `RPF9` archives with 16-byte-aligned zlib-compressed `PSIN/LSCH/LENM/LSNS/LGID/LRSC/CHKS` records. - Unit and negative controls cover `joaat()`, `literal()`, `verified_camera()`, `tree_arrays()`, `inspect_tree()`, `inspect()` (single witness vs ambiguous multi-candidate), missing/directory/0-byte archives (verifying `mmap.mmap` is never called on 0-byte files), bad magic, corrupt zlib, invalid `CHKS` length/total, bad `LRSC` magic/directory sizes, bad root ID, invalid instance marker, truncated schema members, duplicate/non-label state IDs, and `struct.error`/`zlib.error` CLI exit handling. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
tedin7 commented 2026-11-25 14:04:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T13:20:49Z.

Review: COMMENTED


Review of head b38bef1f452763d820b20f8bc434bb7410ec8d2a (comment only).

Summary: Small, safe change. main(argv) is extracted, an empty-file guard is added, and struct.error / zlib.error are now reported as clean exit-1 errors. A 7-test unit suite is registered in CTest as script_state_probe_checks. No P1 or P2 findings.

Checks (run locally on the PR worktree):

  • python3 -B tests/script_state_probe_check.py: Ran 7 tests, OK
  • python3 tests/docs_links_check.py: self-test passed, 19 files and 457 links checked
  • python3 tests/bidi_controls_check.py: no bidirectional controls in 1642 files
  • git diff --check origin/main...HEAD: clean
  • Mergeable. The title is neutral. No clean-room concerns: the diff touches only a probe tool, its test and CMake. CI was still pending when I looked.

Mutations (both killed by the suite):

  1. Narrowing except (OSError, ValueError, struct.error, zlib.error) back to (OSError, ValueError) gave FAILED (errors=2).
  2. Disabling the empty-file guard (st_size == 0 changed to == -1) gave FAILED (failures=1).

The worktree was reverted after each mutation.

Nits (non-blocking):

  • nit: tools/script_state_probe.py (inspect): the explicit empty-file check duplicates what mmap would reject with its own ValueError. It is still useful because it gives a clearer message.
  • nit: CI checks were still pending at review time. Merge should wait for them to go green.

No Windows/WARP run was needed. There is no shader or C++ change.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6058367796 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2352#pullrequestreview-6058367796). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T13:20:49Z. **Review:** COMMENTED --- Review of head `b38bef1f452763d820b20f8bc434bb7410ec8d2a` (comment only). **Summary:** Small, safe change. `main(argv)` is extracted, an empty-file guard is added, and `struct.error` / `zlib.error` are now reported as clean exit-1 errors. A 7-test unit suite is registered in CTest as `script_state_probe_checks`. No P1 or P2 findings. **Checks (run locally on the PR worktree):** - `python3 -B tests/script_state_probe_check.py`: Ran 7 tests, OK - `python3 tests/docs_links_check.py`: self-test passed, 19 files and 457 links checked - `python3 tests/bidi_controls_check.py`: no bidirectional controls in 1642 files - `git diff --check origin/main...HEAD`: clean - Mergeable. The title is neutral. No clean-room concerns: the diff touches only a probe tool, its test and CMake. CI was still pending when I looked. **Mutations (both killed by the suite):** 1. Narrowing `except (OSError, ValueError, struct.error, zlib.error)` back to `(OSError, ValueError)` gave `FAILED (errors=2)`. 2. Disabling the empty-file guard (`st_size == 0` changed to `== -1`) gave `FAILED (failures=1)`. The worktree was reverted after each mutation. **Nits (non-blocking):** - nit: `tools/script_state_probe.py` (`inspect`): the explicit empty-file check duplicates what `mmap` would reject with its own `ValueError`. It is still useful because it gives a clearer message. - nit: CI checks were still pending at review time. Merge should wait for them to go green. No Windows/WARP run was needed. There is no shader or C++ change. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 15:01:38 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2344
No description provided.