diff --git a/crates/io/src/annotations_map.rs b/crates/io/src/annotations_map.rs index af0add860f..6b38319a2b 100644 --- a/crates/io/src/annotations_map.rs +++ b/crates/io/src/annotations_map.rs @@ -179,8 +179,11 @@ fn write_note(n: &Note) -> Vec { b.push(u8::from(n.open)); b.push(0x1C); b.extend_from_slice(&1u16.to_be_bytes()); + // The position comes from the file or a command and can sit anywhere in i32 (issue #939): + // the cast saturates, and the icon's far edge must saturate too rather than wrap (release) or + // overflow (debug) when the note is re-saved. let (x, y) = (n.position[0].round() as i32, n.position[1].round() as i32); - for v in [y, x, y + 20, x + 16] { + for v in [y, x, y.saturating_add(20), x.saturating_add(16)] { b.extend_from_slice(&v.to_be_bytes()); } let pp = if n.popup == [0.0; 4] { [n.position[0] + 20.0, n.position[1] + 20.0, n.position[0] + 260.0, n.position[1] + 160.0] } else { n.popup }; diff --git a/crates/io/tests/annotations.rs b/crates/io/tests/annotations.rs index 38758a515c..df8c3a12ff 100644 --- a/crates/io/tests/annotations.rs +++ b/crates/io/tests/annotations.rs @@ -63,3 +63,87 @@ fn measurement_scale_roundtrips_through_psd() { assert_eq!(again.resources.iter().filter(|r| r.id == MEASUREMENT_SCALE).count(), 1); } } + +/// A PSD `Anno` block with one text note whose icon rectangle is `[top, left, 0, 0]` and whose +/// popup is all zeros (issue #939's fixture). +fn anno_with_icon(top: i32, left: i32) -> Vec { + let mut entry = Vec::new(); + entry.extend_from_slice(b"txtA"); + entry.push(1); // open + entry.push(0x1C); // flags + entry.extend_from_slice(&1u16.to_be_bytes()); // optional blocks + for v in [top, left, 0, 0] { + entry.extend_from_slice(&v.to_be_bytes()); // icon: top, left, bottom, right + } + for v in [0i32; 4] { + entry.extend_from_slice(&v.to_be_bytes()); // popup + } + entry.extend_from_slice(&0u16.to_be_bytes()); // colour space: RGB + for _ in 0..4 { + entry.extend_from_slice(&0u16.to_be_bytes()); + } + entry.extend_from_slice(&[0u8, 0]); // author: empty Pascal string + entry.extend_from_slice(&[0u8, 0]); // name + entry.extend_from_slice(&[0u8, 0]); // modification date + let text = [0xFEu8, 0xFF, 0x00, b'A']; // UTF-16BE "A" + entry.extend_from_slice(&((text.len() + 12) as u32).to_be_bytes()); + entry.extend_from_slice(b"txtC"); + entry.extend_from_slice(&(text.len() as u32).to_be_bytes()); + entry.extend_from_slice(&text); + let mut out = Vec::new(); + out.extend_from_slice(&2u16.to_be_bytes()); + out.extend_from_slice(&1u16.to_be_bytes()); + out.extend_from_slice(&1u32.to_be_bytes()); // one annotation + out.extend_from_slice(&((entry.len() + 4) as u32).to_be_bytes()); + out.extend_from_slice(&entry); + out +} + +fn psd_with_anno(anno: Vec) -> Vec { + let mut doc = Document::new("notes", Size::new(16, 16), ColorMode::Rgb, SampleType::U8); + doc.layers.push(photocraft_doc::Layer::raster("Background", doc.pixel_format())); + let mut file = PsdFile::from_bytes(&to_psd(&doc)).unwrap(); + let mut tb = photocraft_psd::TaggedBlock::new(*b"Anno", anno.clone()); + tb.padding = Some(vec![0; (4 - anno.len() % 4) % 4]); + file.global_blocks.push(tb); + file.to_bytes().unwrap() +} + +/// Icon rectangle `[top, left, bottom, right]` of the first note in the exported `Anno` block. +fn exported_icon(doc: &Document) -> [i32; 4] { + let f = PsdFile::from_bytes(&to_psd(doc)).unwrap(); + let d = &f.global_block(b"Anno").unwrap().data; + // header (2 + 2 + 4), entry length (4), `txtA` (4), open (1), flags (1), optional (2). + let mut out = [0i32; 4]; + for (i, v) in out.iter_mut().enumerate() { + let at = 20 + i * 4; + *v = i32::from_be_bytes(d[at..at + 4].try_into().unwrap()); + } + out +} + +/// Issue #939: a note whose stored icon top sits near `i32::MAX` imported fine and then +/// overflowed `y + 20` on every PSD save (a panic in debug, a wrapped coordinate in release). The +/// far edge saturates instead, so the note survives a round trip with representable geometry. +#[test] +fn a_note_icon_near_i32_max_saves_with_representable_geometry() { + for (top, left) in [(i32::MAX - 5, 0), (0, i32::MAX - 3), (i32::MIN, i32::MIN)] { + let imp = import("notes.psd", &psd_with_anno(anno_with_icon(top, left))).expect("import"); + assert_eq!(imp.document.notes[0].position, [f64::from(left), f64::from(top)]); + let icon = exported_icon(&imp.document); + assert_eq!(icon, [top, left, top.saturating_add(20), left.saturating_add(16)], "{top},{left}"); + let back = import("notes.psd", &to_psd(&imp.document)).unwrap().document; + assert_eq!((back.notes[0].text.as_str(), back.notes[0].position), ("A", [f64::from(left), f64::from(top)])); + } + // The same through the model: a position a command could set beyond i32 saturates, NaN is 0. + let mut doc = Document::new("notes", Size::new(16, 16), ColorMode::Rgb, SampleType::U8); + doc.notes.push(Note { text: "far".into(), position: [1e300, f64::INFINITY], ..Default::default() }); + assert_eq!(exported_icon(&doc), [i32::MAX, i32::MAX, i32::MAX, i32::MAX]); + doc.notes[0].position = [f64::NAN, -1e300]; + assert_eq!(exported_icon(&doc), [i32::MIN, 0, i32::MIN + 20, 16]); + // Control: an ordinary note is unchanged. + doc.notes[0].position = [42.0, 28.0]; + assert_eq!(exported_icon(&doc), [28, 42, 48, 58]); + let back = import("x.psd", &to_psd(&doc)).unwrap().document; + assert_eq!(back.notes[0].position, [42.0, 28.0]); +}