mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 16:24:38 +00:00
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.
This commit is contained in:
3 files changed
+27
-8
No files matched your search
@@ -344,6 +344,16 @@ pub fn open<S: ByteSource + ?Sized>(src: &S) -> Result<VmdFile> {
|
||||
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<Vec<u8>> {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user