Merge branch 'codex/fbx-strict-ascii' into codex/fbx-integration

# Conflicts:
#	crates/mesh/src/tests/fbx.rs
This commit is contained in:
Dylan M. Taylor
2026-11-15 18:52:43 +00:00
2 changed files with 22 additions and 5 deletions
+12 -5
View File
@@ -241,7 +241,7 @@ pub fn read_document(bytes: &[u8], limits: &Limits) -> Result<Document> {
read_document_impl(bytes, limits, false)
}
/// Read records with strict ASCII syntax checking for malformed strings and record delimiters.
/// Read records strictly, checking ASCII delimiters and the binary closing record.
pub fn read_document_strict(bytes: &[u8], limits: &Limits) -> Result<Document> {
read_document_impl(bytes, limits, true)
}
@@ -249,7 +249,7 @@ pub fn read_document_strict(bytes: &[u8], limits: &Limits) -> Result<Document> {
fn read_document_impl(bytes: &[u8], limits: &Limits, strict: bool) -> Result<Document> {
let budget = Budget::new(limits.max_data_bytes);
let (version, records) = if is_binary(bytes) {
read_binary(bytes, &budget)?
read_binary(bytes, &budget, strict)?
} else {
let text = match std::str::from_utf8(bytes) {
Ok(text) => std::borrow::Cow::Borrowed(text),
@@ -435,21 +435,28 @@ fn object_name(b: &[u8]) -> String {
}
}
fn read_binary(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec<Record>)> {
fn read_binary(bytes: &[u8], budget: &Budget, strict: bool) -> Result<(u32, Vec<Record>)> {
let mut pos = BINARY_MAGIC.len();
let records_budget = Budget::new(budget.limit);
let version = Bin { d: bytes, wide: false, budget, records_budget: &records_budget }.u32(&mut pos)?;
let bin = Bin { d: bytes, wide: version >= 7500, budget, records_budget: &records_budget };
let mut records = vec![];
let mut closed = false;
while pos < bytes.len() {
match bin.record(&mut pos, bytes.len(), 0) {
Ok(Some(n)) => records.push(n),
Ok(None) => break,
Ok(None) => {
closed = true;
break;
}
// A damaged tail (footer, padding) after complete sections is ignored.
Err(crate::Error::Malformed(_)) if records.iter().any(|n| n.name == "Objects") => break,
Err(crate::Error::Malformed(_)) if !strict && records.iter().any(|n| n.name == "Objects") => break,
Err(e) => return Err(e),
}
}
if strict && !closed {
return Err(malformed(KIND, "missing binary closing record"));
}
Ok((version, records))
}
+10
View File
@@ -834,3 +834,13 @@ fn object_only_hierarchy_ignores_property_connections() {
assert_eq!(scene.roots.len(), 2);
assert_eq!(doc.scene(&Limits::default(), &mut no_external).unwrap().roots.len(), 1);
}
#[test]
fn strict_binary_requires_a_closing_record() {
let doc = document(ASCII.as_bytes());
let bytes = write_binary(&doc.records, 7400, false);
assert!(crate::fbx::read_document_strict(&bytes, &Limits::default()).is_ok());
let truncated = &bytes[..bytes.len() - 29];
assert!(read_document(truncated, &Limits::default()).is_ok());
assert!(crate::fbx::read_document_strict(truncated, &Limits::default()).is_err());
}