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

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

📋 Pull Request Information

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

Base: main ← Head: fix/bcpack-tool-hardening


📝 Commits (1)

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

📊 Changes

2 files changed (+270 additions, -15 deletions)

View changed files

📝 tests/test_bcpack_tool.py (+180 -0)
📝 tools/bcpack_tool.py (+90 -15)

📄 Description

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.

🔄 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/1422 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/bcpack-tool-hardening` --- ### 📝 Commits (1) - [`e1fd565`](https://github.com/ubiquitarianism/leonida/commit/e1fd5654e520023533c17a6cc6cbf13bcb093fb7) bcpack_tool: single-pass profile, history overwrite guard, and atomic output (BCPACK-TOOL-HARDENING) ### 📊 Changes **2 files changed** (+270 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_bcpack_tool.py` (+180 -0) 📝 `tools/bcpack_tool.py` (+90 -15) </details> ### 📄 Description 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`. --- <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#1451
No description provided.