diff --git a/crates/io/src/svg.rs b/crates/io/src/svg.rs index 903a5cecf..f76b4e752 100644 --- a/crates/io/src/svg.rs +++ b/crates/io/src/svg.rs @@ -41,8 +41,28 @@ impl Tf { let [a, b, c, d, ..] = self.0; a * d - b * c < 0.0 } + fn is_finite(self) -> bool { + self.0.iter().all(|x| x.is_finite()) + } } +/// Every number of `g` is finite: a transform such as `scale(1e308)` can push a finite size in +/// the file past the range of `f64`, and an infinite curve must never reach a sketch. +fn geom_is_finite(g: &Geom2) -> bool { + match g { + Geom2::Point(p) => p.is_finite(), + Geom2::Line(a, b) => a.is_finite() && b.is_finite(), + Geom2::Circle(c, r) => c.is_finite() && r.is_finite(), + Geom2::Arc { c, a, b } => c.is_finite() && a.is_finite() && b.is_finite(), + Geom2::Ellipse { c, major, minor } => c.is_finite() && major.is_finite() && minor.is_finite(), + Geom2::Spline { pts, .. } => pts.iter().all(|p| p.is_finite()), + Geom2::Conic { a, apex, b, rho } => a.is_finite() && apex.is_finite() && b.is_finite() && rho.is_finite(), + Geom2::Text { at, height, angle, .. } => at.is_finite() && height.is_finite() && angle.is_finite(), + } +} + +const OUT_OF_RANGE: &str = "SVG geometry is out of range: a transform or size scales a shape past the largest number SolidCraft can store"; + fn numbers(s: &str) -> Vec { // Numbers separated by commas/space, also "1-2" and "1.5.5" forms. let mut out = Vec::new(); @@ -155,11 +175,19 @@ impl Out { if self.g.len() >= MAX_ENTITIES { return Err(IoError::Invalid("too many SVG elements".into())); } + if !geom_is_finite(&g) { + return Err(IoError::Invalid(OUT_OF_RANGE.into())); + } self.g.push(g); Ok(()) } fn line(&mut self, t: Tf, a: Vec2, b: Vec2) -> Result<()> { let (a, b) = (t.apply(a), t.apply(b)); + if !(a.is_finite() && b.is_finite()) { + // Checked before the length: the distance between infinite ends is NaN, which would + // drop the line silently. + return Err(IoError::Invalid(OUT_OF_RANGE.into())); + } if a.dist(b) > 1e-12 { self.push(Geom2::Line(a, b)) } else { Ok(()) } } fn cubic(&mut self, t: Tf, p: [Vec2; 4]) -> Result<()> { @@ -442,6 +470,9 @@ pub fn read_svg(text: &str) -> Result> { root = Some(r); tf = r; } + if !tf.is_finite() { + return Err(IoError::Invalid(OUT_OF_RANGE.into())); + } match name.as_str() { "path" => path(&mut out, tf, attr(&a, "d").unwrap_or(""))?, "line" => out.line(tf, Vec2::new(num_attr(&a, "x1"), num_attr(&a, "y1")), Vec2::new(num_attr(&a, "x2"), num_attr(&a, "y2")))?, @@ -557,4 +588,27 @@ mod tests { let g = read_svg(r#""#).unwrap(); assert!(matches!(g[0], Geom2::Line(a, b) if (a.dist(b) - 25.4).abs() < 1e-9)); } + + /// A transform that scales a shape past the range of `f64` is refused with a clear error + /// instead of handing the sketch an infinite curve (solidcraft#5020). + #[test] + fn geometry_scaled_to_infinity_is_refused() { + let doc = |body: &str| format!(r#"{body}"#); + for body in [ + r#""#, + r#""#, + r#""#, + r#""#, + r#""#, + r#"A"#, + ] { + match read_svg(&doc(body)) { + Err(IoError::Invalid(m)) => assert!(m.contains("out of range"), "{body}: {m}"), + other => panic!("{body}: expected an out-of-range error, got {other:?}"), + } + } + // Large but finite drawings still read. + let g = read_svg(&doc(r#""#)).unwrap(); + assert!(matches!(g[0], Geom2::Circle(_, r) if (r - 5e6).abs() < 1e-3)); + } } diff --git a/crates/sketch/src/profiles.rs b/crates/sketch/src/profiles.rs index 553e9c043..ef7054473 100644 --- a/crates/sketch/src/profiles.rs +++ b/crates/sketch/src/profiles.rs @@ -436,10 +436,16 @@ impl Grid { fn cell(&self, p: Vec2) -> (i64, i64) { (((p.x - self.lo.x) / self.size).floor() as i64, ((p.y - self.lo.y) / self.size).floor() as i64) } - /// Cells a box covers; None for boxes spanning too many (they are checked against all). + /// Cells a box covers; None for boxes spanning too many (they are checked against all), + /// including infinite or huge boxes whose cell span does not fit in an `i64`. fn cells(&self, b: &solidcraft_geom::Aabb2) -> Option> { + if !(b.min.is_finite() && b.max.is_finite()) { + return None; + } let (c0, c1) = (self.cell(b.min), self.cell(b.max)); - if (c1.0 - c0.0 + 1).saturating_mul(c1.1 - c0.1 + 1) > 2048 { + let span = |lo: i64, hi: i64| hi.checked_sub(lo)?.checked_add(1); + let count = span(c0.0, c1.0)?.checked_mul(span(c0.1, c1.1)?)?; + if count > 2048 { return None; } let mut out = Vec::new(); @@ -1339,3 +1345,29 @@ mod whole_boundary_tests { assert!(copy.whole_curve_boundaries().is_none()); } } + +#[cfg(test)] +mod tests { + use super::*; + use solidcraft_geom::Aabb2; + + /// An infinite or huge box spans more cells than an `i64` holds; it is checked against all + /// shapes instead of wrapping the count and filling memory (solidcraft#5020). + #[test] + fn grid_cells_refuse_infinite_and_huge_boxes() { + let small = Aabb2 { min: Vec2::new(0.0, 0.0), max: Vec2::new(10.0, 10.0) }; + let grid = Grid::new(&[small]); + // A box spanning the whole grid touches the 2 x 2 cells at its corners. + assert_eq!(grid.cells(&small).map(|c| c.len()), Some(4)); + let inf = Aabb2 { min: Vec2::new(-f64::INFINITY, -f64::INFINITY), max: Vec2::new(f64::INFINITY, f64::INFINITY) }; + assert!(grid.cells(&inf).is_none()); + let nan = Aabb2 { min: Vec2::new(f64::NAN, 0.0), max: Vec2::new(1.0, 1.0) }; + assert!(grid.cells(&nan).is_none()); + let huge = Aabb2 { min: Vec2::new(-1e300, -1e300), max: Vec2::new(1e300, 1e300) }; + assert!(grid.cells(&huge).is_none()); + // A grid built over an infinite box falls back to unit cells and still answers. + let grid = Grid::new(&[small, inf]); + assert!(grid.cells(&inf).is_none()); + assert!(grid.cells(&huge).is_none()); + } +}