Update getBodiesUnsafe() to return a slice of size getMaxBodies()#25
Update getBodiesUnsafe() to return a slice of size getMaxBodies()#25r0ckHopper wants to merge 6 commits into
Conversation
getBodiesUnsafe() returned ptr[0..getNumBodies()] but its values can go upto 1024 (getMaxBodies) After destroying bodies, the active count shrinks but remaining bodies stay at the same slots, causing out-of-bounds exceptions. The underlying Jolt array also uses max_bodies, so slicing to getMaxBodies() is safe.
Same issue as addressed in commit 77a907d getBodiesMutUnsafe() returned ptr[0..getNumBodies()] but its values can go upto 1024 (getMaxBodies) After destroying bodies, the active count shrinks but remaining bodies stay at the same slots, causing out-of-bounds exceptions. The underlying Jolt array also uses max_bodies, so slicing to getMaxBodies() is safe.
There was a problem hiding this comment.
Pull request overview
This PR fixes an API mismatch where PhysicsSystem.getBodiesUnsafe() / getBodiesMutUnsafe() returned a slice sized to getNumBodies() even though the underlying Jolt buffer is sized to max_bodies, which could cause out-of-bounds indexing when accessing bodies by BodyId after deletions create holes.
Changes:
- Return a
getMaxBodies()-sized slice fromgetBodiesUnsafe()andgetBodiesMutUnsafe(). - Add a regression test covering the “destroy middle body then index by
BodyId” scenario.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@hazeycode I think my response to the comment will need human input. |
|
Yeah it's fine as it is for now. |
|
Updates the the doc comments as well. Should be good to merge now unless you wanna tweak the wording. |
Currently getBodiesUnsafe() returns a slice of size getNumBodies() but getBodiesUnsafe() actually returns a pointer to slice of size max_bodies.
The issue comes when you delete bodies from the middle and try to access bodies of higher index
getBodiesUnsafe does not reorganize the elements before giving a slice so you can get an out of bounds error
example of the issue is:
let bodies be
[b0, b1, b2]
if you delete b1 jolt physics updates bodies to
[b0, invalid, b2]
but if you call getBodiesUnsafe you only get
[b0, invalid]
so if you do tryGetBody( all_bodies, b2) then it will look for b2 at index 2 which is out of bounds