formats: guard XMA Bits::read and Bits::signed against shift overflow #3309
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3309
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3309"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@SalmanBarkat on GitHub · 2026-11-26 16:55 UTC · original
SalmanBarkat:fix/xma-bits-shift-overflow→main· +51 −2 · 1 files · 1 commits · openFixes #907
Summary
Guards the XMA bitstream reader (
Bits::readandBits::signedincrates/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:Bits::signed(n)computed((self.read(n) << (32 - n)) as i32) >> (32 - n)unconditionally.n == 0,32 - nevaluates to32, triggering a shift-overflow panic in debug builds (and incorrect shifts in release builds).n > 32,32 - ntriggers an unsigned integer underflow panic in debug builds (and wraps in release builds).Bits::read(n)guardedn == 0, but computedself.peek32() >> (32 - n)for all nonzero widths. Anyn > 32would underflow32 - n.How It Was Resolved
Bits::read(n):0immediately whenn == 0.n > 32, advancesself.pospast the highn - 32bits usingsaturating_addand reads the remaining 32 bits cleanly without underflowing32 - n.saturating_addwhen advancingself.posso bit offsets cannot wrap.Bits::signed(n):0immediately whenn == 0.n >= 32, reads the value and converts directly viav as i32without performing an invalid shift.1..=31, performs standard two's-complement sign extension withshift = 32 - n.xma::tests::bits_boundary_widths_do_not_overflowcovering bit widths0,1,32, and40for bothBits::readandBits::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 on GitHub · 2026-11-26 17:08 UTC · original · approved these changes
Reviewed the diff and verified locally on Windows 11 x64:
Bits::read(n > 32) andBits::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 (includingbits_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 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 -creturned 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 on GitHub · 2026-11-26 20:24 UTC · original · approved these changes
LGTM — guarding
Bits::read(n > 32) andBits::signed(n == 0,n >= 32) withsaturating_addresolves the shift overflow/underflow panics while preserving exact behavior for1..=32across all existing decoder callers.Verified
cargo clippy --locked -p victory-formats --all-targets -- -D warnings(default &--all-features): cleancargo test --locked -p victory-formats --all-targets: 47 passed, 0 failed, 5 ignored (includingxma::tests::bits_boundary_widths_do_not_overflow)rustfmt --edition 2024: clean on all PR-touched hunksMinor non-blocking notes
self.posever saturates atusize::MAXwhenself.end == usize::MAX(e.g., on a 32-bit target with a malformed stream),overread()(self.pos > self.endatcrates/victory-formats/src/xma.rs:247-249) remainsfalse.bits_boundary_widths_do_not_overflowcovers byte-aligned widths0,1,32, and40; adding an unaligned wide read where bit 31 of the retained word is set (andu32::MAXsaturation) would round out boundary coverage.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.