[Add] Prevention in case of pilot_stack reallocation.
This commit is contained in:
parent
d649330e5b
commit
9ea3f985d2
16
src/pilot.c
16
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,
|
unsigned int pilot_create(Ship* ship, char* name, Faction* faction,
|
||||||
AI_Profile* ai, const double dir, const Vec2* pos,
|
AI_Profile* ai, const double dir, const Vec2* pos,
|
||||||
const Vec2* vel, const int flags) {
|
const Vec2* vel, const int flags) {
|
||||||
Pilot* dyn = MALLOC_L(Pilot);
|
|
||||||
|
Pilot** tp, *dyn;
|
||||||
|
|
||||||
|
dyn = MALLOC_L(Pilot);
|
||||||
if(dyn == NULL) {
|
if(dyn == NULL) {
|
||||||
WARN("Unable to allocate memory.");
|
WARN("Unable to allocate memory.");
|
||||||
return 0;
|
return 0;
|
||||||
@ -730,8 +733,15 @@ unsigned int pilot_create(Ship* ship, char* name, Faction* faction,
|
|||||||
// Add to the stack.
|
// Add to the stack.
|
||||||
pilots++; // There is a new pilot.
|
pilots++; // There is a new pilot.
|
||||||
|
|
||||||
if(pilots >= mpilots) // Need to grow.
|
if(pilots >= mpilots) {
|
||||||
pilot_stack = realloc(pilot_stack, ++mpilots*sizeof(Pilot*));
|
// 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;
|
pilot_stack[pilots-1] = dyn;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user