From 1a24520535786bfc5fe2189b7891822a04e56099 Mon Sep 17 00:00:00 2001 From: Carlos Fernandez Date: Mon, 23 Nov 2026 11:59:18 -0800 Subject: [PATCH] Skip corrupt H.264 data instead of exiting Feeding data that isn't H.264 to the AVC parser (for example a non-H.264 file concatenated after an H.264 one) made ccextractor panic or exit: - slice_header() indexed SLICE_TYPES with an unchecked slice_type (panic: "index out of bounds: the len is 10 but the index is 32"). The C slice_header() rejects values >= 10; the Rust port lost that check. - read_exp_golomb_unsigned() counted leading zeros without a limit, so a run of zero bytes asked read_bits() for hundreds of bits, which is fatal. - seq_parameter_set_rbsp() accepted any log2_max_frame_num_minus4 and log2_max_pic_order_cnt_lsb_minus4. slice_header() later uses them as bit counts, with the same fatal result. Skip such slices as the C code does, return an error for exp-Golomb codes with 64 or more leading zeros, and reject an SPS whose log2 fields exceed the H.264 limit of 12, keeping the previous one. do_nal errors are already logged and the NAL skipped. --- src/rust/lib_ccxr/src/common/bitstream.rs | 31 ++++++++++++++++- src/rust/src/avc/nal.rs | 42 +++++++++++++++++++---- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/rust/lib_ccxr/src/common/bitstream.rs b/src/rust/lib_ccxr/src/common/bitstream.rs index 7b3d22be6..eacdbb22f 100644 --- a/src/rust/lib_ccxr/src/common/bitstream.rs +++ b/src/rust/lib_ccxr/src/common/bitstream.rs @@ -13,6 +13,8 @@ pub enum BitstreamError { TooManyBits(u32), #[error("Data is insufficient for operation")] InsufficientData, + #[error("Value out of range for {0}: {1}")] + OutOfRange(&'static str, u64), } pub struct BitStreamRust<'a> { @@ -287,9 +289,14 @@ impl<'a> BitStreamRust<'a> { pub fn read_exp_golomb_unsigned(&mut self) -> Result { let mut zeros = 0; - // Count leading zeros + // Count leading zeros. A code with 64 or more is corrupt data (e.g. a + // run of zero bytes), and read_bits() can't read that many bits: + // report an error the caller can skip instead of exiting. while self.read_bits(1)? == 0 && self.bits_left >= 0 { zeros += 1; + if zeros >= 64 { + return Err(BitstreamError::TooManyBits(zeros)); + } } // Read the remaining bits @@ -474,6 +481,28 @@ mod tests { assert!(bs.is_ok()); } + #[test] + fn test_read_exp_golomb_unsigned_valid() { + // 1 -> 0, 010 -> 1, 011 -> 2, 00100 -> 3 + let data = vec![0b10100110, 0b01000000]; + let mut bs = BitStreamRust::new(&data).unwrap(); + + assert_eq!(bs.read_exp_golomb_unsigned().unwrap(), 0); + assert_eq!(bs.read_exp_golomb_unsigned().unwrap(), 1); + assert_eq!(bs.read_exp_golomb_unsigned().unwrap(), 2); + assert_eq!(bs.read_exp_golomb_unsigned().unwrap(), 3); + } + + #[test] + fn test_read_exp_golomb_unsigned_too_many_zeros() { + // Corrupt data: a run of zero bytes is not a valid code. It must be + // an error the caller can skip, not a fatal exit. + let data = vec![0u8; 64]; + let mut bs = BitStreamRust::new(&data).unwrap(); + + assert!(bs.read_exp_golomb_unsigned().is_err()); + } + #[test] fn test_read_bits() { let data = vec![0b10101010]; diff --git a/src/rust/src/avc/nal.rs b/src/rust/src/avc/nal.rs index 767a9da0a..02d1bacdb 100644 --- a/src/rust/src/avc/nal.rs +++ b/src/rust/src/avc/nal.rs @@ -11,6 +11,10 @@ use lib_ccxr::util::log::DebugMessageFlag; use lib_ccxr::{debug, info}; use std::os::raw::c_char; +/// Largest log2_max_frame_num_minus4 and log2_max_pic_order_cnt_lsb_minus4 +/// allowed by H.264 7.4.2.1.1. +const MAX_LOG2_MINUS4: u64 = 12; + /// Process sequence parameter set RBSP pub fn seq_parameter_set_rbsp( ctx: &mut AvcContextRust, @@ -129,17 +133,32 @@ pub fn seq_parameter_set_rbsp( } } - ctx.log2_max_frame_num = q1.read_exp_golomb_unsigned()? as i32; - debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "log2_max_frame_num4_minus4= {:4} ({:#X})", ctx.log2_max_frame_num, ctx.log2_max_frame_num); - ctx.log2_max_frame_num += 4; // 4 is added due to the formula. + let log2_max_frame_num_minus4 = q1.read_exp_golomb_unsigned()?; + debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "log2_max_frame_num4_minus4= {:4} ({:#X})", log2_max_frame_num_minus4, log2_max_frame_num_minus4); + // slice_header() reads this many bits: reject a corrupt SPS rather than + // keep a bit count no read can satisfy. + if log2_max_frame_num_minus4 > MAX_LOG2_MINUS4 { + return Err(BitstreamError::OutOfRange( + "log2_max_frame_num_minus4", + log2_max_frame_num_minus4, + )); + } + ctx.log2_max_frame_num = log2_max_frame_num_minus4 as i32 + 4; // 4 is added due to the formula. ctx.pic_order_cnt_type = q1.read_exp_golomb_unsigned()? as i32; debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "pic_order_cnt_type= {:4} ({:#X})", ctx.pic_order_cnt_type, ctx.pic_order_cnt_type); if ctx.pic_order_cnt_type == 0 { - ctx.log2_max_pic_order_cnt_lsb = q1.read_exp_golomb_unsigned()? as i32; - debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "log2_max_pic_order_cnt_lsb_minus4= {:4} ({:#X})", ctx.log2_max_pic_order_cnt_lsb, ctx.log2_max_pic_order_cnt_lsb); - ctx.log2_max_pic_order_cnt_lsb += 4; // 4 is added due to formula. + let log2_max_pic_order_cnt_lsb_minus4 = q1.read_exp_golomb_unsigned()?; + debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "log2_max_pic_order_cnt_lsb_minus4= {:4} ({:#X})", log2_max_pic_order_cnt_lsb_minus4, log2_max_pic_order_cnt_lsb_minus4); + if log2_max_pic_order_cnt_lsb_minus4 > MAX_LOG2_MINUS4 { + return Err(BitstreamError::OutOfRange( + "log2_max_pic_order_cnt_lsb_minus4", + log2_max_pic_order_cnt_lsb_minus4, + )); + } + ctx.log2_max_pic_order_cnt_lsb = log2_max_pic_order_cnt_lsb_minus4 as i32 + 4; + // 4 is added due to formula. } else if ctx.pic_order_cnt_type == 1 { // CFS: Untested, just copied from specs. let tmp = q1.read_bits(1)?; @@ -373,6 +392,17 @@ pub unsafe fn slice_header( let slice_type: i64 = q1.read_exp_golomb_unsigned()? as i64; debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "slice_type= {:4X}", slice_type); + // Valid slice_type values are 0-9 (H.264 Table 7-6), the indexes of + // SLICE_TYPES. A corrupt slice can carry anything: skip it, as the C + // slice_header() does, instead of indexing past SLICE_TYPES. + if slice_type as usize >= SLICE_TYPES.len() { + info!( + "Invalid slice_type {} in slice header, skipping.", + slice_type + ); + return Ok(()); + } + tmp = q1.read_exp_golomb_unsigned()? as i64; debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "pic_parameter_set_id= {:4} ({:#X})", tmp, tmp);