From dc256b8b39ddba16241c0db2ba07a0371f2a762c Mon Sep 17 00:00:00 2001 From: Ian Swope Date: Thu, 8 Oct 2026 14:04:40 -0500 Subject: [PATCH] A PSD layer with bounds but no channel data no longer aborts on a 360 GB allocation (#755) (#885) record_surface sized the interleave buffer from the layer rectangle alone, so a few-hundred-byte file declaring a 300000x300000 layer with no channels made import allocate ~360 GB and abort. The channel decoders already bound their output by the bytes present, so a buffer is now built only when at least one channel decodes; otherwise the layer imports empty with a warning. --- crates/io/src/psd_import.rs | 6 ++++++ crates/io/tests/psd_structure.rs | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs index c7b50fd4174..846f88e04c3 100644 --- a/crates/io/src/psd_import.rs +++ b/crates/io/src/psd_import.rs @@ -133,6 +133,12 @@ impl Ctx<'_> { if ids.zip(&planes).any(|(id, p)| p.is_none() && rec.channel(id).is_some()) { return Surface::new(self.fmt); } + // Only decoded channel data justifies a buffer: the decoders bound their output by the + // bytes in the file, but a bare rectangle can declare 300000² pixels in a few bytes (#755). + if planes.iter().all(Option::is_none) { + self.warn(format!("layer \"{name}\": no channel data for its {w}x{h} bounds; treated as empty")); + return Surface::new(self.fmt); + } let refs: Vec> = planes.iter().map(|p| p.as_deref()).collect(); let mut fill: Vec> = vec![zero_sample(s); self.cc]; fill.push(max_sample(s)); diff --git a/crates/io/tests/psd_structure.rs b/crates/io/tests/psd_structure.rs index 5ec4cb680e6..5321153fc90 100644 --- a/crates/io/tests/psd_structure.rs +++ b/crates/io/tests/psd_structure.rs @@ -541,3 +541,16 @@ fn every_blend_mode_maps_both_ways() { assert_eq!(photocraft_color::BlendMode::from_psd_key(k.key()), Some(m)); } } + +#[test] +fn layer_bounds_without_channel_data_allocate_nothing() { + // A record can declare a 300000x300000 rectangle in a few bytes. With no channel data to + // back it, import used to interleave a ~360 GB buffer and abort on the allocation (#755). + use photocraft_psd::LayerRecord; + let mut f = testgen::small(Version::Psd, Compression::Raw); + f.layers_mut().push(LayerRecord { rect: photocraft_psd::Rect::from_xywh(0, 0, 300_000, 300_000), name: b"huge".to_vec(), ..Default::default() }); + let (d, warnings) = psd_to_document(&f); + let l = d.layers.last().unwrap(); + assert!(matches!(&l.content, LayerContent::Raster(s) if s.tile_count() == 0)); + assert!(warnings.iter().any(|w| w.contains("\"huge\"")), "{warnings:?}"); +}