From 4a70763629661a3fa29fa40a7ac4ec1a11c4393d Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:39:34 +0100 Subject: [PATCH] fix(image-codecs): refuse JPEG inks of frames too large for their scan data zune-jpeg allocates its whole output before reading a scan, so decode_jpeg_inks now applies decode's scan-data guard (one byte for every 2048 pixels, counted on scan data, not APP padding) first. The comparison test's noise is computed in u64, so it doesn't overflow on 32-bit targets. --- crates/image-codecs/src/codecs/jpeg.rs | 9 +++++++++ crates/image-codecs/src/jpeg_inks.rs | 7 ++++++- crates/image-codecs/tests/it/jpeg_inks.rs | 6 +++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index 15346ea56..f25cca4c7 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -198,6 +198,15 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) { data_end_mode(b, false) } +/// Whether JPEG `b` holds too little scan data for a `w` × `h` frame to be worth decoding: no scan +/// data at all, or under one byte for every [`MAX_PIXELS_PER_BYTE`] pixels (counted on the scan +/// data the marker walk crosses, so APP padding doesn't count). `zune-jpeg` allocates the whole +/// output before it reads a scan, so a few bytes could otherwise ask for a frame of any size. +pub(crate) fn scan_too_short(b: &[u8], w: u32, h: u32) -> bool { + let (end, scan_bytes) = data_end(b); + end == DataEnd::Empty || u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes +} + /// Whether the marker walk actually reaches the first EOI, with malformed segment lengths refused. pub(crate) fn reaches_eoi(b: &[u8]) -> bool { data_end_mode(b, true).0 == DataEnd::Complete diff --git a/crates/image-codecs/src/jpeg_inks.rs b/crates/image-codecs/src/jpeg_inks.rs index 6cf6e453d..226703643 100644 --- a/crates/image-codecs/src/jpeg_inks.rs +++ b/crates/image-codecs/src/jpeg_inks.rs @@ -57,7 +57,9 @@ pub fn jpeg_icc_profile(bytes: &[u8]) -> Option> { /// /// Errors: bytes that aren't a JPEG with readable headers ([`CodecError::InvalidImage`]); one of /// other than four components, or a frame `zune-jpeg` doesn't decode, such as an arithmetic-coded -/// or 12-bit one ([`CodecError::Unsupported`]); a size past `limits`; and data that doesn't decode, +/// or 12-bit one ([`CodecError::Unsupported`]); a size past `limits`; scan data too short for the +/// frame (under a byte for every 2048 pixels, refused before anything is allocated); and data that +/// doesn't decode, /// including frames `zune-jpeg` panics on, which are caught. pub fn decode_jpeg_inks(bytes: &[u8], limits: &Limits) -> Result { let bad = |m: &str| CodecError::InvalidImage(format!("JPEG inks: {m}")); @@ -70,6 +72,9 @@ pub fn decode_jpeg_inks(bytes: &[u8], limits: &Limits) -> Result = (0..n * 4).map(|k| ((k * 2_654_435_761 + i * 97) >> 7) as u8).collect(); + let noise: Vec = (0..n * 4).map(|k| ((k as u64).wrapping_mul(2_654_435_761).wrapping_add(i as u64 * 97) >> 7) as u8).collect(); let ramp: Vec = (0..n * 4).map(|k| (k / 4 % wu * 255 / wu.max(2)) as u8).collect(); for samples in [&noise, &ramp] { for ty in [ColorType::Cmyk, ColorType::CmykAsYcck] {