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

Open
opened 2026-11-26 20:21:54 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1611
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/psocache-usersig-guards


📝 Commits (1)

  • d8f2ac6 psocache_usersig: guard second open/mmap, validate --entry/limit/bounds, and reject empty path

📊 Changes

2 files changed (+137 additions, -41 deletions)

View changed files

📝 tests/test_psocache_usersig.py (+80 -0)
📝 tools/psocache_usersig.py (+57 -41)

📄 Description

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).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1611 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/psocache-usersig-guards` --- ### 📝 Commits (1) - [`d8f2ac6`](https://github.com/ubiquitarianism/leonida/commit/d8f2ac6f598db328630938f24893852bc832542f) psocache_usersig: guard second open/mmap, validate --entry/limit/bounds, and reject empty path ### 📊 Changes **2 files changed** (+137 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_psocache_usersig.py` (+80 -0) 📝 `tools/psocache_usersig.py` (+57 -41) </details> ### 📄 Description 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`). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1626
No description provided.