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

Open
SalmanBarkat wants to merge 1 commit from upstream-pr/3309 into main
SalmanBarkat commented 2026-11-26 16:55:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SalmanBarkat on 2026-11-26T16:55:45Z.


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)
<!-- greyfinch-sync:Marvisak/VIctory:issue:5514065781 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3309). Originally posted by [SalmanBarkat](https://github.com/SalmanBarkat) on 2026-11-26T16:55:45Z. --- 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`)
Dukestar69 commented 2026-11-26 17:08:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T17:08:09Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6078082034 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3309#pullrequestreview-6078082034). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T17:08:09Z. **Review:** APPROVED --- 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).
cloph-dsp commented 2026-11-26 17:08:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T17:08:17Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7198553723 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3309#issuecomment-7198553723). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T17:08:17Z. --- 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).
fuzeofficlal commented 2026-11-26 20:24:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T20:24:26Z.

Review: APPROVED


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.
<!-- greyfinch-sync:Marvisak/VIctory:review:6080118172 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3309#pullrequestreview-6080118172). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T20:24:26Z. **Review:** APPROVED --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3309:upstream-pr/3309'
git switch 'upstream-pr/3309'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3309'
git switch 'upstream-pr/3309'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3309'
git switch 'upstream-pr/3309'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3309'
git switch 'main'
git merge --squash 'upstream-pr/3309'
git switch 'main'
git merge --ff-only 'upstream-pr/3309'
git switch 'main'
git merge 'upstream-pr/3309'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3309
No description provided.