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

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

@⁠fuzeofficlal on GitHub · 2026-11-25 04:18 UTC · original
fuzeofficlal:fix/formats-xma-bitstream-bounds-907 → main · +94 −2 · 1 files · 1 commits · closed without merging

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-25 04:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2061) `fuzeofficlal:fix/formats-xma-bitstream-bounds-907` → `main` · +94 −2 · 1 files · 1 commits · closed without merging 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`.
Avoid shift and subtraction overflow in Bits::read and Bits::signed when
the requested width is 0 or at least 32 bits, while preserving low-32-bit
truncation and saturating bit-cursor advancement.

Closes #907.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:53:30 +00:00
github-bot closed this pull request 2026-11-26 04:53:30 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-25 04:30 UTC · original

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.

**@⁠jeremygold02** on GitHub · 2026-11-25 04:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/2061#issuecomment-7152908819) **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://git.gtahub.st/github/Marvisak--VIctory/pulls/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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 09:24 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 09:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/2061#issuecomment-7158292907) <!-- 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)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 10:18 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 10:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2061#issuecomment-7159519940) 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

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