Skip to content

Commit

Permalink
@turf/unkink-polygon - Fix a potentially unbounded array.push.apply (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mfedderly authored Oct 6, 2023
1 parent dd35b52 commit 7c62021
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ We intend to keep making breaking changes before 7.0.0 is fully released. If you
- [`@turf/points-within-polygon`](points-within-polygon) Fix dropped properties on MultiPoint results (#2227)
- [`@turf/random`](random) Throws error on invalid bbox inputs (#2172)
- [`@turf/boolean-parallel`](boolean-parallel) Lines with 180 degree angle between them are also considered parallel (#2475)
- [`@turf/unkink-polygon`](unkink-polygon) Fix a maximum call stack size exceeded error with very large polygons (#2504)

## 📖 Documentation
- [`@turf/bbox`][bbox] Improve documentation (#2153)
Expand Down
4 changes: 3 additions & 1 deletion packages/turf-unkink-polygon/lib/simplepolygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export default function (feature) {
if (!equalArrays(ring[0], ring[ring.length - 1])) {
ring.push(ring[0]); // Close input ring if it is not
}
vertices.push.apply(vertices, ring.slice(0, ring.length - 1));
for (var j = 0; j < ring.length - 1; j++) {
vertices.push(ring[j]);
}
}
if (!isUnique(vertices))
throw new Error(
Expand Down
22 changes: 22 additions & 0 deletions packages/turf-unkink-polygon/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ test("unkink-polygon", (t) => {
t.end();
});

test("issue #2504", (t) => {
// fill coords with a circle with an arbitrary number of points
const coords = [];
const points = 1000000;
for (let i = 0; i < points; i++) {
const theta = (i / points) * (2 * Math.PI);
coords.push([Math.sin(theta), Math.cos(theta)]);
}
coords.push(coords[0]);

try {
unkinkPolygon({ type: "Polygon", coordinates: [coords] });
t.pass(
"large number of coordinates in a single ring should not cause an error"
);
} catch (e) {
t.fail(e);
}

t.end();
});

test("unkink-polygon -- throws", (t) => {
var array = [1, 2, 3, 4, 5];
for (const value in array) {
Expand Down

0 comments on commit 7c62021

Please sign in to comment.