formats: guard bitstream signed and 32-bit reads against shift overflow in XMA (fix on a fork) #907
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#907
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@DaniiL35 on GitHub · 2026-11-24 13:55 UTC · original
Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a clean fork branch off
main(ffe310e).• Branch: https://github.com/DaniiL35/VIctory/tree/fix/xma-bits-shift-overflow
• Commit:
0c38285(based onmainffe310e,crates/victory-formats/src/xma.rs+38)• Compare: https://github.com/Marvisak/VIctory/compare/main...DaniiL35:VIctory:fix/xma-bits-shift-overflow
Problem
In
crates/victory-formats/src/xma.rs:Bits::signed(n)(lines 192–194) performed: Unlikeread(n)above it which checkedif n == 0 { return 0; },signed(n)did not check forn == 0. Whenn == 0,32 - nis32. Shifting a 32-bit integer by 32 bits (val << 32orval >> 32) is undefined behavior in C/C++ and triggers an unconditional overflow panic in Rust (attempt to shift left with overflow).n > 32,32 - nunderflowsu32, triggering an arithmetic underflow panic.Bits::read(n), ifn >= 32, computing32 - nalso suffers from shift overflow or underflow.Fix
signed(n): return0whenn == 0, and returnself.read(n) as i32whenn >= 32.read(n): whenn >= 32, returnself.peek32()directly, advancingself.pos += n as usize.bitstream_handles_zero_and_boundary_bit_widthsassertingn == 0,n == 32, and signed two's-complement parsing across boundary widths without panic.Verification & Policy Compliance
cargo xtask policy(text-only, <512 KiB).main(ffe310e).AI-assisted with Antigravity.
@NATON4 on GitHub · 2026-11-24 14:47 UTC · original
Opened #992 for this (one commit on
ffe310e,crates/victory-formats/src/xma.rsonly).Same idea as the fork fix above, with one difference for n > 32:
readandsignedkeep the low 32 bits (the field's value truncated to 32 bits) rather than the first 32, and consume all n bits. The new testxma::tests::bits_read_zero_full_and_oversized_widthspanics onmainand passes with the change.@DaniiL35 happy to adjust if you'd prefer your variant.
@DaniiL35 on GitHub · 2026-11-24 16:17 UTC · original
PR opened directly: #1154
@studiostephe on GitHub · 2026-11-25 03:03 UTC · original
The guard the earlier PRs (#992, #1154) proposed never landed — both closed without merging, so
read/signedon main940d32fstill underflow32 - nfor a width past 32 andsigned(0)still shifts by 32.In
crates/victory-formats/src/xma.rs:Bits::read(n)doesself.peek32() >> (32 - n). Forn > 32the shift amount underflows; forn == 32au32 >> 32is undefined.Bits::signed(n)does((self.read(n) << (32 - n)) as i32) >> (32 - n)with non == 0guard, sosigned(0)shifts a 32-bit value by 32 — a panic in debug, undefined in release.The fix clamps both to the whole word for a width of 32 or more (a 32-bit read already takes the whole word, so this matches the existing behavior at the boundary and just stops the underflow), and gives
signedthe samen == 0early returnreadhas.Verified (native, this Mac,
victory-formats):the_bitstream_clamps_zero_and_wide_bit_widthspanics on the unpatched code (attempt to shift left with overflowatsigned(0)) and passes with the fix.cargo test -p victory-formats --locked --lib: 39 passed, 0 failed, 5 ignored.cargo clippy -p victory-formats --locked: 0 warnings.Not covered: the crate's pre-existing rustfmt noise in
xma/tables.rs(and a few long lines elsewhere inxma.rs) is untouched — it is already present on cleanmainand is unrelated to this change; my added lines are fmt-clean. I could not exercise a real XMA stream that actually drives a width ≥ 32 through the decoder (none in the repo's fixtures does), so the guard is verified by the direct bit-width test rather than an end-to-end decode.Local commit
26de54bon a clean940d32f(one file, +44/−2).qwen3.8_27b running locally on an M1 Mac
shout out to Digital Debris Los Angeles
@Marvisak on GitHub · 2026-11-25 06:56 UTC · original
Automated triage for the maintainers.
Confirmed on
main(940d32f). Incrates/victory-formats/src/xma.rs,Bits::signedcomputes32 - nwith non == 0guard, sosigned(0)shifts au32by 32.Bits::readguards onlyn == 0, so a width over 32 underflows32 - n. Whether the decoder ever asks for such a width hasn't been shown: no XMA stream in the tests does. The earlier PRs #992 and #1154 were closed without merging. Open PR #2061 ("Formats: guard XMA bitstream reads and sign extension at 0 and >=32 bits", which names this issue) carries the guard, so review can continue there.Generated by Claude Code