From 66b338c1123164a78a13ec50b4c7c158afbc5620 Mon Sep 17 00:00:00 2001 From: rdb Date: Wed, 18 Sep 2019 15:45:45 +0200 Subject: [PATCH] mathutil: workaround for infinite loop in Triangulator Fixes #737 --- panda/src/mathutil/triangulator.cxx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/panda/src/mathutil/triangulator.cxx b/panda/src/mathutil/triangulator.cxx index fabfe31147..da29f022be 100644 --- a/panda/src/mathutil/triangulator.cxx +++ b/panda/src/mathutil/triangulator.cxx @@ -2185,6 +2185,8 @@ triangulate_single_polygon(int nvert, int posmax, int side) { endv = mchain[posmax].vnum; } + int num_triangles = 0; + while ((v != endv) || (ri > 1)) { // cerr << " v = " << v << " ri = " << ri << " rc = " << rc.size() << " @@ -2199,8 +2201,13 @@ triangulate_single_polygon(int nvert, int posmax, int side) { vert[rc[ri]].pt); if ( crossResult >= 0 ) /* could be convex corner or straight */ { - if ( crossResult > 0) /* convex corner: cut it off */ + if (crossResult > 0) { /* convex corner: cut it off */ _result.push_back(Triangle(this, rc[ri - 1], rc[ri], v)); + if (++num_triangles >= nvert - 2) { + // We can't generate more than this number of triangles. + return; + } + } /* else : perfectly straight, will be abandoned anyway */ ri--; rc.pop_back();