From fe8ee1bdb0243335560d3e726633cc0433ee99fc Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 21 Nov 2026 02:59:27 -0500 Subject: [PATCH] fix(formats): raise Alembic nesting depth limit from 64 to 256 (#728) * fix(formats): raise Alembic nesting depth limit from 64 to 256 * test(formats): avoid unwrap_err on RawMesh in abc depth test * test(formats): assert depth error message in abc nesting test --- crates/formats/src/mesh_import/abc/mod.rs | 5 ++- crates/formats/src/mesh_import/abc/tests.rs | 42 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/crates/formats/src/mesh_import/abc/mod.rs b/crates/formats/src/mesh_import/abc/mod.rs index deaba348..a95b8a38 100644 --- a/crates/formats/src/mesh_import/abc/mod.rs +++ b/crates/formats/src/mesh_import/abc/mod.rs @@ -10,6 +10,9 @@ use crate::{FormatError, Result, malformed}; const KIND: &str = "Alembic"; const DEFAULT_MATERIAL: &str = "Default"; +/// Real-world Alembic files (e.g. Blender's `long-names.abc`) exceed 64 nested transforms; 256 +/// accommodates them while keeping `craft-mesh`'s recursive walk stack-safe and bounded. +const MAX_DEPTH: usize = 256; fn limits() -> craft_mesh::Limits { craft_mesh::Limits { @@ -20,7 +23,7 @@ fn limits() -> craft_mesh::Limits { max_parts: super::MAX_VERTICES, max_materials: super::MAX_VERTICES, max_nodes: 1_000_000, - max_depth: 64, + max_depth: MAX_DEPTH, max_data_bytes: super::MAX_MESH_FILE_BYTES as usize, max_extras: 100_000, } diff --git a/crates/formats/src/mesh_import/abc/tests.rs b/crates/formats/src/mesh_import/abc/tests.rs index 3d93124e..f94f497d 100644 --- a/crates/formats/src/mesh_import/abc/tests.rs +++ b/crates/formats/src/mesh_import/abc/tests.rs @@ -372,3 +372,45 @@ fn indexed_metadata_stops_at_the_entries_a_byte_can_index() { assert_eq!(raw.positions, [Vec3::X, Vec3::Y, Vec3::ZERO]); assert_eq!(raw.groups, vec![(DEFAULT_MATERIAL.into(), "tri".into(), vec![0, 1, 2])]); } + +fn nested_xform_chain(levels: usize) -> Vec { + let mut a = Abc { w: Writer::new() }; + let pp = a.array("P", "", F32, 3, &f32s(&[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0])); + let fi = a.array(".faceIndices", "", I32, 1, &i32s(&[0, 1, 2])); + let fc = a.array(".faceCounts", "", I32, 1, &i32s(&[3])); + let geom = a.compound(".geom", "schema=AbcGeom_PolyMesh_v1", vec![pp, fi, fc]); + let mut node = a.object(vec![geom], &[]); + let mut child_name = "CubeShape"; + let mut child_schema = "schema=AbcGeom_PolyMesh_v1"; + let long_name = "a".repeat(63); + for i in 0..levels { + let ops = a.scalar(".ops", U8, 1, &[0x10]); + let coords: Vec<_> = [1.0f64, 0.0, 0.0].into_iter().flat_map(f64::to_le_bytes).collect(); + let vals = a.scalar(".vals", F64, 3, &coords); + let inherits = a.scalar(".inherits", U8, 1, &[1]); + let xf = a.compound(".xform", "schema=AbcGeom_Xform_v3", vec![ops, vals, inherits]); + node = a.object(vec![xf], &[(child_name, child_schema, node)]); + child_name = if i == 0 { "Cube" } else { &long_name }; + child_schema = "schema=AbcGeom_Xform_v3"; + } + let top = a.object(vec![], &[(&long_name, "schema=AbcGeom_Xform_v3", node)]); + a.archive(top, &["schema=AbcGeom_PolyMesh_v1"]) +} + +#[test] +fn long_transform_chains_import() { + for levels in [64usize, 200] { + let bytes = nested_xform_chain(levels); + let mesh = crate::import_mesh_bytes("long-names.abc", &bytes).unwrap().mesh; + assert_eq!(mesh.indices.len(), 3, "{levels} levels should import one triangle"); + let min_x = mesh.positions.iter().map(|p| p.x).fold(f32::INFINITY, f32::min); + assert!((min_x - levels as f32).abs() < 1e-4, "{levels} levels: expected min_x={levels}, got {min_x}"); + } +} + +#[test] +fn nesting_past_the_limit_is_an_error() { + let bytes = nested_xform_chain(MAX_DEPTH + 1); + let Err(err) = read(&bytes) else { panic!("expected error, got Ok") }; + assert!(err.to_string().contains("nested too deeply"), "unexpected error: {err}"); +}