Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@turf/unkink-polygon - Fix a potentially unbounded array.push.apply #2506

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This number has to be really large in node, I think the limit in Chrome is somewhat smaller

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