[PR #1812] [MERGED] tools: harden bcpack_tables_bptc.py section validation, bounds and file cache #1825

Closed
opened 2026-11-26 21:19:05 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1812
Author: @kirigayakazima
Created: 11/25/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142

Base: main ← Head: fix/bcpack-bptc-safety


📝 Commits (1)

  • 81e34a5 tools: harden bcpack_tables_bptc.py section validation, bounds and file cache

📊 Changes

2 files changed (+167 additions, -58 deletions)

View changed files

📝 tests/test_bcpack_tables_bptc.py (+85 -0)
📝 tools/bcpack_tables_bptc.py (+82 -58)

📄 Description

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)

🔄 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/1812 **Author:** [@kirigayakazima](https://github.com/kirigayakazima) **Created:** 11/25/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `fix/bcpack-bptc-safety` --- ### 📝 Commits (1) - [`81e34a5`](https://github.com/ubiquitarianism/leonida/commit/81e34a5695d96ce22a273f8a10578ccc9d45d72e) tools: harden bcpack_tables_bptc.py section validation, bounds and file cache ### 📊 Changes **2 files changed** (+167 additions, -58 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_bcpack_tables_bptc.py` (+85 -0) 📝 `tools/bcpack_tables_bptc.py` (+82 -58) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 21:19:05 +01:00
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#1825
No description provided.