From c8b24a7528a6e58fd013723ed93626724a3e67f6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 00:45:43 +0000 Subject: [PATCH] address review: GIF frame allocation limit, lower wasm32 still limit, no EXR fallback after a size-limit error --- crates/media/src/gif_anim.rs | 18 +++++++++++++++++- crates/media/src/pool.rs | 2 ++ crates/media/src/probe.rs | 6 +++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/media/src/gif_anim.rs b/crates/media/src/gif_anim.rs index fe0489f4a..d201c8b7e 100644 --- a/crates/media/src/gif_anim.rs +++ b/crates/media/src/gif_anim.rs @@ -93,7 +93,12 @@ impl Anim { if crate::webp_anim::is_webp(bytes) { return crate::webp_anim::load(bytes); } - let dec = GifDecoder::new(Cursor::new(bytes)).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?; + let mut dec = GifDecoder::new(Cursor::new(bytes)).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?; + // A frame may be far larger than the canvas, and the decoder allocates its buffer before + // decoding it: no single buffer may pass what all the frames may hold. + let mut limits = image::Limits::no_limits(); + limits.max_alloc = Some(MAX_BYTES as u64); + image::ImageDecoder::set_limits(&mut dec, limits).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?; // The decoder allocates the full logical canvas for the first frame, however small the // frames themselves are: check it before decoding any. let (w, h) = image::ImageDecoder::dimensions(&dec); @@ -185,6 +190,17 @@ mod tests { assert_eq!(colour_at(&pool, &f, 5.0), [0.0, 0.0, 1.0]); } + #[test] + fn a_frame_far_larger_than_the_canvas_is_refused_before_it_is_allocated() { + // A 1×1 canvas and a 20000×20000 frame (1.6 GB of RGBA); the data itself is not there. + let mut bytes = b"GIF89a".to_vec(); + // (a two-entry global colour table, so the frame gets as far as its buffer) + bytes.extend([1, 0, 1, 0, 0x80, 0, 0, 0, 0, 0, 255, 255, 255]); + bytes.extend([0x2c, 0, 0, 0, 0, 0x20, 0x4e, 0x20, 0x4e, 0, 2, 0]); + let e = crate::gif_anim::Anim::load(&bytes).err().expect("refused").to_string(); + assert!(e.to_lowercase().contains("memory"), "{e}"); + } + #[test] fn a_zero_delay_is_ten_hundredths_like_in_browsers() { let f = probe_bytes("a.gif", gif(&[(RED, 0), (GREEN, 1)]).into()).unwrap(); diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index 655ca4ed8..00d5fa29a 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -859,6 +859,8 @@ impl Inner { let loaded = crate::probe::load_oriented(&bytes, crate::still_format(path, &bytes)); let img = match loaded { Ok(img) => img, + // Over the size limit: the layered fallback would allocate its planes anyway. + Err(e @ image::ImageError::Limits(_)) => return Err(MediaError::Decode(format!("{path}: {e}"))), // A multi-layer OpenEXR file without an unnamed RGB layer: its colour layer. Err(e) => crate::exr_channels::cached_layered_image(path, bytes).ok_or_else(|| MediaError::Decode(format!("{path}: {e}")))?, }; diff --git a/crates/media/src/probe.rs b/crates/media/src/probe.rs index 566af1621..baa00d861 100644 --- a/crates/media/src/probe.rs +++ b/crates/media/src/probe.rs @@ -10,8 +10,12 @@ use image::metadata::Orientation; use crate::{MediaError, Result, ext_of, rate_from_fc}; /// Most pixels a still may decode to: 2 GiB as the renderer's float RGBA (16384 × 8192, beyond -/// any camera's still). +/// any camera's still). A browser tab's wasm32 heap can't hold one allocation that large (and +/// the decoded input beside it): 512 MiB there. +#[cfg(not(target_arch = "wasm32"))] pub(crate) const MAX_STILL_PIXELS: u64 = 1 << 27; +#[cfg(target_arch = "wasm32")] +pub(crate) const MAX_STILL_PIXELS: u64 = 1 << 25; /// Frame rate given to image sequences on import (After Effects' default preference). pub const DEFAULT_SEQUENCE_RATE: FrameRate = FrameRate::FPS_30;