From 66d84247271dc6cb51c9c167b2872f5c096135f5 Mon Sep 17 00:00:00 2001 From: alehel Date: Sun, 15 Nov 2026 08:12:43 +0100 Subject: [PATCH] test(media,video): VMD frame buffer that cannot be had, palette path in the real-file test - media: the frame buffer comes from a helper that returns `None` for a size the address space cannot hold; a test asks it for sizes past `isize::MAX`, where `vec!` panicked. - video: the real-file comparison takes the palette of a frame without a rectangle, as the documented loop does, instead of failing on it. --- crates/media/src/vmd/mod.rs | 17 +++++++++++------ crates/media/src/vmd/tests.rs | 9 +++++++++ crates/video/tests/vmd/main.rs | 9 +++++++-- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/crates/media/src/vmd/mod.rs b/crates/media/src/vmd/mod.rs index 7c8f19f4d..48418e047 100644 --- a/crates/media/src/vmd/mod.rs +++ b/crates/media/src/vmd/mod.rs @@ -344,6 +344,16 @@ pub fn open(src: &S) -> Result { Ok(VmdFile { header, frames, raw_header: raw.to_vec(), origins }) } +/// `size` zero bytes, or `None` if they cannot be had. `vec![0; size]` panics with a capacity +/// overflow for a size the address space cannot hold, which a frame of over 2 GiB is on a 32-bit +/// target. +fn zeroed(size: usize) -> Option> { + let mut bytes = Vec::new(); + bytes.try_reserve_exact(size).ok()?; + bytes.resize(size, 0); + Some(bytes) +} + impl VmdFile { /// The 816 header bytes as stored: the configuration (`CodecParams::extradata`) of the VMD /// video and sound decoders. @@ -359,12 +369,7 @@ impl VmdFile { if f.offset.checked_add(u64::from(f.size)).is_none_or(|end| end > src.len()) { return Err(Error::Invalid(format!("frame {index} reaches past the end of the file"))); } - // fallibly: a size the address space cannot hold (above 2 GiB on a 32-bit target) is an - // error, not a capacity-overflow panic - let size = f.size as usize; - let mut data = Vec::new(); - data.try_reserve_exact(size).map_err(|_| Error::Invalid(format!("frame {index} is too large to load")))?; - data.resize(size, 0); + let mut data = zeroed(f.size as usize).ok_or_else(|| Error::Invalid(format!("frame {index} is too large to load")))?; src.read_at(f.offset, &mut data)?; Ok(data) } diff --git a/crates/media/src/vmd/tests.rs b/crates/media/src/vmd/tests.rs index 302de93ad..aec96c409 100644 --- a/crates/media/src/vmd/tests.rs +++ b/crates/media/src/vmd/tests.rs @@ -177,6 +177,15 @@ fn frame_past_the_end_is_an_error() { assert!(matches!(f.read_frame(&d, 0), Err(Error::Invalid(_)))); } +/// `vec![0; size]` panics for a size past `isize::MAX`, which a frame of over 2 GiB is on a +/// 32-bit target; the frame buffer must not. +#[test] +fn a_buffer_that_cannot_be_had_is_not_a_panic() { + assert_eq!(zeroed(usize::MAX), None); + assert_eq!(zeroed(isize::MAX as usize + 1), None); + assert_eq!(zeroed(3), Some(vec![0; 3])); +} + #[test] fn hostile_input_never_panics() { for (i, seed) in [sample(true), sample(false)].iter().enumerate() { diff --git a/crates/video/tests/vmd/main.rs b/crates/video/tests/vmd/main.rs index be35e5138..33ee065ec 100644 --- a/crates/video/tests/vmd/main.rs +++ b/crates/video/tests/vmd/main.rs @@ -473,9 +473,14 @@ fn real_files_match_ffmpeg() { if frame.size == 0 { continue; } - let rect = file.picture_rect(i).unwrap_or_else(|| panic!("{name}: frame {i} lies outside the picture")); let data = file.read_frame(&source, i).unwrap(); - decoder.decode(rect.into(), new_palette, &data).unwrap_or_else(|e| panic!("{name}: frame {i}: {e}")); + // as the documented loop does: a frame without a rectangle may still bring a palette + match file.picture_rect(i) { + Some(rect) => decoder.decode(rect.into(), new_palette, &data), + None if new_palette => decoder.decode_palette(&data), + None => panic!("{name}: frame {i} lies outside the picture"), + } + .unwrap_or_else(|e| panic!("{name}: frame {i}: {e}")); theirs_out.read_exact(&mut theirs).unwrap_or_else(|e| panic!("{name}: ffmpeg has no picture for frame {i}: {e}")); let ours = decoder.rgb24(); if theirs != ours {