Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Opening a design: range-check integer-valued coordinates too (#5021) (#5026)

raykholoc3d1ce82379a
diff --git a/crates/doc/src/format.rs b/crates/doc/src/format.rs
index c8838bd103..27afafc78b 100644
--- a/crates/doc/src/format.rs
+++ b/crates/doc/src/format.rs
@@ -578,6 +578,7 @@ pub fn read(s: &str) -> Result<Document> {
validate_body_transfers(&v)?;
FORMATS.upgrade(&mut v, version, applied.as_deref())?;
let mut d: Document = serde_json::from_value(v).map_err(|e| DocError::Invalid(format!("design file: {e}")))?;
+ check_typed_numbers(&d)?;
let mut legacy_by_feature = std::collections::BTreeMap::<usize, Vec<usize>>::new();
for (feature, wire) in legacy_wires {
legacy_by_feature.entry(feature).or_default().push(wire);
@@ -1046,6 +1047,16 @@ fn check_design_numbers(v: &Value) -> Result<()> {
}
}
+/// The range check again, on the typed design: an integer such as `9223372036854775808` passes
+/// [`check_numbers`] in the file (ids and counters are integers and may be large), but once it
+/// lands in a coordinate or size it is a float, and floats are written as floats. Integer fields
+/// (ids, counters, indexes) stay integers and are never limited, so the field's type decides
+/// what is checked, not its name.
+fn check_typed_numbers(d: &Document) -> Result<()> {
+ let typed = serde_json::to_value(d).map_err(|e| DocError::Invalid(format!("design file: {e}")))?;
+ check_design_numbers(&typed)
+}
+
/// Every non-integer number in the file is finite and within ±`MAX_REAL` (a damaged or hostile
/// file is refused instead of overflowing the geometry later).
fn check_numbers(v: &Value, depth: usize) -> Result<()> {
@@ -2095,6 +2106,38 @@ mod tests {
}
}
+ /// An integer-valued coordinate is range-checked like a float one, while ids and counters
+ /// may still be large integers (solidcraft#5021).
+ #[test]
+ fn integer_valued_coordinates_are_range_checked_and_large_ids_still_load() {
+ let mut sketch = solidcraft_sketch::Sketch::new();
+ sketch.add_point(solidcraft_geom::Vec2::new(40.0, 0.0), None).unwrap();
+ let mut d = Document::new("range");
+ d.add_feature(crate::FeatureKind::Sketch { plane: crate::PlaneRef::Origin { name: "XY".into() }, sketch }, None).unwrap();
+ let last = |v: &Value| v["features"][0]["sketch"]["points"].as_array().map_or(0, |a| a.len() - 1);
+ for huge in ["9223372036854775808", "4294967295", "2000000000", "-2000000000"] {
+ let json = edited(&d, |v| {
+ let i = last(v);
+ v["features"][0]["sketch"]["points"][i]["pos"][1] = serde_json::from_str(huge).unwrap();
+ });
+ let error = read(&json).unwrap_err().to_string();
+ assert!(error.contains("damaged design") && error.contains("out of range"), "{huge}: {error}");
+ }
+ // In range, an integer-valued coordinate is fine.
+ let json = edited(&d, |v| {
+ let i = last(v);
+ v["features"][0]["sketch"]["points"][i]["pos"][1] = 1_000_000_000u64.into();
+ });
+ assert!(read(&json).is_ok());
+ // Ids and counters are integers and may be past the coordinate limit.
+ let json = edited(&d, |v| {
+ v["features"][0]["id"] = 5_000_000_000u64.into();
+ v["next_id"] = 5_000_000_001u64.into();
+ });
+ let back = read(&json).unwrap();
+ assert_eq!(back.features[0].id, 5_000_000_000);
+ }
+
#[test]
fn exhausted_id_space_is_an_error_not_a_panic() {
let d = two_components_one_occurrence();