From 08155e34e3cc12e445adf4b85215e020949b8f82 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 12 Nov 2026 13:37:48 -0800 Subject: [PATCH] .cdr import: a shape left out at the coordinate limit is not damage A curve or polygon whose points go past the coordinate limit was left out with both the limit warning and the damage warning, in RIFF files and in files of versions 1 and 2. It now gives the limit warning only. A polygon without its sides argument is still damage. --- crates/cdr/src/build.rs | 18 ++++++++++++------ crates/cdr/src/limits.rs | 15 +++++++++++++-- crates/cdr/src/wl.rs | 4 +++- crates/cdr/tests/read.rs | 30 +++++++++++++++++++++++++++++- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/crates/cdr/src/build.rs b/crates/cdr/src/build.rs index 9b6e21c8e8..a89342c1f3 100644 --- a/crates/cdr/src/build.rs +++ b/crates/cdr/src/build.rs @@ -552,22 +552,28 @@ impl<'a, 'b> Ctx<'a, 'b> { // Version 3 counts the first object types from 0, one below the later numbers (R32). let kind = if self.p.types_from_zero && loda.kind <= 4 { loda.kind + 1 } else { loda.kind }; // A shape without its coordinates, or a polygon without its sides, is damage like a shape - // whose coordinates can't be read. + // whose coordinates can't be read; one left out at the coordinate limit is not. + self.budget.coordinates_refused(); let g = match kind { RECTANGLE => coords.and_then(|c| geometry::rect(c, &self.p)), ELLIPSE => coords.and_then(|c| geometry::ellipse(c, &self.p)), CURVE => coords.and_then(|c| geometry::point_list(c, &self.p, self.budget, self.warn)).map(Geometry::Path), PATH if self.p.paths => coords.and_then(|c| geometry::path_record(c, &self.p, self.budget, self.warn)).map(Geometry::Path), - POLYGON => coords - .and_then(|c| geometry::point_list(c, &self.p, self.budget, self.warn)) - .zip(loda.arg(ARG_POLYGON)) - .and_then(|(base, sides)| geometry::polygon(&base, sides, &self.p, self.budget, self.warn)), + POLYGON => match loda.arg(ARG_POLYGON) { + Some(sides) => coords + .and_then(|c| geometry::point_list(c, &self.p, self.budget, self.warn)) + .and_then(|base| geometry::polygon(&base, sides, &self.p, self.budget, self.warn)), + None => { + push(self.warn, Warning::Damaged); + return None; + } + }, _ => { push(self.warn, Warning::UnsupportedObjects); return None; } }; - if g.is_none() { + if g.is_none() && !self.budget.coordinates_refused() { push(self.warn, Warning::Damaged); } g diff --git a/crates/cdr/src/limits.rs b/crates/cdr/src/limits.rs index b3aadc23df..d1b0ff7fae 100644 --- a/crates/cdr/src/limits.rs +++ b/crates/cdr/src/limits.rs @@ -74,11 +74,19 @@ pub(crate) struct Budget { objects: usize, coordinates: usize, dashes: usize, + /// A request for coordinates was refused since [`Budget::coordinates_refused`] last asked. + refused: bool, } impl Budget { pub(crate) fn new(limits: Limits) -> Self { - Budget { limits, inflated: 0, chunks: 0, objects: 0, coordinates: 0, dashes: 0 } + Budget { limits, inflated: 0, chunks: 0, objects: 0, coordinates: 0, dashes: 0, refused: false } + } + + /// Whether a request for coordinates was refused since the last call, which tells a shape + /// left out at the limit from one whose bytes don't parse. + pub(crate) fn coordinates_refused(&mut self) -> bool { + std::mem::take(&mut self.refused) } /// Bytes an inflate may still produce. @@ -117,7 +125,10 @@ impl Budget { self.coordinates = t; true } - None => false, + None => { + self.refused = true; + false + } } } diff --git a/crates/cdr/src/wl.rs b/crates/cdr/src/wl.rs index cb0c345204..ed6bae1ca2 100644 --- a/crates/cdr/src/wl.rs +++ b/crates/cdr/src/wl.rs @@ -134,8 +134,10 @@ fn page_box(b: &[u8], settings: usize, p: &Profile) -> Option<(f64, f64)> { /// One drawn object's record (R43, R44): a shape, or `None` with a warning for text, for an object /// of another type and for a shape that can't be read. fn object(rec: &[u8], fields: Fields, p: &Profile, budget: &mut Budget, warn: &mut Vec) -> Option { + // A curve left out at the coordinate limit is not damage. + budget.coordinates_refused(); let read = shape(rec, fields, p, budget, warn); - if read.is_none() { + if read.is_none() && !budget.coordinates_refused() { push(warn, Warning::Damaged); } read?.map(|s| Object::Shape(Box::new(s))) diff --git a/crates/cdr/tests/read.rs b/crates/cdr/tests/read.rs index e909174e10..9c201231eb 100644 --- a/crates/cdr/tests/read.rs +++ b/crates/cdr/tests/read.rs @@ -420,7 +420,7 @@ fn hostile_version_1_files_stop_within_their_bytes_and_limits() { curve[114..116].copy_from_slice(&3u16.to_le_bytes()); let d = read(&synth::wl_file(b'e', (612.0, 792.0), &[curve]), Limits { coordinates: 2, ..Limits::default() }).unwrap(); assert_eq!(wl_objects(&d).len(), 0); - assert!(d.warnings.contains(&Warning::TooDeep)); + assert_eq!(d.warnings, vec![Warning::TooDeep]); // Objects past the object limit are left out, and records past the chunk limit stop the read. let many: Vec> = (0..10).map(|_| rect()).collect(); let d = read(&synth::wl_file(b'e', (612.0, 792.0), &many), Limits { objects: 4, ..Limits::default() }).unwrap(); @@ -1401,6 +1401,34 @@ fn outlines_flagged_none_and_dashes_read() { assert_eq!((o.cap, o.join), (Cap::Square, vectorcraft_cdr::model::Join::Bevel)); } +/// A curve or polygon left out at the coordinate limit gives the limit warning, not the damage +/// warning. +#[test] +fn shapes_past_the_coordinate_limit_are_not_damage() { + let v = V(1100); + let curve = synth::object(v, 3, &[(30, v.points(&[(0.0, 0.0, 0), (10.0, 0.0, 0x40), (10.0, 10.0, 0x40)]))], None); + let side = synth::POLYGON_SIDE; + let mut sides = vec![0; 4]; + sides.extend(5u32.to_le_bytes()); + sides.extend([0; 8]); + sides.extend(0.05f64.to_le_bytes()); + sides.extend(0.05f64.to_le_bytes()); + sides.extend([v.coord(0.0), v.coord(0.0)].concat()); + let base = v.points(&[(0.0, side, 0x0C), (side * 0.587, side * 0.809, 0x44)]); + let polygon = synth::object(v, 0x14, &[(30, base.clone()), (11000, sides)], None); + for (objects, limit) in [(vec![curve], 2), (vec![polygon], 4)] { + let file = synth::riff(1100, &[synth::page(false, vec![synth::layer(v, [0, 0, 0, 0x98], "L", objects)])]); + let d = read(&file, Limits { coordinates: limit, ..Limits::default() }).unwrap(); + assert!(d.pages[0].layers[0].objects.is_empty()); + assert_eq!(d.warnings, vec![Warning::TooDeep]); + } + // A polygon without its sides is damage, past the limit too. + let sideless = synth::object(v, 0x14, &[(30, base)], None); + let file = synth::riff(1100, &[synth::page(false, vec![synth::layer(v, [0, 0, 0, 0x98], "L", vec![sideless])])]); + let d = read(&file, Limits { coordinates: 1, ..Limits::default() }).unwrap(); + assert_eq!(d.warnings, vec![Warning::Damaged]); +} + /// Dashes stored in an outline whose line type doesn't mark it dashed (bit 2) are not drawn: the /// outline is solid. #[test]