From 40b9b7ef45a0b5d61873aab42d1059c6d507aa58 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:13:32 +0000 Subject: [PATCH] Bound OBJ face and continued-line parsing --- crates/mesh/src/obj.rs | 34 ++++++++++++++++++++++++++-------- crates/mesh/src/tests/obj.rs | 11 +++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/crates/mesh/src/obj.rs b/crates/mesh/src/obj.rs index 092a4e0ff..d1354a66d 100644 --- a/crates/mesh/src/obj.rs +++ b/crates/mesh/src/obj.rs @@ -331,15 +331,26 @@ impl Reader<'_> { Ok(object.parts.get_mut(slot)) } - fn face(&mut self, rest: &[&str]) -> Result<()> { - if rest.len() > self.limits.max_face_corners { - return Err(too_large(KIND, format!("a face of more than {} corners", self.limits.max_face_corners))); + fn face<'a>(&mut self, rest: impl Iterator) -> Result<()> { + let mut corners = Vec::new(); + let mut invalid = false; + for token in rest { + // Check before collecting tokens or corners: one line can hold millions of them. + if corners.len() >= self.limits.max_face_corners { + return Err(too_large(KIND, format!("a face of more than {} corners", self.limits.max_face_corners))); + } + match self.corner(token) { + Some(corner) => corners.push(corner), + None => { + invalid = true; + corners.push(Corner::at(0)); + } + } } - let corners: Option> = rest.iter().map(|t| self.corner(t)).collect(); - let Some(corners) = corners.filter(|c| c.len() >= 3) else { + if invalid || corners.len() < 3 { self.bad_lines += 1; return Ok(()); - }; + } self.triangles = self.triangles.saturating_add(corners.len() - 2); if self.triangles > self.limits.max_triangles { return Err(too_large(KIND, format!("more than {} triangles", self.limits.max_triangles))); @@ -361,7 +372,7 @@ impl Reader<'_> { let Some(key) = it.next() else { return Ok(()) }; let after = line.get(key.len()..).unwrap_or("").trim(); match key { - "v" => self.vertex(&it.collect::>())?, + "v" => self.vertex(&it.take(6).collect::>())?, "vt" => { self.pool_room(self.vt.len(), "texture coordinates")?; // `vt u` alone is a 1D coordinate; OBJ's v axis points up the image. @@ -390,7 +401,7 @@ impl Reader<'_> { } "usemtl" => self.use_material(after)?, "mtllib" => self.library(after, resolve)?, - "f" => self.face(&it.collect::>())?, + "f" => self.face(it)?, _ => {} } Ok(()) @@ -445,6 +456,10 @@ impl Reader<'_> { /// writes; every name asked, and every image a library names, is listed in /// [`Scene::external`]. pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { + // Lossy UTF-8 conversion can allocate, so bound it before copying invalid input. + if std::str::from_utf8(bytes).is_err() && bytes.len() > limits.max_data_bytes { + return Err(too_large(KIND, "invalid UTF-8 text exceeds the memory limit")); + } let text = String::from_utf8_lossy(bytes); let mut r = Reader { limits, @@ -469,6 +484,9 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { let mut joined = String::new(); for raw in text.lines() { let line = raw.split('#').next().unwrap_or(""); + if line.len() > limits.max_data_bytes || joined.len().checked_add(line.len()).is_none_or(|n| n > limits.max_data_bytes) { + return Err(too_large(KIND, "a continued line exceeds the memory limit")); + } if let Some(head) = line.trim_end().strip_suffix('\\') { joined.push_str(head); joined.push(' '); diff --git a/crates/mesh/src/tests/obj.rs b/crates/mesh/src/tests/obj.rs index 9c61773cb..5283012c4 100644 --- a/crates/mesh/src/tests/obj.rs +++ b/crates/mesh/src/tests/obj.rs @@ -341,3 +341,14 @@ fn a_library_named_again_is_read_once() { assert_eq!((s.materials.len(), s.images.len()), (1, 1)); assert_eq!(s.external, ["m.mtl", "red.png"]); } + +#[test] +fn face_tokens_and_continued_lines_are_bounded_before_collection() { + let limits = Limits { max_face_corners: 3, max_data_bytes: 1024, ..Limits::default() }; + let face = format!("v 0 0 0\nf {}\n", "1 ".repeat(100)); + assert!(matches!(read(face.as_bytes(), &limits, &mut no_external), Err(Error::TooLarge(_)))); + let long_line = format!("v {}\\\n{}\n", "1 ".repeat(300), "1 ".repeat(300)); + assert!(matches!(read(long_line.as_bytes(), &limits, &mut no_external), Err(Error::TooLarge(_)))); + let invalid = b"v 0 0 0\nf 1 1 1\n\xff"; + assert!(matches!(read(invalid, &Limits { max_data_bytes: 4, ..Limits::default() }, &mut no_external), Err(Error::TooLarge(_)))); +}