Stop reading Alembic indexed metadata after 255 entries (#464)

* fix(formats): stop reading Alembic indexed metadata after 255 entries (#447)

Objects and properties refer to indexed metadata with one byte, and 0xff already means the metadata is stored inline, so only entries 0 to 254 can ever be used. The reader kept every entry, so a block of zero bytes cost a whole String in memory for each byte of file. It now stops once 255 entries are held.

* test(formats): verify indexed metadata during archive import (#447)
This commit is contained in:
Prajwal Raj authored and GitHub committed 2026-11-13 12:58:42 +01:00
1 parent afc1ee169b
commit 386630ee85
2 files changed
+43 -6

No files matched your search

+17 -6
View File
@@ -281,19 +281,30 @@ fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
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<String>) -> 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,
@@ -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])]);
}