Skip to content

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

PSD: a zero-length annotation entry no longer hangs the import (#4149) (#4117)

David Mannb1b3dff8f12e
diff --git a/crates/io/src/annotations_map.rs b/crates/io/src/annotations_map.rs
index 28ab181c8..27b6c0447 100644
--- a/crates/io/src/annotations_map.rs
+++ b/crates/io/src/annotations_map.rs
@@ -133,6 +133,11 @@ fn parse_entries(data: &[u8]) -> Option<Vec<Entry>> {
for _ in 0..count {
let start = r.p;
let len = r.u32()? as usize;
+ // The length counts itself and the type: anything shorter would put the cursor back on
+ // this entry, and a hostile count would then loop until memory runs out.
+ if len < 8 {
+ return None;
+ }
let end = start.checked_add(len)?;
let raw = data.get(start..end)?;
let kind = r.take(4)?;
@@ -422,6 +427,22 @@ mod tests {
assert!(!anno_unreadable(&d));
}
+ /// An entry length below 8 (its own `u32` plus the type) put the cursor back on the same
+ /// entry, so a count of 0xFFFFFFFF looped ~4.3e9 times and ran out of memory.
+ #[test]
+ fn short_entry_length_is_unreadable_not_a_loop() {
+ for len in [0u8, 4, 7] {
+ for kind in [b"sndA", b"txtA"] {
+ let mut b = vec![0, 2, 0, 1, 0, 0, 0, 3, 0, 0, 0, len];
+ b.extend_from_slice(kind);
+ b.extend_from_slice(&[0; 64]);
+ assert!(parse_entries(&b).is_none(), "len {len} {kind:?}");
+ }
+ }
+ let hostile = [0, 2, 0, 1, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, b's', b'n', b'd', b'A'];
+ assert!(parse_anno(&hostile).is_none());
+ }
+
#[test]
fn scale_resource_roundtrip_and_keep_rules() {
let s = MeasurementScale { pixel_length: 150.0, logical_length: 2.0, units: "mm".into() };