Accept trailing propertyless PLY elements as before

The shared ASCII row reader counts the delimiter after the last value as read,
so a trailing element without properties (e.g. `element edge 1` after the last
vertex row) was rejected by the per-element byte bound. Propertyless elements
read nothing: skip their rows and bound their count by the bytes the previous
reader saw. Adds a regression test.
This commit is contained in:
FredApps committed 2026-11-16 07:25:59 +01:00
1 parent ebb4bc8673
commit fcc957b4ea
1 file changed
+19
+19
View File
@@ -60,7 +60,18 @@ pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
let (mut normals, mut uvs, mut colors) = (Vec::new(), Vec::new(), Vec::new());
// Elements may appear in any order, so take the vertex count from the header.
let nverts: usize = elements.iter().filter(|e| e.name == "vertex").map(|e| e.count).fold(0, usize::saturating_add);
// The shared ASCII reader counts the delimiter after a value as read; the previous reader
// left it in the remaining bytes. Only an element without properties can notice the difference.
let ascii_slack = usize::from(enc == Encoding::Ascii);
for el in &elements {
// An element without properties reads nothing, so its rows are skipped. Its count stays
// bounded by the bytes left, as the previous reader counted them.
if el.props.is_empty() {
if el.count > body.remaining().saturating_add(ascii_slack) {
return Err(malformed(KIND, format!("element \"{}\" claims {} items but the file is too short", el.name, el.count)));
}
continue;
}
// Every item takes at least one byte, so the count can never exceed what is left.
if el.count > body.remaining() {
return Err(malformed(KIND, format!("element \"{}\" claims {} items but the file is too short", el.name, el.count)));
@@ -203,6 +214,14 @@ fn header(bytes: &[u8]) -> Result<(Vec<Element>, Encoding, usize)> {
mod shared_rows_tests {
use super::*;
#[test]
fn trailing_propertyless_element_after_a_newline_still_opens() {
let bytes = b"ply\nformat ascii 1.0\nelement vertex 1\nproperty float x\nproperty float y\nproperty float z\nelement edge 1\nend_header\n0 0 0\n";
assert_eq!(read(bytes).unwrap().positions, vec![Vec3::new(0.0, 0.0, 0.0)]);
let huge = b"ply\nformat ascii 1.0\nelement vertex 1\nproperty float x\nproperty float y\nproperty float z\nelement edge 3\nend_header\n0 0 0\n";
assert!(read(huge).is_err());
}
#[test]
fn binary_scalar_endianness_matches_ascii() {
for (format, big) in [("binary_little_endian", false), ("binary_big_endian", true)] {