Fix off-by-one indexing bug in DouglasPeucker simplify#387
Open
asinghvi17 wants to merge 1 commit intomainfrom
Open
Fix off-by-one indexing bug in DouglasPeucker simplify#387asinghvi17 wants to merge 1 commit intomainfrom
asinghvi17 wants to merge 1 commit intomainfrom
Conversation
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>
evetion
reviewed
Feb 2, 2026
| 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) |
Member
There was a problem hiding this comment.
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
numberorratiovaluesProblem
In the DouglasPeucker
_simplifyfunction, the while loop condition:allowed
ito equalmax_pointsand enter the loop. Theni += 1would makei = max_points + 1, causingresults[i]to be out of bounds since the array is sized for exactlymax_pointselements.Fix
Changed
i ≤toi <in the first part of the condition:Test plan
Fixes #386
🤖 Generated with Claude Code