Never crash: PSD reader bounds overflow and input-sized allocations
Layer bounds come straight from the file as four i32s. Rect::width/height subtracted them in i32 (overflow: a panic in debug builds, garbage sizes in release), and layer_pixels allocated width x height pixels before reading any data, so a 100-byte file claiming a 2^31-square layer hit "capacity overflow" (or an out-of-memory abort). Widths are now computed in i64 and clamped, every decoded image is capped at 2^28 pixels (Error::Unsupported beyond that), zlib data is inflated with a limit of the expected size, RLE count tables and UTF-16 strings reserve no more than the remaining bytes, and the per-pixel copy only visits the part of the layer that lands in the output. Denies the panic lints in the crate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
10644a83aa
commit
dc573f77e6
@@ -218,3 +218,37 @@ fn smart_objects_and_embedded_files() {
|
||||
assert_eq!((e.width, e.height), (20, 10));
|
||||
assert!(p.layers[0].smart_object.is_none());
|
||||
}
|
||||
|
||||
/// Replace the first layer record's bounds (`top, left, bottom, right`) in a written document.
|
||||
fn patch_layer_rect(bytes: &mut [u8], from: Rect, to: Rect) {
|
||||
let enc = |r: Rect| [r.top, r.left, r.bottom, r.right].iter().flat_map(|v| v.to_be_bytes()).collect::<Vec<u8>>();
|
||||
let (old, new) = (enc(from), enc(to));
|
||||
let at = bytes.windows(16).position(|w| w == old.as_slice()).expect("layer rect in the written file");
|
||||
bytes[at..at + 16].copy_from_slice(&new);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extreme_layer_bounds_error_instead_of_overflowing() {
|
||||
// A layer spanning the whole i32 range: its width overflowed i32 subtraction.
|
||||
let r = Rect::new(2, 3, 4, 5);
|
||||
let mut d = WDoc::new(8, 8);
|
||||
d.layers = vec![WLayer::solid("L", r, [1.0, 0.0, 0.0, 1.0])];
|
||||
let mut bytes = write(&d);
|
||||
patch_layer_rect(&mut bytes, r, Rect { top: i32::MIN, left: i32::MIN, bottom: i32::MAX, right: i32::MAX });
|
||||
let p = Psd::parse(bytes).unwrap();
|
||||
assert_eq!(p.layers[0].rect.width(), u32::MAX);
|
||||
assert!(p.layer_pixels(0, false).is_err());
|
||||
assert!(p.layer_pixels(0, true).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn huge_layer_bounds_error_instead_of_allocating() {
|
||||
// A few bytes claiming a 2-billion-pixel-square layer must not try to allocate it.
|
||||
let r = Rect::new(2, 3, 4, 5);
|
||||
let mut d = WDoc::new(8, 8);
|
||||
d.layers = vec![WLayer::solid("L", r, [1.0, 0.0, 0.0, 1.0])];
|
||||
let mut bytes = write(&d);
|
||||
patch_layer_rect(&mut bytes, r, Rect { top: 0, left: 0, bottom: i32::MAX, right: i32::MAX });
|
||||
let p = Psd::parse(bytes).unwrap();
|
||||
assert!(matches!(p.layer_pixels(0, false), Err(Error::Unsupported(_))));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user