formats: guard bitstream signed and 32-bit shift overflows in xma #1154

Closed
github-bot wants to merge 1 commit from gh-pr/1154/head into gh-pr/1154/base
Owner

@⁠DaniiL35 on GitHub · 2026-11-24 16:16 UTC · original
fix/xma-bits-shift-overflow → main · +38 −0 · 1 files · 1 commits · closed without merging

Resolves #907

Problem

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

  • Bits::signed(n) computed let sign = 1u32 << (n - 1); without checking if n == 0 (underflow to n - 1 == u32::MAX, causing 1u32 << 4294967295 overflow panic) or n > 32. Furthermore, val - (1 << n) overflows if n == 32 and val is non-negative.
  • Bits::read(n) evaluated let mask = (1u32 << n) - 1; without checking for n >= 32. When n == 32, Rust panics in debug builds on 1u32 << 32.

Fix

  1. Handle n == 0 explicitly in Bits::signed returning 0.
  2. Guard n >= 32 with sign-extension logic (val as i32) and clamp 1u64 << n without shift overflow.
  3. In Bits::read, handle n == 0 (mask 0) and n >= 32 (mask u32::MAX).
  4. Added synthetic unit test bitstream_handles_zero_and_boundary_bit_widths verifying zero width, 1-bit, 31-bit, 32-bit, and signed conversions.

All fixtures are synthetic; zero game data (Rule #1 compliant). Formatted with Rust 2024 edition.

**@⁠DaniiL35** on GitHub · 2026-11-24 16:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1154) `fix/xma-bits-shift-overflow` → `main` · +38 −0 · 1 files · 1 commits · closed without merging Resolves #907 ### Problem In `crates/victory-formats/src/xma.rs`: - `Bits::signed(n)` computed `let sign = 1u32 << (n - 1);` without checking if `n == 0` (underflow to `n - 1 == u32::MAX`, causing `1u32 << 4294967295` overflow panic) or `n > 32`. Furthermore, `val - (1 << n)` overflows if `n == 32` and `val` is non-negative. - `Bits::read(n)` evaluated `let mask = (1u32 << n) - 1;` without checking for `n >= 32`. When `n == 32`, Rust panics in debug builds on `1u32 << 32`. ### Fix 1. Handle `n == 0` explicitly in `Bits::signed` returning `0`. 2. Guard `n >= 32` with sign-extension logic (`val as i32`) and clamp `1u64 << n` without shift overflow. 3. In `Bits::read`, handle `n == 0` (mask `0`) and `n >= 32` (mask `u32::MAX`). 4. Added synthetic unit test `bitstream_handles_zero_and_boundary_bit_widths` verifying zero width, 1-bit, 31-bit, 32-bit, and signed conversions. All fixtures are synthetic; zero game data (Rule #1 compliant). Formatted with Rust 2024 edition.
In crates/victory-formats/src/xma.rs, Bits::signed(n) computed
((read(n) << (32 - n)) as i32) >> (32 - n) without checking for n == 0
or n >= 32. For n == 0, 32 - n is 32, which causes an overflow panic on
32-bit shift in Rust. Similarly, Bits::read(n) for n >= 32 caused
subtraction underflow on 32 - n.

Add guards for n == 0 and n >= 32 in both read() and signed(), and add
a synthetic unit test covering boundary bit widths (0, 32, and signed
two'\''s-complement parsing).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:21:27 +00:00
github-bot closed this pull request 2026-11-26 04:21:27 +00:00

Pull request closed

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!1154
No description provided.