Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ponyo877
Copy link
Contributor

@ponyo877 ponyo877 commented Oct 25, 2025

Summary

Fixes an index out of range panic in ProcessComponents() when SleepTimeThreshold != INFINITY.

Solution

Revert the for loop to its pre-modernization pattern. This loop requires manual index management because continue is used to skip the increment when bodies are deactivated.

Before PR #43:

for i := 0; i < len(space.dynamicBodies); {
    // ...
    if condition {
        continue  // Skip i++
    }
    i++
}

After PR #43 (bug):

for i := range space.dynamicBodies {
    // ...
    i++  // Double increment
}

This fix:
Reverts to the original pattern.

Testing

All tests pass.

Affected versions: v2.2.0, v2.3.0

This fixes a regression introduced in commit f1e35a6 (modernize for loops 2).

When modernizing the for loop from traditional style to range syntax,
the manual i++ increment was not removed, causing a double-increment bug.

The for..range statement automatically increments the index variable,
so the manual i++ at line 606 caused the loop to skip indices and
eventually panic with 'index out of range'.

Fixes the panic that occurs when SleepTimeThreshold != INFINITY.
@ponyo877 ponyo877 changed the title Fix index out of range panic in ProcessComponents fix index out of range panic in ProcessComponents Oct 25, 2025
Instead of removing the manual i++ increment, revert the for loop to its
original pattern before PR jakecoffman#43. This maintains consistency with the
pre-modernization code structure where loops with conditional continue
statements use manual index management.

The original pattern was:
for i := 0; i < len(space.dynamicBodies); {
    if condition {
        continue  // Skip i++
    }
    i++  // Increment only when not skipping
}
Copy link
Owner

@jakecoffman jakecoffman left a comment

Choose a reason for hiding this comment

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

Thanks for the fix. Reproducible with the contact graph example.

@jakecoffman jakecoffman merged commit 5cffd21 into jakecoffman:master Oct 25, 2025
1 check passed
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.

2 participants