* claim: #5020 * Insert SVG: refuse geometry scaled to infinity, and bound the profile grid (#5020) A transform such as scale(1e308) turned a circle's radius into infinity. The SVG reader now refuses non-finite transforms and geometry with a clear error, and Grid::cells in the profile finder uses checked arithmetic, so an infinite or huge box is checked against all shapes instead of wrapping the cell count and allocating until memory runs out. * Insert SVG: refuse lines with infinite ends; fix the grid test's cell count A line whose transformed ends are infinite was dropped silently, because the distance between them is NaN. It is now refused as out of range, like curves. The new grid test expected one cell for a box spanning its own one-box grid; such a box touches the 2 x 2 cells at its corners, so it expects 4.
This commit is contained in:
1 parent
8cbde001ef
commit
b00a1e87f4
2 files changed
+88
-2
No files matched your search
@@ -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<f64> {
|
||||
// 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<Vec<Geom2>> {
|
||||
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#"<svg><line x1="0" y1="0" x2="96" y2="0"/></svg>"#).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#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100mm" height="100mm">{body}</svg>"#);
|
||||
for body in [
|
||||
r#"<g transform="scale(1e308)"><circle r="5"/></g>"#,
|
||||
r#"<g transform="scale(1e308)"><g transform="scale(1e308)"></g></g>"#,
|
||||
r#"<g transform="scale(1e308)"><rect width="5" height="5"/></g>"#,
|
||||
r#"<g transform="scale(1e308)"><path d="M0 0 C 1 1 2 2 5 5"/></g>"#,
|
||||
r#"<g transform="matrix(1e308 0 0 1e308 1e308 1e308)"><line x1="1" y1="1" x2="5" y2="5"/></g>"#,
|
||||
r#"<g transform="scale(1e308)"><text x="0" y="0" font-size="16">A</text></g>"#,
|
||||
] {
|
||||
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#"<g transform="scale(1e6)"><circle r="5"/></g>"#)).unwrap();
|
||||
assert!(matches!(g[0], Geom2::Circle(_, r) if (r - 5e6).abs() < 1e-3));
|
||||
}
|
||||
}
|
||||
@@ -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<Vec<(i64, i64)>> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user