diff options
| author | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-23 00:31:25 +0000 |
|---|---|---|
| committer | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-23 00:31:25 +0000 |
| commit | d8a3201fa574dcbc60fac2fe4543ea5b5b970823 (patch) | |
| tree | e0cca88d0ddd7d82cac108a3ccc0c713994d0ddc | |
| parent | 53826fa5b52a1ceffdb16a5f0236f92b5897a10a (diff) | |
fix(first-light): reject OBJ UVs that overflow binary32
| -rw-r--r-- | tests/test_first_light.py | 11 | ||||
| -rw-r--r-- | tools/first_light.py | 4 |
2 files changed, 15 insertions, 0 deletions
diff --git a/tests/test_first_light.py b/tests/test_first_light.py index 796ca7899..52037a7a6 100644 --- a/tests/test_first_light.py +++ b/tests/test_first_light.py | |||
| @@ -269,6 +269,17 @@ class ReadObjTests(unittest.TestCase): | |||
| 269 | with self.assertRaisesRegex(SystemExit, "non-finite texture coordinates"): | 269 | with self.assertRaisesRegex(SystemExit, "non-finite texture coordinates"): |
| 270 | fl.read_obj(triangle(u, v)) | 270 | fl.read_obj(triangle(u, v)) |
| 271 | 271 | ||
| 272 | large_finite = triangle("1e38", "-1e38") | ||
| 273 | mesh = Path(large_finite).with_suffix(".bin") | ||
| 274 | fl.write_mesh(str(mesh), *fl.read_obj(large_finite)) | ||
| 275 | previous = mesh.read_bytes() | ||
| 276 | self.assertEqual(len(previous), 80) | ||
| 277 | for u, v in (("1e39", "0.75"), ("0.25", "-1e39")): | ||
| 278 | with self.subTest(u=u, v=v): | ||
| 279 | with self.assertRaisesRegex(SystemExit, "texture coordinates exceed the binary32 range"): | ||
| 280 | fl.write_mesh(str(mesh), *fl.read_obj(triangle(u, v))) | ||
| 281 | self.assertEqual(mesh.read_bytes(), previous) | ||
| 282 | |||
| 272 | def test_negative_relative_indices_inline_comments_and_continuations(self): | 283 | def test_negative_relative_indices_inline_comments_and_continuations(self): |
| 273 | positive = self._write_obj( | 284 | positive = self._write_obj( |
| 274 | "v 0 0 0\nv 1 0 0\nv 0 1 0\n" | 285 | "v 0 0 0\nv 1 0 0\nv 0 1 0\n" |
diff --git a/tools/first_light.py b/tools/first_light.py index f7db7319d..e9bac376b 100644 --- a/tools/first_light.py +++ b/tools/first_light.py | |||
| @@ -186,6 +186,10 @@ def read_obj( | |||
| 186 | raise SystemExit(f"{path}: invalid texture coordinates: {line.strip()!r}") from None | 186 | raise SystemExit(f"{path}: invalid texture coordinates: {line.strip()!r}") from None |
| 187 | if not (math.isfinite(u) and math.isfinite(v)): | 187 | if not (math.isfinite(u) and math.isfinite(v)): |
| 188 | raise SystemExit(f"{path}: non-finite texture coordinates: {line.strip()!r}") | 188 | raise SystemExit(f"{path}: non-finite texture coordinates: {line.strip()!r}") |
| 189 | try: | ||
| 190 | struct.pack("<2f", u, v) # Validate the mesh encoding before opening the output. | ||
| 191 | except OverflowError: | ||
| 192 | raise SystemExit(f"{path}: texture coordinates exceed the binary32 range: {line.strip()!r}") from None | ||
| 189 | uvs.append((u, v)) | 193 | uvs.append((u, v)) |
| 190 | elif p[0] == "f": | 194 | elif p[0] == "f": |
| 191 | face = [] | 195 | face = [] |