Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Merge PR #6041: Raw: charge the retained tile intermediates together (#4959)

David Mannb1e4c9b1f924
diff --cc crates/raw/src/sensor.rs
index cab5ce2a0,b62b953d8..a7c512a21
--- a/crates/raw/src/sensor.rs
+++ b/crates/raw/src/sensor.rs
@@@ -255,19 -255,7 +255,12 @@@ pub(crate) fn read_plane(t: &Tiff, ifd
}
limits.check(width as u64, height as u64, samples as u64 * 2)?;
let segments = segments(t, ifd, width, height)?;
+ // The decoded tiles are all retained in `decoded` before assembly copies only the useful
+ // pixels. Each tile is charged individually below, but file-controlled padding and repeated
+ // source ranges can make the *cumulative* retained samples far exceed both the input and the
+ // useful image. Reject that up front: the sum of padded tile samples (two bytes each) must
- // fit the allocation budget.
- let mut total_tile_samples: u64 = 0;
- for s in &segments {
- let tile = (s.w as u64).saturating_mul(s.h as u64).saturating_mul(samples as u64);
- total_tile_samples = total_tile_samples.saturating_add(tile);
- }
- if total_tile_samples.saturating_mul(2) > limits.max_alloc {
- return Err(RawError::LimitExceeded(format!("{} bytes of retained raw tiles exceed {}", total_tile_samples.saturating_mul(2), limits.max_alloc)));
- }
++ // fit the allocation budget (checked per segment, #4959).
+ retained_segment_bytes(&segments, samples, limits)?;
let le = t.le;
let tiled = t.tag_uint(ifd, tag::TILE_WIDTH).is_some();
let decoded: Vec<Result<Vec<u16>>> = par::map(segments.len(), |i| {