psocache_usersig: guard second open/mmap, validate --entry/limit/bounds, and reject empty path (PSOCACHE-USERSIG-GUARDS) #1611

Merged
IlyassRS3 merged 1 commit from upstream-pr/1611 into main 2026-11-28 10:38:38 +00:00
IlyassRS3 commented 2026-11-25 05:54:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T05:54:42Z.


Item ID: PSOCACHE-USERSIG-GUARDS (unlisted; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at submission time, and no open PR touches tools/psocache_usersig.py).

What changed

  • Guard the second open() / mmap.mmap() / decode pass in main() (tools/psocache_usersig.py): previously only psocache_inspect.inspect() was inside try/except, so a cache file disappearing, becoming unreadable, or shrinking between inspect() and the second open crashed with an unhandled traceback instead of returning exit code 2. Also check len(m) < report["size"] in shared helper _load_meta().
  • Check --entry bounds before re-opening or parsing meta_index: out-of-range --entry (negative or >= counts["graphics"]) now exits 2 immediately after inspect() instead of re-opening, mmapping, and parsing every meta chunk record first.
  • Reject empty/whitespace cache CLI arguments upfront with exit 2 usage error and enforce allow_nan=False on --json output.
  • Validate programmatic arguments and buffer bounds (summarise, describe_pso, stage_record_refs):
    • summarise() rejects non-int / bool / < 1 limit (previously limit=-3 returned "sampled": -3 and limit=True silently sampled 1).
    • describe_pso() rejects negative / bool / out-of-bounds index with ValueError instead of negative offset wraparound or raw struct.error.
    • stage_record_refs() rejects bool stage and checks a >= 0 and meta_off + a + p + 4 <= len(m) before unpacking.
  • Synthetic regression tests (tests/test_psocache_usersig.py): 4 new tests covering all of the above (no game files, no GPU).

Verification

  • AI-assisted: yes
  • GPUs tested: none (CPU-only Python tool on macOS)
  • python3 -W error -I tests/test_psocache_usersig.py -v: 12 passed
  • python3 -W error -I tests/test_text_write_line_endings.py: passed
  • python3 -I scripts/check_no_game_data.py tools/psocache_usersig.py tests/test_psocache_usersig.py: clean
  • python3 -m compileall -q tools/psocache_usersig.py tests/test_psocache_usersig.py: clean
  • Mutation test (tests/test_psocache_usersig.py against unpatched main:tools/psocache_usersig.py): 8 failures, 3 errors across the 12 tests (all 4 new tests fail on unpatched main).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5486008931 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1611). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T05:54:42Z. --- Item ID: `PSOCACHE-USERSIG-GUARDS` (unlisted; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at submission time, and no open PR touches `tools/psocache_usersig.py`). ## What changed - **Guard the second `open()` / `mmap.mmap()` / decode pass in `main()` (`tools/psocache_usersig.py`)**: previously only `psocache_inspect.inspect()` was inside `try/except`, so a cache file disappearing, becoming unreadable, or shrinking between `inspect()` and the second open crashed with an unhandled traceback instead of returning exit code `2`. Also check `len(m) < report["size"]` in shared helper `_load_meta()`. - **Check `--entry` bounds before re-opening or parsing `meta_index`**: out-of-range `--entry` (negative or `>= counts["graphics"]`) now exits `2` immediately after `inspect()` instead of re-opening, mmapping, and parsing every meta chunk record first. - **Reject empty/whitespace `cache` CLI arguments upfront** with exit `2` usage error and enforce `allow_nan=False` on `--json` output. - **Validate programmatic arguments and buffer bounds (`summarise`, `describe_pso`, `stage_record_refs`)**: - `summarise()` rejects non-`int` / `bool` / `< 1` `limit` (previously `limit=-3` returned `"sampled": -3` and `limit=True` silently sampled 1). - `describe_pso()` rejects negative / `bool` / out-of-bounds `index` with `ValueError` instead of negative offset wraparound or raw `struct.error`. - `stage_record_refs()` rejects `bool` `stage` and checks `a >= 0` and `meta_off + a + p + 4 <= len(m)` before unpacking. - **Synthetic regression tests (`tests/test_psocache_usersig.py`)**: 4 new tests covering all of the above (no game files, no GPU). ## Verification - AI-assisted: yes - GPUs tested: none (CPU-only Python tool on macOS) - `python3 -W error -I tests/test_psocache_usersig.py -v`: **12 passed** - `python3 -W error -I tests/test_text_write_line_endings.py`: passed - `python3 -I scripts/check_no_game_data.py tools/psocache_usersig.py tests/test_psocache_usersig.py`: clean - `python3 -m compileall -q tools/psocache_usersig.py tests/test_psocache_usersig.py`: clean - Mutation test (`tests/test_psocache_usersig.py` against unpatched `main:tools/psocache_usersig.py`): **8 failures, 3 errors** across the 12 tests (all 4 new tests fail on unpatched `main`).
copilot-pull-request-reviewer[bot] commented 2026-11-25 05:54:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T05:54:49Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6052534716 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1611#pullrequestreview-6052534716). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T05:54:49Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
MoonEyes commented 2026-11-28 10:38:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T10:38:32Z.


Red/green: the PR's tests fail on main and pass with this PR (Linux, Python 3.13.18).
Targeted batch of 18 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238027603 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1611#issuecomment-7238027603). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T10:38:32Z. --- Red/green: the PR's tests fail on main and pass with this PR (Linux, Python 3.13.18). Targeted batch of 18 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13. AI-assisted (Claude Code, Claude Opus 5.5)
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
ProjectLeonida/mandy!1611
No description provided.