SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Import: a curve that refers to itself is refused instead of overflowing the stack (#11893)
kaltenbrunnermarkus63ffadfebf9b
diff --git a/crates/step/src/geom_tests.rs b/crates/step/src/geom_tests.rsindex 244aa2aee7..a1c9bf237d 100644--- a/crates/step/src/geom_tests.rs+++ b/crates/step/src/geom_tests.rs@@ -415,6 +415,35 @@ fn hostile_geometry_is_an_error_never_a_panic() {}}+/// A curve whose definition leads back to itself, directly or through another curve, is refused+/// with an error that names a curve of the loop. A 2D trimmed curve in a pcurve used to follow+/// its basis without the depth check of the other readers, until the stack overflowed and the+/// app closed; here it is reached both as a surface curve and as the profile of a face's surface.+#[test]+fn a_curve_that_refers_to_itself_is_an_error_never_a_crash() {+ let trimmed = |id: u64, basis: u64| format!("#{id}=TRIMMED_CURVE('',#{basis},(PARAMETER_VALUE(0.)),(PARAMETER_VALUE(1.)),.T.,.PARAMETER.);");+ for curves in [trimmed(20, 20), trimmed(20, 21) + &trimmed(21, 20)] {+ let ex = file(&format!(+ "{BASE}#10=PLANE('',#4);#11=VECTOR('',#3,1.);#12=LINE('',#1,#11);\+ #13=(GEOMETRIC_REPRESENTATION_CONTEXT(2)PARAMETRIC_REPRESENTATION_CONTEXT()REPRESENTATION_CONTEXT('2D SPACE',''));\+ {curves}#14=DEFINITIONAL_REPRESENTATION('',(#20),#13);#15=PCURVE('',#10,#14);#16=SURFACE_CURVE('',#12,(#15),.CURVE_3D.);\+ #17=SURFACE_OF_LINEAR_EXTRUSION('',#16,#11);"+ ));+ let read = [Reader::new(&ex, MM).curve(16).map(|_| ()), Reader::new(&ex, MM).surface(17).map(|_| ())];+ for got in read {+ let Err(e) = got else { panic!("read although it refers to itself: {curves}") };+ assert!(e.contains("curve #20") || e.contains("curve #21"), "{e}");+ }+ }+ // A 3D curve and a surface that refer to themselves name themselves too.+ let ex = file(&format!("{BASE}{}", trimmed(10, 10)));+ let e = Reader::new(&ex, MM).curve(10).err().unwrap_or_default();+ assert!(e.contains("curve #10"), "{e}");+ let ex = file(&format!("{BASE}#10=OFFSET_SURFACE('',#10,1.,.F.);"));+ let e = Reader::new(&ex, MM).surface(10).err().unwrap_or_default();+ assert!(e.contains("surface #10"), "{e}");+}+/// Every edge curve and face surface of every STEP file in `SOLIDCRAFT_STEP_CORPUS` (the/// tangency-labs/models files): read, written, read back. Run with `--ignored`.#[test]diff --git a/crates/step/src/read.rs b/crates/step/src/read.rsindex f9c9c11fa7..36b2ba40ce 100644--- a/crates/step/src/read.rs+++ b/crates/step/src/read.rs@@ -13,7 +13,10 @@ use crate::p21::{Entity, Exchange, Param};pub type R<T> = Result<T, String>;-/// Geometry nesting we follow (a trimmed curve of a surface curve of …).+/// Geometry nesting we follow (a trimmed curve of a surface curve of …). Real files nest a few+/// levels; the cap bounds the stack, and it is what ends a curve or surface whose definition leads+/// back to itself, so every curve and surface read checks it (`read_curve`,+/// `read_surface`).pub const MAX_GEOM_DEPTH: usize = 12;/// Most control points in one B-spline (the whole net for a surface).pub const MAX_CTRL: usize = 1_000_000;@@ -385,9 +388,6 @@ impl<'a> Reader<'a> {if let Some(c) = self.curves.get(&id) {return Ok(c.clone());}- if depth > MAX_GEOM_DEPTH {- return Err("curve definitions nested too deeply".into());- }let l = self.units.len;let c = Arc::new(self.read_curve(id, [l, l, l], 3, depth)?);self.curves.insert(id, c.clone());@@ -401,9 +401,6 @@ impl<'a> Reader<'a> {if let Some(c) = self.curves_2d.get(&key) {return Ok(c.clone());}- if depth > MAX_GEOM_DEPTH {- return Err("curve definitions nested too deeply".into());- }let c = Arc::new(self.read_curve(id, s, 2, depth)?);self.curves_2d.insert(key, c.clone());Ok(c)@@ -425,7 +422,12 @@ impl<'a> Reader<'a> {Ok(s[0])}+ /// Every curve is read here, the basis of a 2D trimmed curve too, so the depth check here+ /// ends every chain of curve references, a cycle included.fn read_curve(&mut self, id: u64, s: [f64; 3], dim: usize, depth: usize) -> R<Curve> {+ if depth > MAX_GEOM_DEPTH {+ return Err(format!("curve #{id}: definitions nested more than {MAX_GEOM_DEPTH} deep, or referring to themselves"));+ }let e = entity(self.ex, id)?;if e.is_complex() {return self.bspline_curve(e, s, dim);@@ -633,15 +635,15 @@ impl<'a> Reader<'a> {if let Some(s) = self.surfaces.get(&id) {return Ok(s.clone());}- if depth > MAX_GEOM_DEPTH {- return Err("surface definitions nested too deeply".into());- }let s = Arc::new(self.read_surface(id, depth)?);self.surfaces.insert(id, s.clone());Ok(s)}fn read_surface(&mut self, id: u64, depth: usize) -> R<Surface> {+ if depth > MAX_GEOM_DEPTH {+ return Err(format!("surface #{id}: definitions nested more than {MAX_GEOM_DEPTH} deep, or referring to themselves"));+ }let e = entity(self.ex, id)?;if e.is_complex() {return self.bspline_surface(e);