formats: guard XMA Bits::read and Bits::signed against shift overflow #3309

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

@⁠SalmanBarkat on GitHub · 2026-11-26 16:55 UTC · original
SalmanBarkat:fix/xma-bits-shift-overflow → main · +51 −2 · 1 files · 1 commits · open

Fixes #907

Summary

Guards the XMA bitstream reader (Bits::read and Bits::signed in crates/victory-formats/src/xma.rs) against shift-overflow panics and arithmetic underflow on boundary bit widths (0, 32, and > 32).

What the Issue Was

In crates/victory-formats/src/xma.rs:

  1. Bits::signed(n) computed ((self.read(n) << (32 - n)) as i32) >> (32 - n) unconditionally.
    • When n == 0, 32 - n evaluates to 32, triggering a shift-overflow panic in debug builds (and incorrect shifts in release builds).
    • When n > 32, 32 - n triggers an unsigned integer underflow panic in debug builds (and wraps in release builds).
  2. Bits::read(n) guarded n == 0, but computed self.peek32() >> (32 - n) for all nonzero widths. Any n > 32 would underflow 32 - n.

How It Was Resolved

  • Bits::read(n):
    • Returns 0 immediately when n == 0.
    • When n > 32, advances self.pos past the high n - 32 bits using saturating_add and reads the remaining 32 bits cleanly without underflowing 32 - n.
    • Uses saturating_add when advancing self.pos so bit offsets cannot wrap.
  • Bits::signed(n):
    • Returns 0 immediately when n == 0.
    • When n >= 32, reads the value and converts directly via v as i32 without performing an invalid shift.
    • For 1..=31, performs standard two's-complement sign extension with shift = 32 - n.
  • Synthetic Unit Test:
    • Added xma::tests::bits_boundary_widths_do_not_overflow covering bit widths 0, 1, 32, and 40 for both Bits::read and Bits::signed (no game data required).

Verification

  • cargo clippy -p victory-formats --all-targets --locked -- -D warnings: passed (0 warnings)
  • cargo clippy -p victory-formats --all-targets --all-features --locked -- -D warnings: passed (0 warnings)
  • cargo test -p victory-formats --locked: 47 passed, 0 failed (5 ignored: benchmarks / external-file tests)
  • cargo xtask policy: passed (191 files ok)
**@⁠SalmanBarkat** on GitHub · 2026-11-26 16:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/3309) `SalmanBarkat:fix/xma-bits-shift-overflow` → `main` · +51 −2 · 1 files · 1 commits · open Fixes #907 ### Summary Guards the XMA bitstream reader (`Bits::read` and `Bits::signed` in `crates/victory-formats/src/xma.rs`) against shift-overflow panics and arithmetic underflow on boundary bit widths (`0`, `32`, and `> 32`). ### What the Issue Was In `crates/victory-formats/src/xma.rs`: 1. **`Bits::signed(n)`** computed `((self.read(n) << (32 - n)) as i32) >> (32 - n)` unconditionally. - When `n == 0`, `32 - n` evaluates to `32`, triggering a shift-overflow panic in debug builds (and incorrect shifts in release builds). - When `n > 32`, `32 - n` triggers an unsigned integer underflow panic in debug builds (and wraps in release builds). 2. **`Bits::read(n)`** guarded `n == 0`, but computed `self.peek32() >> (32 - n)` for all nonzero widths. Any `n > 32` would underflow `32 - n`. ### How It Was Resolved - **`Bits::read(n)`**: - Returns `0` immediately when `n == 0`. - When `n > 32`, advances `self.pos` past the high `n - 32` bits using `saturating_add` and reads the remaining 32 bits cleanly without underflowing `32 - n`. - Uses `saturating_add` when advancing `self.pos` so bit offsets cannot wrap. - **`Bits::signed(n)`**: - Returns `0` immediately when `n == 0`. - When `n >= 32`, reads the value and converts directly via `v as i32` without performing an invalid shift. - For `1..=31`, performs standard two's-complement sign extension with `shift = 32 - n`. - **Synthetic Unit Test**: - Added `xma::tests::bits_boundary_widths_do_not_overflow` covering bit widths `0`, `1`, `32`, and `40` for both `Bits::read` and `Bits::signed` (no game data required). ### Verification - `cargo clippy -p victory-formats --all-targets --locked -- -D warnings`: passed (0 warnings) - `cargo clippy -p victory-formats --all-targets --all-features --locked -- -D warnings`: passed (0 warnings) - `cargo test -p victory-formats --locked`: 47 passed, 0 failed (5 ignored: benchmarks / external-file tests) - `cargo xtask policy`: passed (`191 files ok`)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:57:43 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 17:08 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64:

  • Guarding Bits::read (n > 32) and Bits::signed (n == 0 / n >= 32) with saturating position advances cleanly prevents shift overflow/underflow on boundary bit widths.
  • cargo test -p victory-formats --lib xma:: -- --test-threads=1: 21 passed, 0 failed, 3 ignored (including bits_boundary_widths_do_not_overflow).
  • cargo clippy -p victory-formats --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 17:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3309#pullrequestreview-6078082034) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64: - Guarding `Bits::read` (`n > 32`) and `Bits::signed` (`n == 0` / `n >= 32`) with saturating position advances cleanly prevents shift overflow/underflow on boundary bit widths. - `cargo test -p victory-formats --lib xma:: -- --test-threads=1`: 21 passed, 0 failed, 3 ignored (including `bits_boundary_widths_do_not_overflow`). - `cargo clippy -p victory-formats --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 17:08 UTC · original

Independent local verification of PR #3309 — "formats: guard XMA Bits::read and Bits::signed against shift overflow".

No game data, assets or extracted shaders were used. Windows, Rust/Cargo 1.100.0 (MSVC), repository-pinned toolchain 1.98.1 noted; per-worktree CARGO_TARGET_DIR=D:\Victory-Contrib\target-3309, CARGO_BUILD_JOBS=2. Head verified: 6ca75ce.

Exact test:
cargo test --locked -p victory-formats --lib -- --exact xma::tests::bits_boundary_widths_do_not_overflow
→ 1 passed, 0 failed, 51 filtered out, exit 0.

git diff --check origin/main HEAD → exit 0.

Negative control: the test name is absent on the merge base 63bcca43 (git grep -c returned no matches), confirming the regression test is added by this PR.

Caveat: only the synthetic boundary-width test was run; no title XMA stream was decoded. AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only crate).

**@⁠cloph-dsp** on GitHub · 2026-11-26 17:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3309#issuecomment-7198553723) Independent local verification of PR #3309 — "formats: guard XMA Bits::read and Bits::signed against shift overflow". No game data, assets or extracted shaders were used. Windows, Rust/Cargo 1.100.0 (MSVC), repository-pinned toolchain 1.98.1 noted; per-worktree `CARGO_TARGET_DIR=D:\Victory-Contrib\target-3309`, `CARGO_BUILD_JOBS=2`. Head verified: `6ca75ce`. Exact test: `cargo test --locked -p victory-formats --lib -- --exact xma::tests::bits_boundary_widths_do_not_overflow` → 1 passed, 0 failed, 51 filtered out, exit 0. `git diff --check origin/main HEAD` → exit 0. Negative control: the test name is absent on the merge base `63bcca43` (`git grep -c` returned no matches), confirming the regression test is added by this PR. Caveat: only the synthetic boundary-width test was run; no title XMA stream was decoded. AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only crate).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 20:24 UTC · original · approved these changes

LGTM — guarding Bits::read (n > 32) and Bits::signed (n == 0, n >= 32) with saturating_add resolves the shift overflow/underflow panics while preserving exact behavior for 1..=32 across all existing decoder callers.

Verified

  • cargo clippy --locked -p victory-formats --all-targets -- -D warnings (default & --all-features): clean
  • cargo test --locked -p victory-formats --all-targets: 47 passed, 0 failed, 5 ignored (including xma::tests::bits_boundary_widths_do_not_overflow)
  • rustfmt --edition 2024: clean on all PR-touched hunks

Minor non-blocking notes

  • If self.pos ever saturates at usize::MAX when self.end == usize::MAX (e.g., on a 32-bit target with a malformed stream), overread() (self.pos > self.end at crates/victory-formats/src/xma.rs:247-249) remains false.
  • bits_boundary_widths_do_not_overflow covers byte-aligned widths 0, 1, 32, and 40; adding an unaligned wide read where bit 31 of the retained word is set (and u32::MAX saturation) would round out boundary coverage.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 20:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/3309#pullrequestreview-6080118172) · **approved these changes** LGTM — guarding `Bits::read` (`n > 32`) and `Bits::signed` (`n == 0`, `n >= 32`) with `saturating_add` resolves the shift overflow/underflow panics while preserving exact behavior for `1..=32` across all existing decoder callers. ### Verified - `cargo clippy --locked -p victory-formats --all-targets -- -D warnings` (default & `--all-features`): clean - `cargo test --locked -p victory-formats --all-targets`: 47 passed, 0 failed, 5 ignored (including `xma::tests::bits_boundary_widths_do_not_overflow`) - `rustfmt --edition 2024`: clean on all PR-touched hunks ### Minor non-blocking notes - If `self.pos` ever saturates at `usize::MAX` when `self.end == usize::MAX` (e.g., on a 32-bit target with a malformed stream), `overread()` (`self.pos > self.end` at `crates/victory-formats/src/xma.rs:247-249`) remains `false`. - `bits_boundary_widths_do_not_overflow` covers byte-aligned widths `0`, `1`, `32`, and `40`; adding an unaligned wide read where bit 31 of the retained word is set (and `u32::MAX` saturation) would round out boundary coverage.
This pull request can be merged automatically.
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/3309/head:github-bot/gh-3309'
git switch 'github-bot/gh-3309'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3309'
git switch 'github-bot/gh-3309'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3309'
git switch 'github-bot/gh-3309'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3309'
git switch 'main'
git merge --squash 'github-bot/gh-3309'
git switch 'main'
git merge --ff-only 'github-bot/gh-3309'
git switch 'main'
git merge 'github-bot/gh-3309'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3309
No description provided.