From 005ad49872fba7b7234e0be26690f10ae1edcd82 Mon Sep 17 00:00:00 2001 From: 9hvn9yt2vk-netizen <360448330+9hvn9yt2vk-netizen@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:01:06 +0100 Subject: [PATCH] xma: stop at a zero-length frame instead of looping forever next_frame walks the frames before `start` to learn whether the frame at `start` is the packet's last. A damaged packet whose earlier frame has a length of 0 never advanced `at`, so the walk spun forever (in a debug build it overflowed `number` after about 4 billion rounds and panicked). Apply the rule read_frame already uses: a length that does not cover its own 15-bit field is not a frame, and the search ends. Found with a random-input harness. The new test hangs without the fix and passes at once with it. --- crates/victory-formats/src/xma.rs | 33 ++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/victory-formats/src/xma.rs b/crates/victory-formats/src/xma.rs index 06aa427..4a9e606 100644 --- a/crates/victory-formats/src/xma.rs +++ b/crates/victory-formats/src/xma.rs @@ -136,7 +136,14 @@ fn next_frame(data: &[u8], start: usize, end: usize) -> Option { let mut number = 0; while at < start { let mut cursor = Cursor { data, pos: at }; - at += cursor.bits(LENGTH_BITS)? as usize; + let bits = cursor.bits(LENGTH_BITS)? as usize; + // The same rule as `read_frame`: a length that does not cover its own + // field is not a frame. A zero length would never move `at`, so a + // damaged packet would loop here for good. + if bits <= LENGTH_BITS as usize { + return None; + } + at += bits; number += 1; } let last = number + 1 >= header.frames; @@ -1015,6 +1022,30 @@ fn to_i16(v: f32) -> i16 { mod tests { use super::*; + /// Writes `value` as `n` bits, MSB first, at bit `pos`. + fn put_bits(data: &mut [u8], pos: usize, n: u32, value: u32) { + for i in 0..n as usize { + if value >> (n as usize - 1 - i) & 1 == 1 { + data[(pos + i) / 8] |= 1 << (7 - (pos + i) % 8); + } + } + } + + /// A damaged packet whose first frame says it is 0 bits long: finding + /// the frame after a later one must give up, not walk in place forever + /// (found by random-input testing). + #[test] + fn a_zero_length_frame_before_the_start_ends_the_search() { + let mut data = vec![0u8; PACKET_BYTES]; + // Two frames, the first at payload bit 0; its 15-bit length stays 0. + put_bits(&mut data, 0, 32, 2 << 26); + let start = HEADER_BITS + 100; + put_bits(&mut data, start, LENGTH_BITS, 200); + let frame = read_frame(&data, start).expect("the frame at `start` is well formed"); + assert_eq!(frame.bits, 200); + assert_eq!(frame.next, None); + } + /// The direct sum `imdct_half` stands for (FFmpeg's naive inverse MDCT: /// the standard formula, negated). fn imdct_reference(input: &[f32]) -> Vec {