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

Catch infinite loop when passing sweep line over endpoints (#95) #97

Merged
merged 2 commits into from
Jun 29, 2020
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ The tests are broken up into unit tests and end-to-end tests. The end-to-end tes

The Martinez-Rueda-Feito polygon clipping algorithm is used to compute the result in `O((n+k)*log(n))` time, where `n` is the total number of edges in all polygons involved and `k` is the number of intersections between edges.

## Settings

Global settings are set via environment variables.

* **POLYGON_CLIPPING_MAX_QUEUE_SIZE** and **POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS**: Aims to prevent infinite loops - usually caused by floating-point math round-off errors. Defaults are 1,000,000.

## Changelog

This project adheres to [Semantic Versioning](https://semver.org/).
Expand Down
29 changes: 29 additions & 0 deletions src/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import rounder from './rounder'
import SweepEvent from './sweep-event'
import SweepLine from './sweep-line'

// Limits on iterative processes to prevent infinite loops - usually caused by floating-point math round-off errors.
const POLYGON_CLIPPING_MAX_QUEUE_SIZE = process.env.POLYGON_CLIPPING_MAX_QUEUE_SIZE || 1000000
const POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS = process.env.POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS || 1000000

export class Operation {
run (type, geom, moreGeoms) {
operation.type = type
Expand Down Expand Up @@ -52,6 +56,14 @@ export class Operation {
const sweepEvents = multipolys[i].getSweepEvents()
for (let j = 0, jMax = sweepEvents.length; j < jMax; j++) {
queue.insert(sweepEvents[j])

if (queue.size > POLYGON_CLIPPING_MAX_QUEUE_SIZE) {
// prevents an infinite loop, an otherwise common manifestation of bugs
throw new Error(
'Infinite loop when putting segment endpoints in a priority queue ' +
'(queue size too big). Please file a bug report.'
)
}
}
}

Expand All @@ -72,6 +84,23 @@ export class Operation {
'Please file a bug report.'
)
}

if (queue.size > POLYGON_CLIPPING_MAX_QUEUE_SIZE) {
// prevents an infinite loop, an otherwise common manifestation of bugs
throw new Error(
'Infinite loop when passing sweep line over endpoints ' +
'(queue size too big). Please file a bug report.'
)
}

if (sweepLine.segments.length > POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS) {
// prevents an infinite loop, an otherwise common manifestation of bugs
throw new Error(
'Infinite loop when passing sweep line over endpoints ' +
'(too many sweep line segments). Please file a bug report.'
)
}

const newEvents = sweepLine.process(evt)
for (let i = 0, iMax = newEvents.length; i < iMax; i++) {
const evt = newEvents[i]
Expand Down