From 9ea3f985d26552553c5177f34a124db3b494c3d9 Mon Sep 17 00:00:00 2001 From: Allanis Date: Wed, 20 Mar 2013 21:27:57 +0000 Subject: [PATCH] [Add] Prevention in case of pilot_stack reallocation. --- src/pilot.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/pilot.c b/src/pilot.c index 41bb6fc..8ecce72 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -711,7 +711,10 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, Faction* faction, unsigned int pilot_create(Ship* ship, char* name, Faction* faction, AI_Profile* ai, const double dir, const Vec2* pos, const Vec2* vel, const int flags) { - Pilot* dyn = MALLOC_L(Pilot); + + Pilot** tp, *dyn; + + dyn = MALLOC_L(Pilot); if(dyn == NULL) { WARN("Unable to allocate memory."); return 0; @@ -729,9 +732,16 @@ unsigned int pilot_create(Ship* ship, char* name, Faction* faction, } else { // Add to the stack. pilots++; // There is a new pilot. - - if(pilots >= mpilots) // Need to grow. - pilot_stack = realloc(pilot_stack, ++mpilots*sizeof(Pilot*)); + + if(pilots >= mpilots) { + // Need to grow. About 20 at a time. + mpilots += 20; + tp = pilot_stack; + pilot_stack = realloc(pilot_stack, mpilots*sizeof(Pilot*)); + if((pilot_stack != tp) && player) + // Take into account possible mem move. + player = pilot_stack[0]; + } pilot_stack[pilots-1] = dyn; }