From 15aaced60ab820832e7dce75a630790fcb1b0b6b Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 07:47:17 +0100 Subject: [PATCH 1/2] fix(exr): bound the display decoder by its largest block, not the whole part decode_display_part refused a part whose data window across all channels exceeded max_bytes, even when the kept planes fit. The EXR crate decompresses one block at a time, so bound the largest all-channel block (a tile within the data window, or one scan-line group across it) instead. Large multi-layer files whose picture planes fit now decode; the selected-plane and file capacity checks are unchanged. --- crates/exr/src/decode.rs | 20 ++++++++++++-------- crates/exr/src/decode_tests.rs | 22 +++++++++++++++++++--- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/crates/exr/src/decode.rs b/crates/exr/src/decode.rs index ab21064ce..7daf7a448 100644 --- a/crates/exr/src/decode.rs +++ b/crates/exr/src/decode.rs @@ -515,18 +515,22 @@ impl<'s> ReadChannels<'s> for DisplaySelect<'_> { u32s: vec![], }); } - // The EXR crate decompresses each accepted block before `read_block` can discard ignored - // channels. Bound that all-channel input separately from the selected output planes. - let Some(block_bytes) = layer_w - .checked_mul(layer_h) - .and_then(|pixels| pixels.checked_mul(header.channels.list.len().max(1))) - .and_then(|samples| samples.checked_mul(std::mem::size_of::())) - else { + // The EXR crate decompresses each accepted block, with every channel, before `read_block` + // can discard the ignored ones, so bound that all-channel block separately from the + // selected output planes. Blocks are decompressed one at a time, so the bound is the + // largest block (a tile within the data window, or a scan-line group across it), not the + // whole part: a part whose full channel set is larger than `max_bytes` still decodes when + // the kept planes and each block fit. + let block_pixels = match header.blocks { + exr::meta::BlockDescription::Tiles(tiles) => tiles.tile_size.0.min(layer_w).checked_mul(tiles.tile_size.1.min(layer_h)), + exr::meta::BlockDescription::ScanLines => header.compression.scan_lines_per_block().checked_mul(layer_w), + }; + let Some(block_bytes) = block_pixels.and_then(|pixels| pixels.checked_mul(header.channels.bytes_per_pixel.max(1))) else { self.block_limit.set(Some(format!("data-window block size exceeds {}", self.limits.max_bytes))); return Err(exr::error::Error::Invalid("data-window block size overflow".into())); }; if u64::try_from(block_bytes).map_or(true, |bytes| bytes > self.limits.max_bytes) || block_bytes > isize::MAX as usize { - self.block_limit.set(Some(format!("{block_bytes} data-window bytes exceed {}", self.limits.max_bytes))); + self.block_limit.set(Some(format!("{block_bytes} bytes in one data-window block exceed {}", self.limits.max_bytes))); return Err(exr::error::Error::Invalid("data-window blocks exceed limit".into())); } let pixels = display_w.checked_mul(display_h).filter(|n| *n > 0).ok_or(exr::error::Error::Invalid("display window".into()))?; diff --git a/crates/exr/src/decode_tests.rs b/crates/exr/src/decode_tests.rs index 54c28d9aa..e82e42a34 100644 --- a/crates/exr/src/decode_tests.rs +++ b/crates/exr/src/decode_tests.rs @@ -129,10 +129,26 @@ fn decoded_display_part_places_selected_channels_and_checks_total_budget() { let limits = Limits { max_bytes: 6 * 4 - 1, ..Limits::default() }; assert!(matches!(decode_display_part(&bytes, 0, &all, &|_| 0.0, &limits), Err(Error::LimitExceeded(_)))); - // One selected display plane fits exactly, but the EXR decoder must decompress both data - // channels in its input block before discarding the ignored one. + // One selected display plane fits exactly. Both data channels together (2x2x2 f32, 32 bytes) + // don't, but they are decompressed one scan-line block (2 pixels x 2 channels) at a time. let block_limits = Limits { max_bytes: 6 * 4, ..Limits::default() }; - assert!(matches!(decode_display_part(&bytes, 0, &|name| name == "R", &|_| 0.0, &block_limits), Err(Error::LimitExceeded(_)))); + let part = decode_display_part(&bytes, 0, &|name| name == "R", &|_| 9.0, &block_limits).unwrap(); + assert_eq!(part.channels[0].samples, [0.0, 1.0, 9.0, 2.0, 3.0, 9.0]); +} + +#[test] +fn decoded_display_part_bounds_each_all_channel_block() { + // The kept R plane is 4 pixels of f32 (16 bytes), but each scan-line block carries all four + // channels (64 bytes) before the ignored ones are discarded. + let bytes = file(vec![layer(None, 4, 1, &["R", "G", "B", "A"], false, exr::prelude::Compression::ZIP1)], (4, 1)); + let r = |name: &str| name == "R"; + assert!(matches!(decode_display_part(&bytes, 0, &r, &|_| 0.0, &Limits { max_bytes: 16, ..Limits::default() }), Err(Error::LimitExceeded(_)))); + assert_eq!(decode_display_part(&bytes, 0, &r, &|_| 0.0, &Limits { max_bytes: 64, ..Limits::default() }).unwrap().channels.len(), 1); + // A tile is the block of a tiled part: a 4x4 tile of four f32 channels is 256 bytes, while + // the whole 8x8 part is 1024 and the kept R plane of the 2x2 display window only 16. + let tiled = file(vec![layer(None, 8, 8, &["R", "G", "B", "A"], true, exr::prelude::Compression::ZIP1)], (2, 2)); + assert!(matches!(decode_display_part(&tiled, 0, &r, &|_| 0.0, &Limits { max_bytes: 255, ..Limits::default() }), Err(Error::LimitExceeded(_)))); + assert_eq!(decode_display_part(&tiled, 0, &r, &|_| 0.0, &Limits { max_bytes: 256, ..Limits::default() }).unwrap().channels[0].samples.len(), 4); } #[test] From 33f35d74f8e8c99b7038c2a2e7535be37998e41c Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:38:50 +0100 Subject: [PATCH 2/2] exr: clamp the scan-line block bound to the part height A part shorter than its compression's scan-line group (a one-row ZIP16 part) only decodes its own rows, so the all-channel block bound is min(scan_lines_per_block, layer_h) rows. --- crates/exr/src/decode.rs | 3 ++- crates/exr/src/decode_tests.rs | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/exr/src/decode.rs b/crates/exr/src/decode.rs index 7daf7a448..d8cb32827 100644 --- a/crates/exr/src/decode.rs +++ b/crates/exr/src/decode.rs @@ -523,7 +523,8 @@ impl<'s> ReadChannels<'s> for DisplaySelect<'_> { // the kept planes and each block fit. let block_pixels = match header.blocks { exr::meta::BlockDescription::Tiles(tiles) => tiles.tile_size.0.min(layer_w).checked_mul(tiles.tile_size.1.min(layer_h)), - exr::meta::BlockDescription::ScanLines => header.compression.scan_lines_per_block().checked_mul(layer_w), + // a part shorter than its compression's scan-line group has only its own rows + exr::meta::BlockDescription::ScanLines => header.compression.scan_lines_per_block().min(layer_h).checked_mul(layer_w), }; let Some(block_bytes) = block_pixels.and_then(|pixels| pixels.checked_mul(header.channels.bytes_per_pixel.max(1))) else { self.block_limit.set(Some(format!("data-window block size exceeds {}", self.limits.max_bytes))); diff --git a/crates/exr/src/decode_tests.rs b/crates/exr/src/decode_tests.rs index e82e42a34..66a64cfb4 100644 --- a/crates/exr/src/decode_tests.rs +++ b/crates/exr/src/decode_tests.rs @@ -144,6 +144,10 @@ fn decoded_display_part_bounds_each_all_channel_block() { let r = |name: &str| name == "R"; assert!(matches!(decode_display_part(&bytes, 0, &r, &|_| 0.0, &Limits { max_bytes: 16, ..Limits::default() }), Err(Error::LimitExceeded(_)))); assert_eq!(decode_display_part(&bytes, 0, &r, &|_| 0.0, &Limits { max_bytes: 64, ..Limits::default() }).unwrap().channels.len(), 1); + // ZIP groups 16 scan lines, but a one-row part's block is that row: still 64 bytes, not 1024 + let zip16 = file(vec![layer(None, 4, 1, &["R", "G", "B", "A"], false, exr::prelude::Compression::ZIP16)], (4, 1)); + assert!(matches!(decode_display_part(&zip16, 0, &r, &|_| 0.0, &Limits { max_bytes: 63, ..Limits::default() }), Err(Error::LimitExceeded(_)))); + assert_eq!(decode_display_part(&zip16, 0, &r, &|_| 0.0, &Limits { max_bytes: 64, ..Limits::default() }).unwrap().channels.len(), 1); // A tile is the block of a tiled part: a 4x4 tile of four f32 channels is 256 bytes, while // the whole 8x8 part is 1024 and the kept R plane of the 2x2 display window only 16. let tiled = file(vec![layer(None, 8, 8, &["R", "G", "B", "A"], true, exr::prelude::Compression::ZIP1)], (2, 2));