diff --git a/crates/ec-vp9-syntax/src/superframe.rs b/crates/ec-vp9-syntax/src/superframe.rs index d5a6c13d..bf5bac0e 100644 --- a/crates/ec-vp9-syntax/src/superframe.rs +++ b/crates/ec-vp9-syntax/src/superframe.rs @@ -52,9 +52,14 @@ pub fn split(chunk: &[u8]) -> Result> { "VP9 superframe: frame {i} of {frames} runs past the chunk ({size} bytes at {offset})" )) })?; - out.push(&chunk[offset..end]); + if end > offset { + out.push(&chunk[offset..end]); + } offset = end; } + if out.is_empty() { + return Err(Error::corrupt("VP9 superframe: index names no frame data")); + } Ok(out) } @@ -102,6 +107,13 @@ mod tests { assert_eq!(frames[2][0], 2); } + #[test] + fn zero_size_entry_is_skipped() { + let chunk = pack(&[3, 0, 5], 1); + let frames = split(&chunk).unwrap(); + assert_eq!(frames.iter().map(|f| f.len()).collect::>(), [3, 5]); + } + #[test] fn oversized_entry_is_corrupt() { let mut chunk = pack(&[3, 5], 1); diff --git a/crates/ec-vp9/src/decode.rs b/crates/ec-vp9/src/decode.rs index 3080c2d8..292358a5 100644 --- a/crates/ec-vp9/src/decode.rs +++ b/crates/ec-vp9/src/decode.rs @@ -90,6 +90,22 @@ struct Planes { /// before any picture or mode-info buffer is sized from them (fuzz regression: a 24580-wide key frame). const MAX_LUMA_SAMPLES: u64 = 8192 * 4352; +fn validate(hdr: &FrameHeader) -> Result<()> { + if hdr.bit_depth != 8 && hdr.bit_depth != 10 && hdr.bit_depth != 12 { + return Err(Error::unsupported( + format!("vp9 bit depth {}", hdr.bit_depth), + "this lane decodes 8-, 10- and 12-bit only", + )); + } + if u64::from(hdr.width) * u64::from(hdr.height) > MAX_LUMA_SAMPLES { + return Err(Error::unsupported( + format!("vp9 frame {}x{}", hdr.width, hdr.height), + "this lane decodes up to VP9 level 6.2 (8192x4352 luma samples)", + )); + } + Ok(()) +} + impl Planes { /// A frame scratch at the 64-multiple ALIGNED extent, filled with the /// neutral grey `1 << (bd - 1)` (`128` at 8-bit, `512` at 10-bit). The @@ -310,18 +326,7 @@ impl Decoder { None => Err(corrupt("show_existing_frame names an empty slot")), }; } - if hdr.bit_depth != 8 && hdr.bit_depth != 10 && hdr.bit_depth != 12 { - return Err(Error::unsupported( - format!("vp9 bit depth {}", hdr.bit_depth), - "this lane decodes 8-, 10- and 12-bit only", - )); - } - if u64::from(hdr.width) * u64::from(hdr.height) > MAX_LUMA_SAMPLES { - return Err(Error::unsupported( - format!("vp9 frame {}x{}", hdr.width, hdr.height), - "this lane decodes up to VP9 level 6.2 (8192x4352 luma samples)", - )); - } + validate(&hdr)?; // One index per decoded frame (the dumps' `frame=` field), so this // path and `decode_syntax` label the same frame the same way. let index = self.decode_index; @@ -474,18 +479,7 @@ impl Decoder { if hdr.show_existing_frame { continue; } - if hdr.bit_depth != 8 && hdr.bit_depth != 10 && hdr.bit_depth != 12 { - return Err(Error::unsupported( - format!("vp9 bit depth {}", hdr.bit_depth), - "this lane decodes 8-, 10- and 12-bit only", - )); - } - if u64::from(hdr.width) * u64::from(hdr.height) > MAX_LUMA_SAMPLES { - return Err(Error::unsupported( - format!("vp9 frame {}x{}", hdr.width, hdr.height), - "this lane decodes up to VP9 level 6.2 (8192x4352 luma samples)", - )); - } + validate(&hdr)?; if hdr.frame_type == FrameType::Key { // Propagate: a corrupt keyframe leaves a half-updated frame // context, prev-frame MV field and reference slots behind, so