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.
This commit is contained in:
Ian Swope authored and GitHub committed 2026-10-08 15:04:40 -04:00
1 parent 5049a1d062
commit dc256b8b39
2 files changed
+19

No files matched your search

+6
View File
@@ -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));
+13
View File
@@ -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:?}");
}