address review: GIF frame allocation limit, lower wasm32 still limit, no EXR fallback after a size-limit error

This commit is contained in:
Dylan M. Taylor
2026-11-11 00:45:43 +00:00
parent 90e5dc16d3
commit c8b24a7528
3 changed files with 24 additions and 2 deletions
+17 -1
View File
@@ -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();
+2
View File
@@ -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}")))?,
};
+5 -1
View File
@@ -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;