From f658926eb18daf583437cd7a179725b097c5cae7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:40:50 +0000 Subject: [PATCH] Offer strict FBX ASCII record parsing --- crates/mesh/src/fbx.rs | 36 ++++++++++++++++++++++++++++++++---- crates/mesh/src/tests/fbx.rs | 10 ++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 49191c0c0..6c2d92e32 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -230,6 +230,15 @@ pub fn is_binary(bytes: &[u8]) -> bool { /// Read an FBX file (binary or ASCII) into its records. pub fn read_document(bytes: &[u8], limits: &Limits) -> Result { + read_document_impl(bytes, limits, false) +} + +/// Read records with strict ASCII syntax checking for malformed strings and record delimiters. +pub fn read_document_strict(bytes: &[u8], limits: &Limits) -> Result { + read_document_impl(bytes, limits, true) +} + +fn read_document_impl(bytes: &[u8], limits: &Limits, strict: bool) -> Result { let budget = Budget::new(limits.max_data_bytes); let (version, records) = if is_binary(bytes) { read_binary(bytes, &budget)? @@ -243,7 +252,7 @@ pub fn read_document(bytes: &[u8], limits: &Limits) -> Result { String::from_utf8_lossy(bytes) } }; - read_ascii(&text, &budget)? + read_ascii(&text, &budget, strict)? }; Ok(Document { version, records, left: budget.left.get() }) } @@ -441,6 +450,8 @@ struct Ascii<'a> { text: &'a str, pos: usize, lookahead: Option>, + strict: bool, + unterminated: bool, } impl<'a> Ascii<'a> { @@ -468,6 +479,7 @@ impl<'a> Ascii<'a> { '"' => { let tail = rest.get(1..).unwrap_or(""); let end = tail.find('"').unwrap_or(tail.len()); + self.unterminated |= end == tail.len(); self.pos += 1 + end + usize::from(end < tail.len()); return Some(Tok::Value(tail.get(..end).unwrap_or(""), true)); } @@ -512,11 +524,14 @@ fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, de while let Some(t) = scan.take() { match t { Tok::Close if depth > 0 => return Ok(out), + Tok::Close if scan.strict => return Err(malformed(KIND, "unbalanced closing brace")), Tok::Key(name) => { records_budget.take(std::mem::size_of::().checked_add(name.len()), "its records")?; let mut record = Record { name: name.to_string(), ..Record::default() }; + let mut needs_comma = false; loop { match scan.peek() { + Some(Tok::Value(..) | Tok::Count) if scan.strict && needs_comma => break, Some(Tok::Value(value, quoted)) => { let number = if quoted { None @@ -530,9 +545,15 @@ fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, de .props .push(number.unwrap_or_else(|| Prop::Str(if quoted { value.replace(""", "\"") } else { value.to_string() }))); } - Some(Tok::Count | Tok::Comma) => {} + Some(Tok::Count) => {} + Some(Tok::Comma) => { + needs_comma = false; + scan.take(); + continue; + } _ => break, } + needs_comma = true; scan.take(); } if scan.peek() == Some(Tok::Open) { @@ -541,16 +562,23 @@ fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, de } out.push(record); } + _ if scan.strict => return Err(malformed(KIND, "expected a property name")), _ => {} } } + if scan.strict && depth > 0 { + return Err(malformed(KIND, "missing closing brace")); + } Ok(out) } -fn read_ascii(text: &str, budget: &Budget) -> Result<(u32, Vec)> { - let mut scan = Ascii { text, pos: 0, lookahead: None }; +fn read_ascii(text: &str, budget: &Budget, strict: bool) -> Result<(u32, Vec)> { + let mut scan = Ascii { text, pos: 0, lookahead: None, strict, unterminated: false }; let records_budget = Budget::new(budget.limit); let records = ascii_list(&mut scan, budget, &records_budget, 0)?; + if strict && scan.unterminated { + return Err(malformed(KIND, "unterminated string")); + } let version = records .iter() .find(|n| n.name == "FBXHeaderExtension") diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index d5978883e..9541d67cb 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -641,3 +641,13 @@ fn ascii_budget_applies_while_reading_values_and_records() { let ignored = format!(";{}\nObjects: {{}}", ",".repeat(10_000)); assert!(read_document(ignored.as_bytes(), &limits).is_ok()); } + +#[test] +fn strict_ascii_reports_unbalanced_and_unterminated_records() { + let strict = |s: &str| crate::fbx::read_document_strict(s.as_bytes(), &Limits::default()); + assert!(strict(ASCII).is_ok()); + for s in ["Objects: { A: 1", "Objects: { } }", "Objects: { A: \"unterminated", "Objects: { A: 1 2 }"] { + assert!(strict(s).is_err(), "{s}"); + } + assert!(read_document(b"Objects: { A: 1", &Limits::default()).is_ok()); +}