Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change: Use SlErrorCorrupt() on pool index error when loading a saveg…
…ame, instead of terminating.
  • Loading branch information
PeterN committed Feb 11, 2019
1 parent cac2f62 commit c61d7bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/core/pool_func.cpp
Expand Up @@ -12,6 +12,10 @@
#include "../stdafx.h"
#include "pool_type.hpp"

#include "../openttd.h"
#include "../string_func.h"
#include "../saveload/saveload.h"

#include "../safeguards.h"

/**
Expand All @@ -38,3 +42,23 @@
if (pool->type & pt) pool->CleanPool();
}
}

/* static */ void NORETURN PoolBase::IndexRangeError(const char *name, size_t index, size_t pool_max)
{
char msg[256];
seprintf(msg, lastof(msg), "%s index " PRINTF_SIZE " out of range (" PRINTF_SIZE ")", name, index, pool_max);

if (_switch_mode == SM_LOAD_GAME || _switch_mode == SM_LOAD_SCENARIO) SlErrorCorrupt(msg);

usererror("%s", msg);
}

/* static */ void NORETURN PoolBase::IndexUsedError(const char *name, size_t index)
{
char msg[256];
seprintf(msg, lastof(msg), "%s index " PRINTF_SIZE " already in use", name, index);

if (_switch_mode == SM_LOAD_GAME || _switch_mode == SM_LOAD_SCENARIO) SlErrorCorrupt(msg);

usererror("%s", msg);
}
6 changes: 3 additions & 3 deletions src/core/pool_func.hpp
Expand Up @@ -152,18 +152,18 @@ 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 SlErrorCorrupt() on failure! (index out of range or already used)
*/
DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index)
{
if (index >= Tmax_size) {
usererror("failed loading savegame: %s index " PRINTF_SIZE " out of range (" PRINTF_SIZE ")", this->name, index, Tmax_size);
PoolBase::IndexRangeError(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);
PoolBase::IndexUsedError(this->name, index);
}

return this->AllocateItem(size, index);
Expand Down
4 changes: 4 additions & 0 deletions src/core/pool_type.hpp
Expand Up @@ -44,6 +44,10 @@ struct PoolBase {

static void Clean(PoolType);

static void NORETURN IndexRangeError(const char *name, size_t index, size_t pool_max);

static void NORETURN IndexUsedError(const char *name, size_t index);

/**
* Constructor registers this object in the pool vector.
* @param pt type of this pool.
Expand Down

0 comments on commit c61d7bc

Please sign in to comment.