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

Manually merged
github merged 1 commit from gh-pr/1812/head into gh-pr/1812/base 2026-11-26 10:06:36 +00:00
Owner

@⁠kirigayakazima on GitHub · 2026-11-25 12:25 UTC · original
kirigayakazima:fix/bcpack-bptc-safety → main · +167 −58 · 2 files · 1 commits · merged 2026-11-25 17:36 UTC

Claimed in the coordination issue as: BCPACK-BPTC-SECTION-AND-VALIDATION-SAFETY (#1608)

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)
**@⁠kirigayakazima** on GitHub · 2026-11-25 12:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1812) `kirigayakazima:fix/bcpack-bptc-safety` → `main` · +167 −58 · 2 files · 1 commits · merged 2026-11-25 17:36 UTC Claimed in the coordination issue as: BCPACK-BPTC-SECTION-AND-VALIDATION-SAFETY (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:06:35 +00:00
github manually merged commit 81e34a5695 into gh-pr/1812/base 2026-11-26 10:06:36 +00:00
Author
Owner

@⁠madvillain91 on GitHub · 2026-11-25 13:19 UTC · original · reviewed

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.

**@⁠madvillain91** on GitHub · 2026-11-25 13:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1812#pullrequestreview-6058350201) · **reviewed** 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.
Author
Owner

@⁠DeadlyChirp on GitHub · 2026-11-25 15:48 UTC · original

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

**@⁠DeadlyChirp** on GitHub · 2026-11-25 15:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1812#issuecomment-7167334930) 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.)
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 17:36 UTC · original

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

**@⁠aslan2142** on GitHub · 2026-11-25 17:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1812#issuecomment-7169739482) 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).
This discussion has been locked. Commenting is limited to contributors.
No description provided.