Merge pull request #1832 from storytold/fix/exr-display-block-bound

fix(exr): bound the display decoder by its largest block, not the whole part
This commit is contained in:
FredApps
2026-11-16 09:42:09 +01:00
committed by GitHub
2 changed files with 36 additions and 11 deletions
+13 -8
View File
@@ -515,18 +515,23 @@ 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::<f32>()))
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)),
// 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)));
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()))?;
+23 -3
View File
@@ -129,10 +129,30 @@ 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);
// 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));
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]