Skip to content

Commit

Permalink
Change: Use SlErrorCorrupt() on pool index error when loading a saveg…
Browse files Browse the repository at this point in the history
…ame, instead of terminating. (OpenTTD#7219)
  • Loading branch information
PeterN authored and douiwby committed Apr 16, 2020
1 parent 67c16ec commit 72693be
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/core/pool_func.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,20 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size)
* @param size size of item
* @param index index of item
* @return pointer to allocated item
* @note usererror() on failure! (index out of range or already used)
* @note SlErrorCorruptFmt() on failure! (index out of range or already used)
*/
DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index)
{
extern void NORETURN SlErrorCorruptFmt(const char *format, ...);

if (index >= Tmax_size) {
usererror("failed loading savegame: %s index " PRINTF_SIZE " out of range (" PRINTF_SIZE ")", this->name, index, Tmax_size);
SlErrorCorruptFmt("%s index " PRINTF_SIZE " out of range (" PRINTF_SIZE ")", this->name, index, Tmax_size);
}

if (index >= this->size) this->ResizeFor(index);

if (this->data[index] != NULL) {
usererror("failed loading savegame: %s index " PRINTF_SIZE " already in use", this->name, index);
SlErrorCorruptFmt("%s index " PRINTF_SIZE " already in use", this->name, index);
}

return this->AllocateItem(size, index);
Expand Down
19 changes: 19 additions & 0 deletions src/saveload/saveload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,25 @@ void NORETURN SlErrorCorrupt(const char *msg)
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
}

/**
* Issue an SlErrorCorrupt with a format string.
* @param format format string
* @param ... arguments to format string
* @note This function does never return as it throws an exception to
* break out of all the saveload code.
*/
void NORETURN SlErrorCorruptFmt(const char *format, ...)
{
va_list ap;
char msg[256];

va_start(ap, format);
vseprintf(msg, lastof(msg), format, ap);
va_end(ap);

SlErrorCorrupt(msg);
}


typedef void (*AsyncSaveFinishProc)(); ///< Callback for when the savegame loading is finished.
static AsyncSaveFinishProc _async_save_finish = NULL; ///< Callback to call when the savegame loading is finished.
Expand Down
1 change: 1 addition & 0 deletions src/saveload/saveload.h
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ void SlObject(void *object, const SaveLoad *sld);
bool SlObjectMember(void *object, const SaveLoad *sld);
void NORETURN SlError(StringID string, const char *extra_msg = NULL);
void NORETURN SlErrorCorrupt(const char *msg);
void NORETURN SlErrorCorruptFmt(const char *format, ...);

bool SaveloadCrashWithMissingNewGRFs();

Expand Down

0 comments on commit 72693be

Please sign in to comment.