Formats: guard XMA bitstream reads and sign extension at 0 and >=32 bits #2059

Closed
fuzeofficlal wants to merge 1 commit from upstream-pr/2061 into main
fuzeofficlal commented 2026-11-25 04:18:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T04:18:10Z.


Closes #907.

Summary

  • Bits::read(n): guard n >= 32 by advancing past the high n - 32 bits with saturating_add and returning the low 32 bits via peek32(), avoiding 32 - n underflow and shift overflow while keeping MSB-first truncation continuous with n < 32.
  • Bits::signed(n): return 0 without advancing when n == 0 (avoiding << 32 / >> 32 shift overflow) and self.read(n) as i32 when n >= 32.

Testing

  • Added xma::tests::bits_handles_zero_word_and_oversized_widths covering aligned/unaligned n = 0, 1, 16, 31, 32, 33, 36, 64, u32::MAX, two's-complement extremes, and cursor saturation.
  • cargo test -p victory-formats, cargo clippy --workspace --all-targets -- -D warnings, and cargo xtask policy.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6597105198 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2061). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T04:18:10Z. --- Closes #907. ### Summary - **`Bits::read(n)`**: guard `n >= 32` by advancing past the high `n - 32` bits with `saturating_add` and returning the low 32 bits via `peek32()`, avoiding `32 - n` underflow and shift overflow while keeping MSB-first truncation continuous with `n < 32`. - **`Bits::signed(n)`**: return `0` without advancing when `n == 0` (avoiding `<< 32` / `>> 32` shift overflow) and `self.read(n) as i32` when `n >= 32`. ### Testing - Added `xma::tests::bits_handles_zero_word_and_oversized_widths` covering aligned/unaligned `n = 0, 1, 16, 31, 32, 33, 36, 64, u32::MAX`, two's-complement extremes, and cursor saturation. - `cargo test -p victory-formats`, `cargo clippy --workspace --all-targets -- -D warnings`, and `cargo xtask policy`.
catminou4 commented 2026-11-25 05:05:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T04:34:45Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-formats --locked.

39 passed / 0 failed. XMA bitstream reads and sign extension guarded at 0 and bounds (+1 test) — XMA decoder hardening; 39 passed / 0 failed / 5 ignored.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051907684 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2061#pullrequestreview-6051907684). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T04:34:45Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-formats --locked`. 39 passed / 0 failed. XMA bitstream reads and sign extension guarded at 0 and bounds (+1 test) — XMA decoder hardening; 39 passed / 0 failed / 5 ignored.
DenRakEiw commented 2026-11-25 05:05:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T04:36:25Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-formats --lib xma:: — 14 passed, 0 failed (3 ignored), including bits_handles_zero_word_and_oversized_widths
  • cargo clippy -p victory-formats --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean bounds guards for XMA bitstream reads and sign extension at 0 and >=32 bit widths. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6051919369 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2061#pullrequestreview-6051919369). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T04:36:25Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-formats --lib xma::` — 14 passed, 0 failed (3 ignored), including `bits_handles_zero_word_and_oversized_widths` - `cargo clippy -p victory-formats --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean bounds guards for XMA bitstream reads and sign extension at 0 and >=32 bit widths. Looks good. AI-assisted (Gemini 4 Neon).
jeremygold02 commented 2026-11-25 05:06:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-25T04:30:42Z.


Triage verdict — Keep the XMA bit-reader hardening (fbb6461)

Evidence

  • @fuzeofficlal’s change carries forward @NATON4’s low-word behavior from closed #992. It consumes the requested width, returns the low 32 bits for wider fields, and now saturates cursor arithmetic. That is useful defensive coverage for @DaniiL35’s #907. Reader · Earlier patch

Checks and limits

  • The focused test passed on the exact head. A disposable extension also passed aligned and unaligned in-bounds 64-bit reads with known low words. Moving the logical end one bit earlier preserved the extracted word and set the overread flag, matching the reader’s existing separate bounds-reporting contract.
  • These are synthetic helper checks. I did not find a current decode path that requests more than 32 bits.

Next step

  • Retain the saturating cursor and zero-width controls, preserve credit for the earlier patch and report, and consider keeping the in-bounds 64-bit value case alongside the oversized-overread case.

This verification was AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152908819 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2061#issuecomment-7152908819). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-25T04:30:42Z. --- **Triage verdict — Keep the XMA bit-reader hardening** (`fbb6461`) **Evidence** - @fuzeofficlal’s change carries forward @NATON4’s low-word behavior from closed #992. It consumes the requested width, returns the low 32 bits for wider fields, and now saturates cursor arithmetic. That is useful defensive coverage for @DaniiL35’s #907. [Reader](https://github.com/Marvisak/VIctory/blob/fbb64618517f3edcedab452d897753d11d202300/crates/victory-formats/src/xma.rs#L200-L249) · [Earlier patch](https://github.com/Marvisak/VIctory/pull/992/files) **Checks and limits** - The focused test passed on the exact head. A disposable extension also passed aligned and unaligned in-bounds 64-bit reads with known low words. Moving the logical end one bit earlier preserved the extracted word and set the overread flag, matching the reader’s existing separate bounds-reporting contract. - These are synthetic helper checks. I did not find a current decode path that requests more than 32 bits. **Next step** - Retain the saturating cursor and zero-width controls, preserve credit for the earlier patch and report, and consider keeping the in-bounds 64-bit value case alongside the oversized-overread case. This verification was AI-assisted.
Marvisak commented 2026-11-25 10:07:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T09:24:41Z.


Automated triage for the maintainers.
Proposed close.

Bits::read and Bits::signed (crates/victory-formats/src/xma.rs) gain branches for widths of 32 bits or more and for signed(0). Neither case can occur in main. Every caller passes a width bounded below 32:

  • large_value: at most 23
  • the post-processing matrix in frame: channels * channels * 4, and Decoder::new clamps channels to 1..=2, so at most 16
  • read(len) with a 4-bit len: at most 15
  • the 3-bit modifier_len: at most 7
  • log2 of frame and subframe sizes: small

The only signed call is signed(6). The reviewer above also found no decode path that asks for more than 32 bits. This makes the change a guard for cases that cannot occur. The saturating cursor arithmetic is in the same category, because pos is bounded by the packet length.

This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. A real caller that can pass such a width, for example from a damaged stream, would change the verdict.

CI: passing (check).


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158292907 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2061#issuecomment-7158292907). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T09:24:41Z. --- <!-- victory-triage v1 head=fbb64618517f3edcedab452d897753d11d202300 verdict=proposed-close at=2026-11-25T09:24:34Z --> Automated triage for the maintainers. **Proposed close.** `Bits::read` and `Bits::signed` (crates/victory-formats/src/xma.rs) gain branches for widths of 32 bits or more and for `signed(0)`. Neither case can occur in main. Every caller passes a width bounded below 32: - `large_value`: at most 23 - the post-processing matrix in `frame`: `channels * channels * 4`, and `Decoder::new` clamps `channels` to 1..=2, so at most 16 - `read(len)` with a 4-bit `len`: at most 15 - the 3-bit `modifier_len`: at most 7 - `log2` of frame and subframe sizes: small The only `signed` call is `signed(6)`. The reviewer above also found no decode path that asks for more than 32 bits. This makes the change a guard for cases that cannot occur. The saturating cursor arithmetic is in the same category, because `pos` is bounded by the packet length. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. A real caller that can pass such a width, for example from a damaged stream, would change the verdict. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 11:03:39 +00:00
fuzeofficlal commented 2026-11-25 11:05:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T10:18:39Z.


Agreed with the triage analysis — all packet/frame call sites bound Bits::read widths below 32 and only pass signed(6) on main. Closing to keep the review queue focused.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7159519940 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2061#issuecomment-7159519940). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T10:18:39Z. --- Agreed with the triage analysis — all packet/frame call sites bound `Bits::read` widths below 32 and only pass `signed(6)` on `main`. Closing to keep the review queue focused.

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