erincatto / box2d

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

Double free crash on b2Array_Grow #767

Open onehundredfeet opened 1 month ago

onehundredfeet commented 1 month 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 1 month 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 1 month 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 1 month ago

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