tools: harden bcpack_tables_bptc.py section validation, bounds and file cache #1812

Merged
kirigayakazima merged 1 commit from upstream-pr/1812 into main 2026-11-25 17:36:11 +00:00
kirigayakazima commented 2026-11-25 12:25:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-25T12:25:20Z.


Claimed in the coordination issue as: BCPACK-BPTC-SECTION-AND-VALIDATION-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162308818)

What and why

  • Extract _load_json_file() in tools/bcpack_tables_bptc.py for both _read_history_section() and load_bc_history(), keying _FILE_CACHE by os.path.realpath and (st_mtime_ns, st_size, st_dev, st_ino) so same-size, same-mtime atomic file replacements invalidate cleanly, and translating RecursionError to ValueError.
  • Reject non-object codec sections (e.g. {"bc6h": []}, {"bc7": "bad"}) with ValueError in _read_history_section() instead of failing later with AttributeError.
  • Validate container types (list/tuple) in load_bc6h() (endpointHistory, indexHistory, endpointConfirmedBits, indexConfirmedBits) and in load_bc7() (historyEndpointLow2Fits, historyEndpointLastResort) so scalar rows raise ValueError instead of TypeError.
  • Bound-check ep_cb[k][field] (0 <= cb <= 8) before computing 8 - max(depth, cb) in historyEndpointLow2Fits (avoiding negative shift counts), and validate all indices in historyEndpointLastResort.
  • Add regression tests in tests/test_bcpack_tables_bptc.py.

Tested

  • python -I tests/test_bcpack_tables_bptc.py -v -> Ran 27 tests in 0.077s, OK (verified in a detached upstream/main worktree that all six new tests fail before the fix: FAILED (failures=8, errors=11)).
  • Sibling suites test_bcpack.py, test_bcpack_history_calib.py, test_bcpack_native.py (skipped=12), test_bcpack_tables.py, and test_bcpack_tool.py -> exit 0.
  • python -I tests/test_ci_registration.py -> Ran 16 tests in 4.487s, OK.
  • python -I tests/test_text_write_line_endings.py -> Ran 3 tests in 12.675s, OK.
  • python -I scripts/check_no_game_data.py tools/bcpack_tables_bptc.py tests/test_bcpack_tables_bptc.py -> exit 0.
  • python -I -m compileall -q tools/bcpack_tables_bptc.py tests/test_bcpack_tables_bptc.py and git diff --check -> clean.

GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5491278743 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1812). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-25T12:25:20Z. --- Claimed in the coordination issue as: BCPACK-BPTC-SECTION-AND-VALIDATION-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162308818) ### What and why - Extract `_load_json_file()` in `tools/bcpack_tables_bptc.py` for both `_read_history_section()` and `load_bc_history()`, keying `_FILE_CACHE` by `os.path.realpath` and `(st_mtime_ns, st_size, st_dev, st_ino)` so same-size, same-mtime atomic file replacements invalidate cleanly, and translating `RecursionError` to `ValueError`. - Reject non-object codec sections (e.g. `{"bc6h": []}`, `{"bc7": "bad"}`) with `ValueError` in `_read_history_section()` instead of failing later with `AttributeError`. - Validate container types (`list`/`tuple`) in `load_bc6h()` (`endpointHistory`, `indexHistory`, `endpointConfirmedBits`, `indexConfirmedBits`) and in `load_bc7()` (`historyEndpointLow2Fits`, `historyEndpointLastResort`) so scalar rows raise `ValueError` instead of `TypeError`. - Bound-check `ep_cb[k][field]` (`0 <= cb <= 8`) before computing `8 - max(depth, cb)` in `historyEndpointLow2Fits` (avoiding negative shift counts), and validate all indices in `historyEndpointLastResort`. - Add regression tests in `tests/test_bcpack_tables_bptc.py`. ### Tested - `python -I tests/test_bcpack_tables_bptc.py -v` -> `Ran 27 tests in 0.077s`, `OK` (verified in a detached `upstream/main` worktree that all six new tests fail before the fix: `FAILED (failures=8, errors=11)`). - Sibling suites `test_bcpack.py`, `test_bcpack_history_calib.py`, `test_bcpack_native.py` (`skipped=12`), `test_bcpack_tables.py`, and `test_bcpack_tool.py` -> exit `0`. - `python -I tests/test_ci_registration.py` -> `Ran 16 tests in 4.487s`, `OK`. - `python -I tests/test_text_write_line_endings.py` -> `Ran 3 tests in 12.675s`, `OK`. - `python -I scripts/check_no_game_data.py tools/bcpack_tables_bptc.py tests/test_bcpack_tables_bptc.py` -> exit `0`. - `python -I -m compileall -q tools/bcpack_tables_bptc.py tests/test_bcpack_tables_bptc.py` and `git diff --check` -> clean. GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64) AI-assisted: yes ### Checklist - [x] Based on current main and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
madvillain91 commented 2026-11-25 13:19:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by madvillain91 on 2026-11-25T13:19:46Z.

Review: COMMENTED


Independent Windows 11 26H2 (26300.9711) check at 81e34a569 against main 384abe5b0 (PR base 0d80bc5df), using CPython 3.12.3 (python -I) and Git for Windows 2.56.0.windows.2. All 46 tests passed across test_bcpack_tables_bptc.py (27/27 in 0.195s), test_ci_registration.py (16/16 in 7.738s), and test_text_write_line_endings.py (3/3 in 15.670s), with check_no_game_data.py and compileall clean. Verified realpath + (st_mtime_ns, st_size, st_dev, st_ino) cache invalidation on Windows NTFS same-size/same-mtime os.replace, non-dict/scalar/bounds validation, RecursionError -> ValueError conversion, and BC7 confirmed-bits <= 8 guard preventing negative shifts. LGTM.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6058350201 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1812#pullrequestreview-6058350201). Originally posted by [madvillain91](https://github.com/madvillain91) on 2026-11-25T13:19:46Z. **Review:** COMMENTED --- Independent Windows 11 26H2 (26300.9711) check at 81e34a569 against main 384abe5b0 (PR base 0d80bc5df), using CPython 3.12.3 (python -I) and Git for Windows 2.56.0.windows.2. All 46 tests passed across test_bcpack_tables_bptc.py (27/27 in 0.195s), test_ci_registration.py (16/16 in 7.738s), and test_text_write_line_endings.py (3/3 in 15.670s), with check_no_game_data.py and compileall clean. Verified realpath + (st_mtime_ns, st_size, st_dev, st_ino) cache invalidation on Windows NTFS same-size/same-mtime os.replace, non-dict/scalar/bounds validation, RecursionError -> ValueError conversion, and BC7 confirmed-bits <= 8 guard preventing negative shifts. LGTM.
DeadlyChirp commented 2026-11-25 15:48:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DeadlyChirp on 2026-11-25T15:48:45Z.


Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

python -m pytest -q tests/test_bcpack_tables_bptc.py:

  • PR: 27 passed. main: 21 passed. The new tests pass on Windows.

(AI-assisted test run; output checked by hand.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7167334930 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1812#issuecomment-7167334930). Originally posted by [DeadlyChirp](https://github.com/DeadlyChirp) on 2026-11-25T15:48:45Z. --- Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. `python -m pytest -q tests/test_bcpack_tables_bptc.py`: - PR: 27 passed. main: 21 passed. The new tests pass on Windows. (AI-assisted test run; output checked by hand.)
aslan2142 commented 2026-11-25 17:36:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-25T17:36:14Z.


Merged. Thank you, @kirigayakazima. Section validation, bounds and file handling are hardened.

  • The new tests fail on main and pass here; the docs, line-ending and CI/ctest registration checks pass, and so does the game-data check.
  • Checked on main 360c728bd + 81e34a569: the semantic suite passes (490 files, 6,292 tests, 0 files failed); test_bcpack_tables_bptc pass; the 15 docs and writer tests pass; check_no_game_data.py is clean on the 2 changed files.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7169739482 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1812#issuecomment-7169739482). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-25T17:36:14Z. --- Merged. Thank you, @kirigayakazima. Section validation, bounds and file handling are hardened. - The new tests fail on `main` and pass here; the docs, line-ending and CI/ctest registration checks pass, and so does the game-data check. - **Checked on `main` `360c728bd` + `81e34a569`:** the semantic suite passes (490 files, 6,292 tests, 0 files failed); `test_bcpack_tables_bptc` pass; the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 2 changed files. 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!1812
No description provided.