Skip to content

Fix off-by-one indexing bug in DouglasPeucker simplify#387

Open
asinghvi17 wants to merge 1 commit intomainfrom
as/fix386
Open

Fix off-by-one indexing bug in DouglasPeucker simplify#387
asinghvi17 wants to merge 1 commit intomainfrom
as/fix386

Conversation

@asinghvi17
Copy link
Member

Summary

  • Fix BoundsError when simplifying small geometries with low number or ratio values
  • Add regression test for issue Simplify indexing bug #386

Problem

In the DouglasPeucker _simplify function, the while loop condition:

while i  min(MIN_POINTS + 1, max_points) || (i < max_points && max_dist > max_tol)

allowed i to equal max_points and enter the loop. Then i += 1 would make i = max_points + 1, causing results[i] to be out of bounds since the array is sized for exactly max_points elements.

Fix

Changed i ≤ to i < in the first part of the condition:

while i < min(MIN_POINTS + 1, max_points) || (i < max_points && max_dist > max_tol)

Test plan

  • Added test case that reproduces the bug with small geometries
  • All existing simplify tests pass

Fixes #386

🤖 Generated with Claude Code

The while loop condition allowed `i` to reach `max_points` before
incrementing, causing a BoundsError when accessing `results[i]` after
`i += 1`. This occurred when simplifying small geometries with low
`number` or `ratio` values.

Fixes #386

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@asinghvi17 asinghvi17 requested a review from evetion February 1, 2026 21:44
start_idx, end_idx = 1, npoints
max_idx, max_dist = _find_max_squared_dist(points, start_idx, end_idx)
while i min(MIN_POINTS + 1, max_points) || (i < max_points && max_dist > max_tol)
while i < min(MIN_POINTS + 1, max_points) || (i < max_points && max_dist > max_tol)
Copy link
Member

Choose a reason for hiding this comment

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

This is a fix, but I'm not sure what it means in terms of the algorithm (should MIN_POINTS + 1 become MIN_POINTS?). There's also preserve_endpoint that extends the length with 1 when true, does that matter here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplify indexing bug

2 participants