bcpack_tool: single-pass profile, history overwrite guard, and atomic output (BCPACK-TOOL-HARDENING) #1422

Open
github-bot wants to merge 1 commit from github-bot/gh-1422 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 23:51 UTC · original
IlyassRS3:fix/bcpack-tool-hardening → main · +270 −15 · 2 files · 1 commits · open

Item ID: BCPACK-TOOL-HARDENING
AI-assisted: yes

Summary

Fixes five bugs in tools/bcpack_tool.py:

  1. Eliminate double rANS decode in profile_stream(): profile_stream() previously called inspect_stream() and then called bcpack.decode_lanes() a second time on the same input. Extracted _inspect_decoded_lanes() so both inspect_stream() and profile_stream() decode the bitstream exactly once.
  2. Refuse overwriting --history in decode_file(): decode_file() guarded in_path via same_report_path, but allowed out_path to overwrite a --history JSON file (directly or via symlink/hardlink/case-folded alias) or the per-codec <history_dir>/<codec>.json file read by bcpack_tables_bptc. Both are now protected via same_report_path (while preserving dict history support for Python API callers).
  3. Fix --history error attribution for directories with a trailing slash: os.path.basename("/path/to/hist_dir/") returns "", and "" not in str(e) was always False, silently dropping filename attribution on json.JSONDecodeError. _history_paths() normalizes the directory path and resolves to the concrete <codec>.json file when present so errors always name the failing history file.
  4. Atomic output publishing & early directory/symlink-loop checks: decode_file() now rejects symlink cycles (ELOOP) and existing directories (IsADirectoryError) before reading/decoding, preserves symlinks by resolving os.path.realpath(out_path) (matching tools/glb_inspect.py), stages regular-file writes through tempfile.NamedTemporaryFile + file_mode.published_mode + os.replace, and cleans up staged files on failure so existing output bytes are never truncated on partial write errors.
  5. Handle bcpack.BcpackUnsupportedCodec and redirected stdout cleanly in CLI: main() now catches bcpack.BcpackUnsupportedCodec (NotImplementedError) alongside (ValueError, OSError) so it emits one ERROR: line and exits 2 instead of printing a traceback, and guards sys.stdout.reconfigure(errors="replace").

Verification (macOS Apple M5, arm64, Python 3.14; no game files)

  • python3 -m compileall -q make_game.py tools tests — OK
  • python3 -I scripts/check_no_game_data.py tools/bcpack_tool.py tests/test_bcpack_tool.py — OK
  • python3 -I tests/test_bcpack_tool.py -v — 28/28 OK (10 new regression tests)
  • python3 -I tests/test_bcpack.py — 20/20 OK
  • python3 -I tests/test_bcpack_tables.py — 34/34 OK
  • python3 -I tests/test_bcpack_tables_bptc.py — 21/21 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • python3 -I tests/test_ci_registration.py — 16/16 OK
  • Mutation proof (running the new tests/test_bcpack_tool.py against unpatched upstream/main 364ab4eab): FAILED (failures=6, errors=1) — catches double decode_lanes, overwriting --history file / symlink / <dir>/bc1.json, dropped trailing-slash history attribution, non-atomic overwrite, and uncaught BcpackUnsupportedCodec.
**@⁠IlyassRS3** on GitHub · 2026-11-24 23:51 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1422) `IlyassRS3:fix/bcpack-tool-hardening` → `main` · +270 −15 · 2 files · 1 commits · open Item ID: `BCPACK-TOOL-HARDENING` AI-assisted: yes ### Summary Fixes five bugs in `tools/bcpack_tool.py`: 1. **Eliminate double rANS decode in `profile_stream()`**: `profile_stream()` previously called `inspect_stream()` and then called `bcpack.decode_lanes()` a second time on the same input. Extracted `_inspect_decoded_lanes()` so both `inspect_stream()` and `profile_stream()` decode the bitstream exactly once. 2. **Refuse overwriting `--history` in `decode_file()`**: `decode_file()` guarded `in_path` via `same_report_path`, but allowed `out_path` to overwrite a `--history` JSON file (directly or via symlink/hardlink/case-folded alias) or the per-codec `<history_dir>/<codec>.json` file read by `bcpack_tables_bptc`. Both are now protected via `same_report_path` (while preserving `dict` history support for Python API callers). 3. **Fix `--history` error attribution for directories with a trailing slash**: `os.path.basename("/path/to/hist_dir/")` returns `""`, and `"" not in str(e)` was always `False`, silently dropping filename attribution on `json.JSONDecodeError`. `_history_paths()` normalizes the directory path and resolves to the concrete `<codec>.json` file when present so errors always name the failing history file. 4. **Atomic output publishing & early directory/symlink-loop checks**: `decode_file()` now rejects symlink cycles (`ELOOP`) and existing directories (`IsADirectoryError`) before reading/decoding, preserves symlinks by resolving `os.path.realpath(out_path)` (matching `tools/glb_inspect.py`), stages regular-file writes through `tempfile.NamedTemporaryFile` + `file_mode.published_mode` + `os.replace`, and cleans up staged files on failure so existing output bytes are never truncated on partial write errors. 5. **Handle `bcpack.BcpackUnsupportedCodec` and redirected stdout cleanly in CLI**: `main()` now catches `bcpack.BcpackUnsupportedCodec` (`NotImplementedError`) alongside `(ValueError, OSError)` so it emits one `ERROR:` line and exits 2 instead of printing a traceback, and guards `sys.stdout.reconfigure(errors="replace")`. ### Verification (macOS Apple M5, arm64, Python 3.14; no game files) - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/bcpack_tool.py tests/test_bcpack_tool.py` — OK - `python3 -I tests/test_bcpack_tool.py -v` — 28/28 OK (10 new regression tests) - `python3 -I tests/test_bcpack.py` — 20/20 OK - `python3 -I tests/test_bcpack_tables.py` — 34/34 OK - `python3 -I tests/test_bcpack_tables_bptc.py` — 21/21 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - `python3 -I tests/test_ci_registration.py` — 16/16 OK - **Mutation proof** (running the new `tests/test_bcpack_tool.py` against unpatched `upstream/main` `364ab4eab`): `FAILED (failures=6, errors=1)` — catches double `decode_lanes`, overwriting `--history` file / symlink / `<dir>/bc1.json`, dropped trailing-slash history attribution, non-atomic overwrite, and uncaught `BcpackUnsupportedCodec`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:50:55 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 23:51 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 23:51 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1422#pullrequestreview-6049324159) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1422/head:github-bot/gh-1422'
git switch 'github-bot/gh-1422'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1422'
git switch 'github-bot/gh-1422'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1422'
git switch 'github-bot/gh-1422'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1422'
git switch 'main'
git merge --squash 'github-bot/gh-1422'
git switch 'main'
git merge --ff-only 'github-bot/gh-1422'
git switch 'main'
git merge 'github-bot/gh-1422'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.