erincatto / box2d

Box2D is a 2D physics engine for games
https://box2d.org
MIT License
8.36k stars 1.54k forks source link

Double free crash on b2Array_Grow #767

Closed onehundredfeet closed 3 months ago

onehundredfeet commented 6 months ago

Make sure these boxes are checked before submitting your issue - thank you!

I'm getting a crash on b2Array_Grow. The capacity of the array coming in is 0 and I'm getting a double free error. I'm going to continue debugging and see if I can figure it out. But in case it's obvious to anyone here I wanted to put this up.

onehundredfeet commented 6 months ago

It seems to be when it's pushing an event. The array that gets passed in has already been freed on the second push.

onehundredfeet commented 6 months ago

found the bug. Preparing a PR. It was

if (hit == true)
                {
                    event.normal = contactSim->manifold.normal;

                    b2CheckId(world->shapeArray, contactSim->shapeIdA);
                    b2CheckId(world->shapeArray, contactSim->shapeIdB);
                    b2Shape* shapeA = world->shapeArray + contactSim->shapeIdA;
                    b2Shape* shapeB = world->shapeArray + contactSim->shapeIdB;

                    event.shapeIdA = (b2ShapeId){shapeA->id + 1, world->worldId, shapeA->revision};
                    event.shapeIdB = (b2ShapeId){shapeB->id + 1, world->worldId, shapeB->revision};

                    b2Array_Push(events, event);
                }

Events is a temporary variable. it needs to be:

if (hit == true)
                {
                    event.normal = contactSim->manifold.normal;

                    b2CheckId(world->shapeArray, contactSim->shapeIdA);
                    b2CheckId(world->shapeArray, contactSim->shapeIdB);
                    b2Shape* shapeA = world->shapeArray + contactSim->shapeIdA;
                    b2Shape* shapeB = world->shapeArray + contactSim->shapeIdB;

                    event.shapeIdA = (b2ShapeId){shapeA->id + 1, world->worldId, shapeA->revision};
                    event.shapeIdB = (b2ShapeId){shapeB->id + 1, world->worldId, shapeB->revision};

                    b2Array_Push(world->contactHitArray, event);
                }
onehundredfeet commented 6 months ago

https://github.com/erincatto/box2c/pull/166

erincatto commented 3 months ago

This was fixed. Thanks for the report!