From 525dea58f03cee1b1022ec6997b8f8fb5e37aff2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 22:40:02 -0800 Subject: [PATCH] Native file: a distorted image with a malformed saved corner opens placed by its transform A native file whose `image_quad` held an entry that was not a point among its first four failed to open: `image_corners` read each entry as a `Point` and returned the error. Such corners, and an `image_quad` that is not a list, are now dropped as the file is read, and the image opens placed by its transform, as a list of other than four points already did. A point is `[x, y]` or `{"x": x, "y": y}`; values that are not taken are skipped without being kept in memory. Follows #8392. --- crates/doc/src/image_quad.rs | 44 ++++++++ crates/doc/src/node/wire.rs | 195 ++++++++++++++++++++++++++++++----- docs/mcp.md | 4 +- 3 files changed, 218 insertions(+), 25 deletions(-) diff --git a/crates/doc/src/image_quad.rs b/crates/doc/src/image_quad.rs index e07fd47760..3b32607a15 100644 --- a/crates/doc/src/image_quad.rs +++ b/crates/doc/src/image_quad.rs @@ -474,6 +474,50 @@ mod tests { assert_eq!(read.image_quad(), Some(q)); } + /// An entry among the first four that is not a point, or a field that is not a list, drops the + /// corners and leaves the rest of the image read; the entries after a bad one are skipped. + #[test] + fn malformed_corners_a_file_holds_are_dropped_as_it_is_read() { + let n = image(100, 100); + let plain: Node = serde_json::from_value(serde_json::to_value(&n).unwrap()).unwrap(); + let bad_entries = [ + serde_json::json!([[40.0, 0.0], "not a point", [100.0, 100.0], [0.0, 100.0]]), + serde_json::json!([[40.0, 0.0], [60.0, 0.0], [100.0, 100.0], {"x": 1}]), + serde_json::json!([null, [60.0, 0.0], [100.0, 100.0], [0.0, 100.0]]), + serde_json::json!([[40.0, 0.0], [60.0], [100.0, 100.0], [0.0, 100.0], [5.0, 5.0]]), + serde_json::json!([[40.0, 0.0], [[1.0, 2.0], 3.0]]), + serde_json::json!([[40.0, 0.0], [60.0, 0.0, 1.0], [100.0, 100.0], [0.0, 100.0]]), + serde_json::json!([[40.0, 0.0], {"x": 60.0, "y": "0"}, [100.0, 100.0], [0.0, 100.0]]), + ]; + let bad_fields = [serde_json::json!("corners"), serde_json::json!(4), serde_json::json!(true), serde_json::json!({"a": [1.0, 2.0]})]; + for quad in bad_entries.into_iter().chain(bad_fields) { + let mut v = serde_json::to_value(&n).unwrap(); + v["image_quad"] = quad.clone(); + // File text, so the fields after the corners are read from the same stream. + let text = serde_json::to_string(&v).unwrap(); + let read: Node = serde_json::from_str(&text).unwrap_or_else(|e| panic!("{quad}: {e}")); + assert!(read.extras().image_quad.is_none(), "{quad}"); + assert!(!read.is_distorted_image(), "{quad}"); + assert_eq!(read, plain, "{quad}"); + assert_eq!(read.geometric_bounds(), Some(Rect::new(0.0, 0.0, 100.0, 100.0)), "{quad}"); + } + // A point with a repeated coordinate. + let mut v = serde_json::to_value(&n).unwrap(); + v["image_quad"] = serde_json::json!([[40.0, 0.0], "SECOND", [100.0, 100.0], [0.0, 100.0]]); + let text = serde_json::to_string(&v).unwrap().replace("\"SECOND\"", r#"{"x":60.0,"y":0.0,"y":1.0}"#); + let read: Node = serde_json::from_str(&text).unwrap(); + assert_eq!(read, plain); + // Both forms of a point, whole numbers and unknown fields read as corners, from text and + // from a value. + let mut v = serde_json::to_value(&n).unwrap(); + v["image_quad"] = serde_json::json!([[40, 0], {"y": 0.0, "x": 60.0, "z": [1, 2]}, [100.0, 100.0], {"x": 0, "y": 100}]); + let q = pts([(40.0, 0.0), (60.0, 0.0), (100.0, 100.0), (0.0, 100.0)]); + let read: Node = serde_json::from_str(&serde_json::to_string(&v).unwrap()).unwrap(); + assert_eq!(read.image_quad(), Some(q)); + let read: Node = serde_json::from_value(v).unwrap(); + assert_eq!(read.image_quad(), Some(q)); + } + #[test] fn refused_corners_change_nothing_and_a_bad_quad_in_a_file_is_ignored() { let mut n = image(10, 10); diff --git a/crates/doc/src/node/wire.rs b/crates/doc/src/node/wire.rs index 9027c80b09..60a3c75d79 100644 --- a/crates/doc/src/node/wire.rs +++ b/crates/doc/src/node/wire.rs @@ -264,39 +264,188 @@ pub(super) struct NodeWire { } /// A distorted image's corners as a file holds them: a list of exactly four points, else none (the -/// image is then placed by its transform). At most five points of a longer list are kept while it -/// is read. +/// image is then placed by its transform). A point is `[x, y]` or `{"x": x, "y": y}`. A value that +/// is not a list, or a list with an entry that is not a point, is none too. Values that are not +/// taken are skipped as they are read, without being kept in memory, and at most four entries of a +/// longer list are decoded. fn image_corners<'de, D: serde::Deserializer<'de>>(d: D) -> Result>, D::Error> { + use serde::de::{Deserializer, Error, IgnoredAny, MapAccess, SeqAccess, Visitor}; + use std::fmt::{Formatter, Result as FmtResult}; use vectorcraft_geom::Point; + + /// Skips the rest of a list; true when it held another entry. + fn rest_of_list<'de, A: SeqAccess<'de>>(mut seq: A) -> Result { + let mut more = false; + while seq.next_element::()?.is_some() { + more = true; + } + Ok(more) + } + fn rest_of_map<'de, A: MapAccess<'de>>(mut map: A) -> Result<(), A::Error> { + while map.next_entry::()?.is_some() {} + Ok(()) + } + // The visitors below give none for a value of a kind they do not take. + macro_rules! none_for { + (scalars) => { + fn visit_bool(self, _: bool) -> Result { + Ok(None) + } + fn visit_str(self, _: &str) -> Result { + Ok(None) + } + fn visit_unit(self) -> Result { + Ok(None) + } + fn visit_none(self) -> Result { + Ok(None) + } + }; + (numbers) => { + fn visit_i64(self, _: i64) -> Result { + Ok(None) + } + fn visit_u64(self, _: u64) -> Result { + Ok(None) + } + fn visit_f64(self, _: f64) -> Result { + Ok(None) + } + }; + (maps $de:lifetime) => { + fn visit_map>(self, map: A) -> Result { + rest_of_map(map)?; + Ok(None) + } + }; + } + + /// A coordinate: a number, else none. + struct Coord(Option); + impl<'de> serde::Deserialize<'de> for Coord { + fn deserialize>(d: D) -> Result { + struct V; + impl<'de> Visitor<'de> for V { + type Value = Option; + fn expecting(&self, f: &mut Formatter) -> FmtResult { + f.write_str("a number") + } + fn visit_i64(self, v: i64) -> Result { + Ok(Some(v as f64)) + } + fn visit_u64(self, v: u64) -> Result { + Ok(Some(v as f64)) + } + fn visit_f64(self, v: f64) -> Result { + Ok(Some(v)) + } + fn visit_seq>(self, seq: A) -> Result { + rest_of_list(seq)?; + Ok(None) + } + none_for!(scalars); + none_for!(maps 'de); + } + d.deserialize_any(V).map(Coord) + } + } + + /// The name of a point's field. + enum Key { + X, + Y, + Other, + } + impl<'de> serde::Deserialize<'de> for Key { + fn deserialize>(d: D) -> Result { + struct V; + impl Visitor<'_> for V { + type Value = Key; + fn expecting(&self, f: &mut Formatter) -> FmtResult { + f.write_str("a field name") + } + fn visit_str(self, v: &str) -> Result { + Ok(match v { + "x" => Key::X, + "y" => Key::Y, + _ => Key::Other, + }) + } + } + d.deserialize_identifier(V) + } + } + + /// One entry of the list: a point, else none. + struct Corner(Option); + impl<'de> serde::Deserialize<'de> for Corner { + fn deserialize>(d: D) -> Result { + struct V; + impl<'de> Visitor<'de> for V { + type Value = Option; + fn expecting(&self, f: &mut Formatter) -> FmtResult { + f.write_str("a point") + } + fn visit_seq>(self, mut seq: A) -> Result { + let x = seq.next_element::()?.and_then(|c| c.0); + let y = match x { + Some(_) => seq.next_element::()?.and_then(|c| c.0), + None => None, + }; + let more = rest_of_list(seq)?; + Ok(x.zip(y).filter(|_| !more).map(|(x, y)| Point::new(x, y))) + } + fn visit_map>(self, mut map: A) -> Result { + let (mut x, mut y, mut bad) = (None, None, false); + while let Some(key) = map.next_key::()? { + let slot = match key { + Key::X => &mut x, + Key::Y => &mut y, + Key::Other => { + map.next_value::()?; + continue; + } + }; + let v = map.next_value::()?.0; + // A repeated or non-numeric coordinate makes the entry no point. + bad |= slot.is_some() || v.is_none(); + *slot = slot.or(v); + } + Ok(x.zip(y).filter(|_| !bad).map(|(x, y)| Point::new(x, y))) + } + none_for!(scalars); + none_for!(numbers); + } + d.deserialize_any(V).map(Corner) + } + } + struct Corners; - impl<'de> serde::de::Visitor<'de> for Corners { + impl<'de> Visitor<'de> for Corners { type Value = Option>; - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + fn expecting(&self, f: &mut Formatter) -> FmtResult { f.write_str("a list of points") } - fn visit_none(self) -> Result { - Ok(None) + fn visit_some>(self, d: D) -> Result { + d.deserialize_any(Corners) } - fn visit_unit(self) -> Result { - Ok(None) - } - fn visit_some>(self, d: D) -> Result { - d.deserialize_seq(Corners) - } - fn visit_seq>(self, mut seq: A) -> Result { + fn visit_seq>(self, mut seq: A) -> Result { let mut points = Vec::with_capacity(4); - while points.len() < 4 - && let Some(p) = seq.next_element::()? - { - points.push(p); + let mut malformed = false; + while points.len() < 4 && !malformed { + match seq.next_element::()? { + Some(Corner(Some(p))) => points.push(p), + Some(Corner(None)) => malformed = true, + None => break, + } } - // A fifth entry drops the field, so the rest is skipped without decoding it. - let mut extra = false; - while seq.next_element::()?.is_some() { - extra = true; - } - Ok(if extra { None } else { <[Point; 4]>::try_from(points).ok().map(Box::new) }) + // A malformed or fifth entry drops the field, so the rest is skipped without decoding it. + let extra = rest_of_list(seq)?; + Ok(if extra || malformed { None } else { <[Point; 4]>::try_from(points).ok().map(Box::new) }) } + none_for!(scalars); + none_for!(numbers); + none_for!(maps 'de); } d.deserialize_option(Corners) } diff --git a/docs/mcp.md b/docs/mcp.md index e7abf476a9..930a2aff7f 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -7565,8 +7565,8 @@ allowed. In EMF each piece stores only the pixels inside its clip, with a pixel clip, so they write the image as the parallelogram nearest its corners and warn. Corners that would fold the image over itself, put three corners on one line, join two corners, cross the warp's horizon or lie too close together for a perspective map are an error, and nothing changes. A transform that squeezes the corners past what a perspective map -can place leaves the image at the parallelogram nearest them. A file whose `image_quad` holds other than four points, -or breaks these rules, opens with the image placed by its transform. A drag is one undo step, and a distort that leaves +can place leaves the image at the parallelogram nearest them. A file whose `image_quad` is not a list, holds other than four points +(an entry that is not a point included), or breaks these rules, opens with the image placed by its transform. A drag is one undo step, and a distort that leaves the corners where they are records none. Image info (`image.info`) gives a distorted image's resolution at its middle, and Link Info (`links.info`) gives its scale and rotation there.