formats/xma: handle zero-frame continuation/empty packets in next_frame #627

Open
github-bot wants to merge 1 commit from github-bot/gh-627 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-25 03:26 UTC · original
AscilCH:fix/formats-xma-next-frame-zero-frame-packets → main · +80 −5 · 1 files · 1 commits · open

Problem

Two bugs in xma::next_frame (crates/victory-formats/src/xma.rs):

  1. Cross-packet frame ending inside a frames == 0 continuation packet: if a frame started in packet index and finished inside a subsequent packet end_pkt whose header has frames == 0 (tail bits followed by 0xFF padding), neither end.is_multiple_of(PACKET_BITS) nor end / PACKET_BITS == index held. next_frame returned Some(end) inside 0xFF padding, causing the next read_frame to see length 0x7fff and silently truncate the rest of the audio stream.
  2. Advancing into a frames == 0 packet (first_frame_bit == 0x7fff): in XMA2, packets starting no frames set first_frame_bit = 0x7fff (32767 > PACKET_BITS - HEADER_BITS). next_frame unconditionally computed next * PACKET_BITS + HEADER_BITS + header.first_frame_bit, jumping ~2 packets forward right into another packet's header and bypassing that packet's skip count.

Fix

  • Trigger packet advancement when end / PACKET_BITS != index && packet(data, end_pkt)?.frames == 0 in addition to packet-boundary and intra-packet last-frame cases.
  • Follow the skip chain across any frames == 0 packets until finding a packet with frames > 0, checking HEADER_BITS + first_frame_bit < PACKET_BITS (and on the starting packet as well) with checked arithmetic.
  • Add synthetic unit test next_frame_handles_zero_frame_continuation_and_empty_packets covering continuation packets, empty packets with skip, and invalid first_frame_bit.

Verification

  • cargo test -p victory-formats --locked xma: 6 passed, 0 failed, 2 ignored (external dumps)
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-formats --all-targets --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-25 03:26 UTC · [original](https://github.com/fxceless/victory-fork/pull/627) `AscilCH:fix/formats-xma-next-frame-zero-frame-packets` → `main` · +80 −5 · 1 files · 1 commits · open ## Problem Two bugs in `xma::next_frame` (`crates/victory-formats/src/xma.rs`): 1. **Cross-packet frame ending inside a `frames == 0` continuation packet**: if a frame started in packet `index` and finished inside a subsequent packet `end_pkt` whose header has `frames == 0` (tail bits followed by `0xFF` padding), neither `end.is_multiple_of(PACKET_BITS)` nor `end / PACKET_BITS == index` held. `next_frame` returned `Some(end)` inside `0xFF` padding, causing the next `read_frame` to see length `0x7fff` and silently truncate the rest of the audio stream. 2. **Advancing into a `frames == 0` packet (`first_frame_bit == 0x7fff`)**: in XMA2, packets starting no frames set `first_frame_bit = 0x7fff` (`32767 > PACKET_BITS - HEADER_BITS`). `next_frame` unconditionally computed `next * PACKET_BITS + HEADER_BITS + header.first_frame_bit`, jumping ~2 packets forward right into another packet's header and bypassing that packet's `skip` count. ## Fix - Trigger packet advancement when `end / PACKET_BITS != index && packet(data, end_pkt)?.frames == 0` in addition to packet-boundary and intra-packet last-frame cases. - Follow the `skip` chain across any `frames == 0` packets until finding a packet with `frames > 0`, checking `HEADER_BITS + first_frame_bit < PACKET_BITS` (and on the starting packet as well) with checked arithmetic. - Add synthetic unit test `next_frame_handles_zero_frame_continuation_and_empty_packets` covering continuation packets, empty packets with `skip`, and invalid `first_frame_bit`. ## Verification - `cargo test -p victory-formats --locked xma`: 6 passed, 0 failed, 2 ignored (external dumps) - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-formats --all-targets --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
Two bugs in `xma::next_frame`:
1. When a cross-packet frame finished inside a `frames == 0` continuation packet
   (followed by 0xFF padding), `next_frame` left `end` pointing into the padding
   instead of advancing to the next packet, causing the next `read_frame` to
   read length `0x7fff` and silently truncate the stream.
2. When advancing to the next packet encountered a `frames == 0` packet (whose
   XMA2 `first_frame_bit` is `0x7fff`), `next_frame` added `0x7fff` directly to
   the bit position, jumping ~2 packets forward into an unrelated packet header
   and bypassing `skip`.

Advance across `frames == 0` packets along the `skip` chain until finding a
packet with `frames > 0`, validate `HEADER_BITS + first_frame_bit < PACKET_BITS`,
and add synthetic unit tests covering all three cases.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:47:42 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/627/head:github-bot/gh-627'
git switch 'github-bot/gh-627'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-627'
git switch 'github-bot/gh-627'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-627'
git switch 'github-bot/gh-627'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-627'
git switch 'main'
git merge --squash 'github-bot/gh-627'
git switch 'main'
git merge --ff-only 'github-bot/gh-627'
git switch 'main'
git merge 'github-bot/gh-627'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.