From 58b4e7568ad94ff3217826846b62e72d7b705e50 Mon Sep 17 00:00:00 2001 From: hajduzsolt <66201672+hajduzsolt@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:17:16 +0100 Subject: [PATCH] XAudio2: bound Opus stream count before allocation Reject stream counts that cannot fit in the packet before reserving the output vector. Co-Authored-By: Codex GPT-6 --- crates/xcl/src/hle/opus.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/opus.rs b/crates/xcl/src/hle/opus.rs index ac9c01313f..dad9e8e718 100644 --- a/crates/xcl/src/hle/opus.rs +++ b/crates/xcl/src/hle/opus.rs @@ -363,7 +363,9 @@ fn split_self_delimited(data: &[u8], pos: &mut usize) -> Option> { /// Split an RFC 7845 multistream packet into `stream_count` standard single-stream packets. pub fn split_multistream_packet<'a>(packet: &'a [u8], stream_count: usize) -> Option>> { - if stream_count == 0 { + // Every self-delimited stream needs a TOC and a length byte; the last needs a TOC. + // Reject impossible counts before reserving the output vector. + if stream_count == 0 || stream_count > packet.len() / 2 + packet.len() % 2 { return None; } if stream_count == 1 { @@ -1526,6 +1528,17 @@ fn f32_to_i16(x: f32) -> i16 { #[cfg(test)] pub(crate) mod tests { use super::*; + #[test] + fn multistream_count_must_fit_the_packet_before_reserving() { + assert!(split_multistream_packet(&[], 1).is_none()); + assert!(split_multistream_packet(&[0], 0).is_none()); + assert!(split_multistream_packet(&[0], 2).is_none()); + assert!(split_multistream_packet(&[0], usize::MAX).is_none()); + let streams = split_multistream_packet(&[0, 0, 0], 2).unwrap(); + assert_eq!(streams.len(), 2); + assert_eq!(streams[0].as_ref(), &[0]); + assert_eq!(streams[1].as_ref(), &[0]); + } #[test] fn rfc6716_table2_all_32_configs() {