mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
Bound OBJ face and continued-line parsing
This commit is contained in:
1 parent
654e8d55e1
commit
40b9b7ef45
2 files changed
+37
-8
No files matched your search
+26
-8
@@ -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<Item = &'a str>) -> 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<Vec<Corner>> = 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::<Vec<_>>())?,
|
||||
"v" => self.vertex(&it.take(6).collect::<Vec<_>>())?,
|
||||
"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::<Vec<_>>())?,
|
||||
"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<Scene> {
|
||||
// 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<Scene> {
|
||||
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(' ');
|
||||
|
||||
@@ -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(_))));
|
||||
}
|
||||
Reference in new issue
Block a user