1
0
Fork 0

(svn r16808) -Fix (r16338): don't crash when loading savegame with invalid references

release/1.0
smatz 2009-07-13 11:09:02 +00:00
parent a67f861d55
commit aa7bcb1259
1 changed files with 28 additions and 8 deletions

View File

@ -1135,6 +1135,8 @@ static void SlLoadChunks()
} }
} }
static const char *_sl_ptrs_error; ///< error message if there was an error during fixing pointers, NULL otherwise
/** Fix all pointers (convert index -> pointer) */ /** Fix all pointers (convert index -> pointer) */
static void SlFixPointers() static void SlFixPointers()
{ {
@ -1142,6 +1144,7 @@ static void SlFixPointers()
const ChunkHandler * const *chsc; const ChunkHandler * const *chsc;
_sl.action = SLA_PTRS; _sl.action = SLA_PTRS;
_sl_ptrs_error = NULL;
DEBUG(sl, 1, "Fixing pointers"); DEBUG(sl, 1, "Fixing pointers");
@ -1157,6 +1160,9 @@ static void SlFixPointers()
} }
} }
/* We need to fix all possible pointers even if there were invalid ones. This way pool cleaning will work fine. */
if (_sl_ptrs_error != NULL) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, _sl_ptrs_error);
DEBUG(sl, 1, "All pointers fixed"); DEBUG(sl, 1, "All pointers fixed");
assert(_sl.action == SLA_PTRS); assert(_sl.action == SLA_PTRS);
@ -1520,41 +1526,55 @@ static void *IntToReference(size_t index, SLRefType rt)
switch (rt) { switch (rt) {
case REF_ORDERLIST: case REF_ORDERLIST:
if (OrderList::IsValidID(index)) return OrderList::Get(index); if (OrderList::IsValidID(index)) return OrderList::Get(index);
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Referencing invalid OrderList"); _sl_ptrs_error = "Referencing invalid OrderList";
break;
case REF_ORDER: case REF_ORDER:
if (Order::IsValidID(index)) return Order::Get(index); if (Order::IsValidID(index)) return Order::Get(index);
/* in old versions, invalid order was used to mark end of order list */ /* in old versions, invalid order was used to mark end of order list */
if (CheckSavegameVersionOldStyle(5, 2)) return NULL; if (CheckSavegameVersionOldStyle(5, 2)) return NULL;
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Referencing invalid Order"); _sl_ptrs_error = "Referencing invalid Order";
break;
case REF_VEHICLE_OLD: case REF_VEHICLE_OLD:
case REF_VEHICLE: case REF_VEHICLE:
if (Vehicle::IsValidID(index)) return Vehicle::Get(index); if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Referencing invalid Vehicle"); _sl_ptrs_error = "Referencing invalid Vehicle";
break;
case REF_STATION: case REF_STATION:
if (Station::IsValidID(index)) return Station::Get(index); if (Station::IsValidID(index)) return Station::Get(index);
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Referencing invalid Station"); _sl_ptrs_error = "Referencing invalid Station";
break;
case REF_TOWN: case REF_TOWN:
if (Town::IsValidID(index)) return Town::Get(index); if (Town::IsValidID(index)) return Town::Get(index);
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Referencing invalid Town"); _sl_ptrs_error = "Referencing invalid Town";
break;
case REF_ROADSTOPS: case REF_ROADSTOPS:
if (RoadStop::IsValidID(index)) return RoadStop::Get(index); if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Referencing invalid RoadStop"); _sl_ptrs_error = "Referencing invalid RoadStop";
break;
case REF_ENGINE_RENEWS: case REF_ENGINE_RENEWS:
if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index); if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Referencing invalid EngineRenew"); _sl_ptrs_error = "Referencing invalid EngineRenew";
break;
case REF_CARGO_PACKET: case REF_CARGO_PACKET:
if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index); if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Referencing invalid CargoPacket"); _sl_ptrs_error = "Referencing invalid CargoPacket";
break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
/* Print a debug message about each invalid reference */
DEBUG(sl, 1, "%s (index = " PRINTF_SIZE ")", _sl_ptrs_error, index);
/* Return NULL for broken savegames */
return NULL;
} }
/** The format for a reader/writer type of a savegame */ /** The format for a reader/writer type of a savegame */