diff --git a/crates/formats/src/mesh_import/abc/mod.rs b/crates/formats/src/mesh_import/abc/mod.rs index 5a19982c..b0a0ef22 100644 --- a/crates/formats/src/mesh_import/abc/mod.rs +++ b/crates/formats/src/mesh_import/abc/mod.rs @@ -281,19 +281,30 @@ fn read_inner(bytes: &[u8]) -> Result { if root.len() < 6 || !(0..6).all(|i| is_data(i) != (i == 2)) { return Err(malformed(KIND, "the archive group does not have the Alembic layout")); } - // Indexed metadata: a u8 length before each string. let block = o.data(root[5])?; - let mut c = Cursor { b: block, pos: 0 }; - while c.pos < block.len() { - let n = c.u8()? as usize; - o.indexed_meta.push(c.string(n)?); - } + read_indexed_meta(block, &mut o.indexed_meta)?; let mut sink = PolygonSink::default(); let mut walk = Walk { o, sink: &mut sink, visits: 0 }; walk.object(root[2], Mat4::IDENTITY, "", 0)?; Ok(sink.finish()) } +/// The most indexed metadata entries that can ever be used: objects and properties refer to one +/// with a single byte, and 0xff already means "stored inline", so only 0 to 254 are reachable. +const MAX_INDEXED_META: usize = 255; + +/// Indexed metadata: a u8 length before each string. `out` starts with the empty entry 0. Reading +/// stops once `out` holds [`MAX_INDEXED_META`] entries, because no later entry can be referred to +/// and an empty one costs a whole `String` in memory for one byte of file. +fn read_indexed_meta(block: &[u8], out: &mut Vec) -> Result<()> { + let mut c = Cursor { b: block, pos: 0 }; + while c.pos < block.len() && out.len() < MAX_INDEXED_META { + let n = c.u8()? as usize; + out.push(c.string(n)?); + } + Ok(()) +} + struct Walk<'a, 'b> { o: Ogawa<'a>, sink: &'b mut PolygonSink, diff --git a/crates/formats/src/mesh_import/abc/tests.rs b/crates/formats/src/mesh_import/abc/tests.rs index fecb0623..92f18620 100644 --- a/crates/formats/src/mesh_import/abc/tests.rs +++ b/crates/formats/src/mesh_import/abc/tests.rs @@ -323,3 +323,29 @@ fn self_referencing_groups_are_bounded() { let bytes = a.archive(at, &[]); assert!(read(&bytes).is_err()); } + +#[test] +fn indexed_metadata_stops_at_the_entries_a_byte_can_index() { + // One real entry followed by a million empty ones: a byte each in the file, a `String` each + // in memory, and no index past 254 can be used anyway. + let mut block = vec![3, b'a', b'b', b'c']; + block.extend(std::iter::repeat_n(0u8, 1_000_000)); + let mut kept = vec![String::new()]; + read_indexed_meta(&block, &mut kept).unwrap(); + assert_eq!(kept.len(), MAX_INDEXED_META); + assert_eq!(kept.get(1).map(String::as_str), Some("abc")); + + // The mesh schema comes from metadata index 1 even when the table has unreachable entries. + 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 mesh = a.object(vec![geom], &[]); + let top = a.object(vec![], &[("tri", "", mesh)]); + let mut metas = vec!["schema=AbcGeom_PolyMesh_v1"]; + metas.extend(std::iter::repeat_n("", 5_000)); + let raw = read(&a.archive(top, &metas)).unwrap(); + assert_eq!(raw.positions, [Vec3::X, Vec3::Y, Vec3::ZERO]); + assert_eq!(raw.groups, vec![(DEFAULT_MATERIAL.into(), "tri".into(), vec![0, 1, 2])]); +}