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

Closed
DaniiL35 wants to merge 1 commit from upstream-pr/1154 into main
DaniiL35 commented 2026-11-24 16:16:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaniiL35 on 2026-11-24T16:16:54Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6586291128 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1154). Originally posted by [DaniiL35](https://github.com/DaniiL35) on 2026-11-24T16:16:54Z. --- 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.
upstream-sync closed this pull request 2026-11-24 17:03:32 +00:00

Pull request closed

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