From ac4454bb38825b72d12436d43501fe6945b4575c Mon Sep 17 00:00:00 2001 From: Simon Hull <5488089+simonhull@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:21:07 -0600 Subject: [PATCH] Geom: check indices before computing tangents (#386) Mesh::from_parts computed normals and tangents before validate(). An index past the last vertex reads as the origin in the MikkTSpace callbacks, but the all-degenerate guard compares positions.get() and counts a missing vertex as different. So two positions with indices [0, 1, 2] passed the guard while MikkTSpace saw every triangle as degenerate and underflowed at generated.rs:1100: a panic in debug, an out-of-bounds walk in release. compute_tangents now also skips MikkTSpace (using the per-triangle fallback, which reads with get()) when an index is out of range, and from_parts refuses such indices before building the mesh, with the same error validate() gives. --- crates/geom/src/mesh.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/geom/src/mesh.rs b/crates/geom/src/mesh.rs index 03d47845..e00ed374 100644 --- a/crates/geom/src/mesh.rs +++ b/crates/geom/src/mesh.rs @@ -315,8 +315,11 @@ impl Mesh { if self.uvs.len() != self.positions.len() { self.uvs = vec![Vec2::ZERO; self.positions.len()]; } - // mikktspace crashes (out-of-bounds) when every triangle is degenerate; skip it then. - if crate::unwrap::all_triangles_degenerate(self) { + // mikktspace crashes (out-of-bounds) when every triangle is degenerate; skip it then. Its + // callbacks read a missing vertex as the origin, which the degenerate check cannot see, so + // an index past the last vertex skips it too. + let n = self.positions.len(); + if !self.indices.iter().all(|&i| (i as usize) < n) || crate::unwrap::all_triangles_degenerate(self) { self.simple_tangents(); } else { let faces = self.tangent_faces(); @@ -497,6 +500,9 @@ impl Mesh { /// A single-material mesh from raw attributes; computes missing normals and tangents. pub fn from_parts(positions: Vec, normals: Option>, uvs: Vec, indices: Vec, material: &str) -> Result { + if let Some(bad) = indices.iter().find(|&&i| i as usize >= positions.len()) { + return Err(GeomError::Invalid(format!("index {bad} out of range ({} vertices)", positions.len()))); + } let count = indices.len() as u32; let mut m = Mesh { normals: normals.unwrap_or_default(), @@ -910,4 +916,30 @@ mod tests { assert_eq!(m.uv_tiles(1).into_iter().collect::>(), vec![(1, 0)]); assert_eq!(m.uv_tiles(0).into_iter().collect::>(), vec![(0, 0)]); } + + /// An index past the last vertex reads as the origin in MikkTSpace's callbacks. With two + /// positions at the origin and one missing, the degenerate-mesh guard (which sees the missing + /// vertex as different) let it through while MikkTSpace saw every triangle as degenerate and + /// underflowed: a panic in debug builds, an out-of-bounds walk in release. + #[test] + fn from_parts_refuses_an_out_of_range_index_without_crashing() { + let positions = vec![Vec3::ZERO, Vec3::X]; + let uvs = vec![Vec2::ZERO, Vec2::X]; + let e = Mesh::from_parts(positions, None, uvs, vec![0, 1, 2], "M").unwrap_err(); + assert!(e.to_string().contains("out of range"), "{e}"); + } + + #[test] + fn tangents_of_a_mesh_with_an_out_of_range_index_fall_back_without_crashing() { + for indices in [vec![0, 1, 2], vec![0, 0, 2], vec![0, 1, 5, 0, 1, 1]] { + let mut m = Mesh::from_parts(vec![Vec3::ZERO, Vec3::X, Vec3::Y], None, vec![Vec2::ZERO, Vec2::X, Vec2::Y], vec![0, 1, 2], "M").unwrap(); + m.positions.truncate(2); + m.normals.truncate(2); + m.uvs.truncate(2); + m.indices = indices; + m.compute_tangents(); + assert_eq!(m.tangents.len(), 2); + assert!(m.tangents.iter().all(|t| t.is_finite()), "{:?}", m.tangents); + } + } }