From 0838881b87f3a59ccd841d247e4b975347529220 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Wed, 28 Oct 2026 10:44:16 +0100 Subject: Turn remaining malformed-input crashes into Error Co-Authored-By: Claude Sonnet 5.5 --- generat3d/scene.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'generat3d/scene.py') diff --git a/generat3d/scene.py b/generat3d/scene.py index 25e4b61..28dc00c 100644 --- a/generat3d/scene.py +++ b/generat3d/scene.py @@ -62,9 +62,17 @@ class Scene: graph: Graph +def _finite(v): + return (not isinstance(v, bool) and isinstance(v, (int, float)) + and math.isfinite(v)) + + def _channel(node, name): comps = {c["id"]: c.get("current_value", c.get("value", 0.0)) for c in node.get(name, [])} - return [comps.get(a, 0.0) for a in AXES] + out = [comps.get(a, 0.0) for a in AXES] + if not all(_finite(v) for v in out): + raise Error(f"{node['id']}: {name} is not a finite number") + return out def _lookup(library, doc, url, section): @@ -147,9 +155,12 @@ class _Loader: self.graph.add_prop(key(n["id"], n["id"], f"{ch}/{a}"), v) for f in n.get("formulas", []): self.pending.append((f, fpath)) + verts = np.array(geom["vertices"]["values"], dtype=float) + if verts.ndim != 2 or verts.shape[1] != 3 or not np.isfinite(verts).all(): + raise Error(f"{fpath}: bad vertices") return Figure(asset["id"], inst.get("name", asset.get("name", asset["id"])), inst.get("label", asset.get("label", asset["id"])), - np.array(geom["vertices"]["values"], dtype=float), + verts, list(geom.get("polygon_material_groups", {}).get("values", [])), nodes) @@ -179,6 +190,9 @@ class _Loader: if parent and parent != self.figure.id: continue ch = mod.get("channel", {}) + for name in ("value", "min", "max"): + if name in ch and not _finite(ch[name]): + raise Error(f"{path}#{mod['id']}: {name} is not a finite number") k = key(self.figure.id, mod["id"], "value") self.graph.add_prop(k, ch.get("value", 0.0), ch.get("min", -math.inf), ch.get("max", math.inf), ch.get("clamped", False)) @@ -206,7 +220,7 @@ class _Loader: doc = self.library.load(path) try: self._modifiers(path, doc) - except (KeyError, TypeError, ValueError, IndexError, AttributeError) as e: + except (KeyError, TypeError, ValueError, IndexError, AttributeError, OverflowError) as e: raise Error(f"{path}: malformed morph data: {e!r}") from None def _morphs(self, fpath): @@ -263,5 +277,5 @@ def load(path, library): """Load a .duf scene and everything its figure needs.""" try: return _Loader(path, library).run() - except (KeyError, TypeError, IndexError, ValueError, AttributeError) as e: + except (KeyError, TypeError, IndexError, ValueError, AttributeError, OverflowError) as e: raise Error(f"{path}: malformed scene data: {e!r}") from None -- cgit v1.2.3