Skip to content

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

A PSD layer with bounds but no channel data no longer aborts on a 360 GB allocation (#755) (#885)

Ian Swopedc256b8b39dd
diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs
index c7b50fd41..846f88e04 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<Option<&[u8]>> = planes.iter().map(|p| p.as_deref()).collect();
let mut fill: Vec<Vec<u8>> = 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 5ec4cb680..5321153fc 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:?}");
+}