From 159d7134d0c7cb45903864172122263353310401 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:13:19 +0100 Subject: [PATCH] fix(image-codecs): read the JPEGs zune-jpeg panics on with craft-jpeg zune-jpeg 0.5 asserts in its upsampler on some subsampled four-component frames whose first component isn't the largest. decode now catches that panic and reads the file with craft-jpeg, as it reads arithmetic-coded and 12-bit files. Refs #778. --- crates/image-codecs/src/codecs/jpeg.rs | 51 ++++++++++++++---------- crates/image-codecs/tests/it/png_jpeg.rs | 35 ++++++++++++++++ 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index 15346ea56..7ec85e562 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -389,6 +389,30 @@ fn decode_craft_jpeg(bytes: &[u8], limits: &Limits, adobe_transform: Option, /// truncated or not, are far above this. const MAX_PIXELS_PER_BYTE: u64 = 2048; +/// Decode with craft-jpeg: the files [`uses_craft_jpeg`] routes away from `zune-jpeg`, and the +/// ones `zune-jpeg` panics on. +fn decode_whole(bytes: &[u8], limits: &Limits, meta: JpegMeta, end: DataEnd, scan_bytes: u64) -> Result { + // craft-jpeg sizes its buffers from the frame header before it reads a scan. + if scan_bytes == 0 { + return Err(err("the file has no scan data")); + } + if let Some((w, h, _)) = meta.frame + && end == DataEnd::Truncated + // Only truncated scans: arithmetic coding (SOF9–SOF11) can spend well under a bit on + // a flat block, so a complete scan may be shorter than this bound. + && u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes + { + return Err(err("the truncated scan is too short for the frame it declares")); + } + let (mut img, damaged) = decode_craft_jpeg(bytes, limits, meta.adobe_transform, meta.adobe_marker)?; + img.icc = meta.icc; + img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, photoshop_resources: meta.photoshop, ..Default::default() }; + if end == DataEnd::Truncated || damaged { + img.warnings.push(DecodeWarning::Truncated { format: F }); + } + Ok(img) +} + pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { let (end, scan_bytes) = data_end(bytes); if end == DataEnd::Empty { @@ -399,25 +423,7 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result limits.check_bytes(w, h, u64::from(nc.max(1)))?; } if uses_craft_jpeg(bytes) { - // craft-jpeg sizes its buffers from the frame header before it reads a scan. - if scan_bytes == 0 { - return Err(err("the file has no scan data")); - } - if let Some((w, h, _)) = meta.frame - && end == DataEnd::Truncated - // Only truncated scans: arithmetic coding (SOF9–SOF11) can spend well under a bit on - // a flat block, so a complete scan may be shorter than this bound. - && u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes - { - return Err(err("the truncated scan is too short for the frame it declares")); - } - let (mut img, damaged) = decode_craft_jpeg(bytes, limits, meta.adobe_transform, meta.adobe_marker)?; - img.icc = meta.icc; - img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, photoshop_resources: meta.photoshop, ..Default::default() }; - if end == DataEnd::Truncated || damaged { - img.warnings.push(DecodeWarning::Truncated { format: F }); - } - return Ok(img); + return decode_whole(bytes, limits, meta, end, scan_bytes); } let options = DecoderOptions::default().set_strict_mode(false).set_max_width(65535).set_max_height(65535); let stripped = without_extended_xmp(bytes); @@ -443,7 +449,12 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result return Err(err(format!("{what} is too short for the frame it declares"))); } dec.set_options(dec.options().jpeg_set_out_colorspace(out_cs)); - let mut px = dec.decode().map_err(err)?; + // zune-jpeg 0.5 asserts on some subsampled four-component frames whose first component isn't + // the largest (its upsampler's scratch row is sized from that component): craft-jpeg reads them. + let Ok(decoded) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| dec.decode())) else { + return decode_whole(bytes, limits, meta, end, scan_bytes); + }; + let mut px = decoded.map_err(err)?; let expected = w as usize * h as usize * layout.channels(); if px.len() < expected { return Err(err("short pixel buffer")); diff --git a/crates/image-codecs/tests/it/png_jpeg.rs b/crates/image-codecs/tests/it/png_jpeg.rs index 07c57bf24..98a134121 100644 --- a/crates/image-codecs/tests/it/png_jpeg.rs +++ b/crates/image-codecs/tests/it/png_jpeg.rs @@ -759,3 +759,38 @@ fn bounded_png_stream_preserves_late_exif_across_metadata_yields() { assert_eq!(extras.orientation, 6); assert_eq!(extras.meta.text.len(), 150); } + +/// zune-jpeg 0.5 asserts on some subsampled CMYK frames whose first component isn't the largest +/// (jpeg-encoder writes C, M and Y at 1×1 and K larger); craft-jpeg reads those instead, and they +/// decode to the inks they were written with. The others of these files read as before. +#[test] +fn cmyk_frames_zune_jpeg_panics_on_decode() { + use jpeg_encoder::{ColorType, Encoder, SamplingFactor as S}; + let mut seen = 0; + for (s, progressive) in + [(S::F_2_2, true), (S::R_4_2_0, true), (S::F_4_1, false), (S::F_4_2, false), (S::F_2_4, false), (S::R_4_1_1, false), (S::R_4_1_0, false)] + { + for (w, h) in [(1u16, 1u16), (9, 1), (1, 9), (17, 13), (40, 40)] { + let inks: Vec = (0..usize::from(w) * usize::from(h)).flat_map(|_| [30u8, 90, 150, 210]).collect(); + let mut out = Vec::new(); + let mut e = Encoder::new(&mut out, 100); + e.set_sampling_factor(s); + e.set_progressive(progressive); + e.encode(&inks, w, h, ColorType::Cmyk).unwrap(); + let zune = std::panic::catch_unwind(|| { + let mut d = zune_jpeg::JpegDecoder::new(zune_core::bytestream::ZCursor::new(&out[..])); + d.decode().is_ok() + }); + if zune.is_ok() { + // Read or refused by zune-jpeg as before; no panic either way. + let _ = decode(&out); + continue; + } + seen += 1; + let img = decode(&out).unwrap_or_else(|e| panic!("{s:?} {w}x{h}: {e}")); + assert_eq!((img.dimensions(), img.layout()), ((u32::from(w), u32::from(h)), ChannelLayout::Cmyk)); + assert!(img.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 3), "{s:?} progressive {progressive} {w}x{h}"); + } + } + assert!(seen >= 10, "zune-jpeg panicked on {seen} of them"); +}