Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

fix(media): validate HDR scanlines before frame allocation (#2957)

Earl Cof6bd7a724faf
diff --git a/crates/media/src/still/hdr.rs b/crates/media/src/still/hdr.rs
index 4e0765bc3..12ff4e72d 100644
--- a/crates/media/src/still/hdr.rs
+++ b/crates/media/src/still/hdr.rs
@@ -197,8 +197,17 @@ pub fn decode(name: &str, b: &[u8]) -> Result<VideoFrame> {
if (b.len().saturating_sub(pos) as u64).saturating_mul(4096) < (w as u64).saturating_mul(h as u64) {
return Err(err(name, "truncated pixel data"));
}
- let mut px = vec![1f32; w * h * 4];
+ // Validate every encoded scanline before allocating the complete float image. A short HDR
+ // header can otherwise request up to a GiB of zero-initialized pixels and only then fail on
+ // its first missing or malformed scanline.
+ let pixel_data = pos;
let mut scan = vec![[0u8; 4]; n2];
+ for _ in 0..n1 {
+ scanline(name, b, &mut pos, &mut scan)?;
+ }
+
+ let mut px = vec![1f32; w * h * 4];
+ pos = pixel_data;
for i in 0..n1 {
scanline(name, b, &mut pos, &mut scan)?;
for (j, &p) in scan.iter().enumerate() {
diff --git a/crates/media/src/still/hdr/tests.rs b/crates/media/src/still/hdr/tests.rs
index 3e0b5564a..612b660bc 100644
--- a/crates/media/src/still/hdr/tests.rs
+++ b/crates/media/src/still/hdr/tests.rs
@@ -112,6 +112,9 @@ fn hostile_input_is_an_error_not_a_panic() {
}
assert!(decode("a.hdr", &file("-Y 2 +X 2", &[0; 8])).is_err());
assert!(decode("a.hdr", &file("-Y 99999999 +X 99999999", &[0; 16])).is_err());
+ // Pass the coarse compressed-size check but fail scanline validation. This must be rejected
+ // before a 1 GiB float image is initialized.
+ assert!(decode("a.hdr", &file("-Y 67108864 +X 1", &[0; 16_384])).is_err());
assert!(decode("a.hdr", &file("-Y 0 +X 4", &[])).is_err());
assert!(decode("a.hdr", &file("-Y 2 -Y 2", &[0; 16])).is_err());
assert!(decode("a.hdr", &file("junk", &[0; 16])).is_err());