Skip to content

Conversation

@MadsWedendahlKruse
Copy link

Summary

This PR fixes a potential out-of-bounds panic in Path::path_with_height that can occur when the path tightly follows polygon edges or corners. In certain cases, the next_i index can become equal to self.path.len() after the last waypoint is processed, but before the method exits. The next line then tries to access self.path[next_i], causing an index-out-of-bounds panic. I came across with problem while using polyanya for a game navigation system.

Details

The issue stems from this condition:

if self.path.len() < next_i {
    // TODO: shouldn't happen
    break;
}

When next_i == self.path.len(), the comparison is false (len() < next_i), so execution continues and self.path[next_i] is accessed, which is invalid.
Changing the check to next_i >= self.path.len() ensures we break out safely once all waypoints have been consumed.

Fix

- if self.path.len() < next_i {
-     // TODO: shouldn't happen
-     break;
- }
+ // Guard: stop once we've consumed all waypoints.
+ if next_i >= self.path.len() {
+     break;
+ }

Impact

Prevents runtime panics when computing heighted paths.

No API or behavioral changes except improved stability.

Verified in downstream projects using polyanya for Recast navmesh height sampling (previously mentioned game navigation).

Additional Notes

The existing comment // TODO: shouldn't happen suggests the author already anticipated this case; this patch simply codifies the intended safeguard.

@MadsWedendahlKruse MadsWedendahlKruse changed the title Title: Fix out-of-bounds panic in Path::path_with_height when next_i == self.path.len() Fix out-of-bounds panic in Path::path_with_height when next_i == self.path.len() Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant