From ccabfee3b5b9353b1f48f0bfd833f3fa3df36782 Mon Sep 17 00:00:00 2001 From: Phil Hart Date: Wed, 18 Nov 2026 15:00:30 -0500 Subject: [PATCH] craft-raw: no frame floor for tiny lossless JPEG tiles (#2232) A DNG tile's frame may hold at most twice the tile's samples and a Sony quad tile's at most the tile's samples, with no 65,536-sample floor. The floor let each of many tiny tiles decode and keep a frame far larger than the tile budget counts (storytold/lightcraft#4024). A DNG tile also drops the surplus capacity of a larger frame after the resize. Claude-Session: https://claude.ai/code/session_01Fnx9MwPUug72xHRgg993uc Co-authored-by: Claude Opus 5.5 --- crates/raw/src/arw.rs | 32 +++++++++++++++++++++++++++++--- crates/raw/src/tiffraw.rs | 21 ++++++++++++++++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/crates/raw/src/arw.rs b/crates/raw/src/arw.rs index 389c464a5..6ac30f37e 100644 --- a/crates/raw/src/arw.rs +++ b/crates/raw/src/arw.rs @@ -365,9 +365,10 @@ fn read_quad_tiles(bytes: &[u8], info: &ImageInfo, limits: &Limits) -> Result c.width as usize || f.height * 2 > c.height as usize { return Err(RawError::Corrupt(format!( "lossless ARW tile: {} samples in a {}×{}×{} frame", @@ -1707,6 +1708,31 @@ mod tests { assert_eq!(read_quad_tiles(&file, &info, &Limits::default()).unwrap(), mosaic); } + /// storytold/lightcraft#4024: 256 tiles of 2×2 all pointing at one frame of 64×64×4 samples are refused before the + /// frame is decoded (at most a tile's samples), not after each tile has decoded 16,384 samples. + #[test] + fn tiny_quad_tiles_do_not_decode_a_large_frame() { + let frame = ljpeg::encode(&vec![300u16; 64 * 64 * 4], 64, 64, 4, 14, 1, 0); + let info = ImageInfo { + width: 32, + height: 32, + bits_per_sample: vec![14], + samples_per_pixel: 1, + compression: 7, + photometric: photometric::CFA, + planar: 1, + predictor: 1, + sample_format: 1, + new_subfile_type: 0, + layout: Layout::Tiles { tile_width: 2, tile_height: 2 }, + offsets: vec![0; 256], + byte_counts: vec![frame.len() as u64; 256], + }; + // refused by the decoder's sample limit (a Limit), not by the shape check after a full decode (Corrupt) + let got = read_quad_tiles(&frame, &info, &Limits::default()); + assert!(matches!(got, Err(RawError::Limit(_))), "{got:?}"); + } + /// storytold/lightcraft#2561: a small file whose one-byte or packed 12-bit strip declares a large frame is refused before the full /// decode allocates the frame. #[test] diff --git a/crates/raw/src/tiffraw.rs b/crates/raw/src/tiffraw.rs index b7291f9d0..7dc29f5a7 100644 --- a/crates/raw/src/tiffraw.rs +++ b/crates/raw/src/tiffraw.rs @@ -344,7 +344,9 @@ fn decode_chunk( unpack_chunk(src, order, packing, cw, ch, cpp, bits, float, 1) } comp::JPEG => { - let f = ljpeg::decode(src, n.saturating_mul(2).max(1 << 16))?; + // A frame may hold at most twice the tile's samples, with no floor: a 65,536-sample floor let each of + // many tiny tiles decode (and keep) a frame far larger than the tile budget counts (storytold/lightcraft#4024) + let f = ljpeg::decode(src, n.saturating_mul(2))?; if f.data.len() < n { // Some writers encode edge tiles smaller than nominal: accept a frame that covers the rows inside the image. if f.data.is_empty() || f.data.len() % (cw * cpp) != 0 || f.data.len() / (cw * cpp) < vh { @@ -353,6 +355,8 @@ fn decode_chunk( } let mut v = f.data; v.resize(n, 0); + // a larger frame's surplus is not kept: the tile holds what the decode budget counts for it + v.shrink_to_fit(); Ok(ChunkPx::U16(v)) } #[cfg(feature = "dng")] @@ -1138,6 +1142,21 @@ mod budget_tests { } } + /// storytold/lightcraft#4024: 1,024 tiles of 2×2 all pointing at one 8 KB lossless JPEG of 128×128×4 samples. Each + /// reference brings enough bytes for both `check_image` bounds, but a tile may not decode a frame of 65,536 + /// samples: with a 65,536-sample floor on the decode limit each tile held about 128 KB that the grouping + /// counted as 8 bytes (134 MB for a 4,096-sample image; 34 GB for the same grid at 1024×1024). + #[test] + fn tiny_tiles_with_a_large_frame_are_refused() { + let tile = ljpeg::encode(&vec![300u16; 128 * 128 * 4], 128, 128, 4, 12, 1, 0); + let info = ImageInfo { bits_per_sample: vec![12], byte_counts: vec![tile.len() as u64; 1024], ..aliased(64, 64, 2, 0) }; + assert!(read_image(&tile, &info, ByteOrder::Little, Packing::Msb, &Limits::default()).is_err()); + // a frame of the tile's own size still decodes + let fits = ljpeg::encode(&[300u16; 4], 2, 2, 1, 12, 1, 0); + let info = ImageInfo { bits_per_sample: vec![12], byte_counts: vec![fits.len() as u64; 4], ..aliased(4, 4, 2, 0) }; + assert_eq!(read_image(&fits, &info, ByteOrder::Little, Packing::Msb, &Limits::default()), Ok(RawData::U16(vec![300; 4 * 4]))); + } + /// storytold/lightcraft#2554: tiled decoders decode the grid in groups of at most a quarter of the image (1 Mi samples at /// least), each group at least one chunk, covering every chunk once and in order. #[cfg(feature = "arw")]