Skip to content

Commit 7c62021

Browse files
authored
@turf/unkink-polygon - Fix a potentially unbounded array.push.apply (#2506)
1 parent dd35b52 commit 7c62021

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ We intend to keep making breaking changes before 7.0.0 is fully released. If you
4545
- [`@turf/points-within-polygon`](points-within-polygon) Fix dropped properties on MultiPoint results (#2227)
4646
- [`@turf/random`](random) Throws error on invalid bbox inputs (#2172)
4747
- [`@turf/boolean-parallel`](boolean-parallel) Lines with 180 degree angle between them are also considered parallel (#2475)
48+
- [`@turf/unkink-polygon`](unkink-polygon) Fix a maximum call stack size exceeded error with very large polygons (#2504)
4849

4950
## 📖 Documentation
5051
- [`@turf/bbox`][bbox] Improve documentation (#2153)

packages/turf-unkink-polygon/lib/simplepolygon.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export default function (feature) {
4444
if (!equalArrays(ring[0], ring[ring.length - 1])) {
4545
ring.push(ring[0]); // Close input ring if it is not
4646
}
47-
vertices.push.apply(vertices, ring.slice(0, ring.length - 1));
47+
for (var j = 0; j < ring.length - 1; j++) {
48+
vertices.push(ring[j]);
49+
}
4850
}
4951
if (!isUnique(vertices))
5052
throw new Error(

packages/turf-unkink-polygon/test.js

+22
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ test("unkink-polygon", (t) => {
3636
t.end();
3737
});
3838

39+
test("issue #2504", (t) => {
40+
// fill coords with a circle with an arbitrary number of points
41+
const coords = [];
42+
const points = 1000000;
43+
for (let i = 0; i < points; i++) {
44+
const theta = (i / points) * (2 * Math.PI);
45+
coords.push([Math.sin(theta), Math.cos(theta)]);
46+
}
47+
coords.push(coords[0]);
48+
49+
try {
50+
unkinkPolygon({ type: "Polygon", coordinates: [coords] });
51+
t.pass(
52+
"large number of coordinates in a single ring should not cause an error"
53+
);
54+
} catch (e) {
55+
t.fail(e);
56+
}
57+
58+
t.end();
59+
});
60+
3961
test("unkink-polygon -- throws", (t) => {
4062
var array = [1, 2, 3, 4, 5];
4163
for (const value in array) {

0 commit comments

Comments
 (0)