From 0e7abce271c218faa6ae19059207fed5d61b0ef8 Mon Sep 17 00:00:00 2001 From: Andrew Kurushin Date: Tue, 19 Oct 2010 15:31:07 +0000 Subject: SAGA: fix submit 53486 "Added sanity checks for realloc() calls - bug report #3087852". zero count realloc may return NULL as valid value svn-id: r53614 --- engines/saga/actor.cpp | 10 ++++++---- engines/saga/saga.cpp | 10 ++++++---- engines/saga/shorten.cpp | 15 +++++++++------ engines/saga/sprite.cpp | 7 ++----- 4 files changed, 23 insertions(+), 19 deletions(-) (limited to 'engines') diff --git a/engines/saga/actor.cpp b/engines/saga/actor.cpp index d44de0d75e..6e6707c2b5 100644 --- a/engines/saga/actor.cpp +++ b/engines/saga/actor.cpp @@ -149,10 +149,11 @@ void ActorData::setTileDirectionsSize(int size, bool forceRealloc) { } _tileDirectionsAlloced = size; byte *tmp = (byte*)realloc(_tileDirections, _tileDirectionsAlloced * sizeof(*_tileDirections)); - if (tmp) + if ((tmp != NULL) || (_tileDirectionsAlloced == 0)) { _tileDirections = tmp; - else + } else { error("ActorData::setTileDirectionsSize(): Error while reallocating memory"); + } } void ActorData::cycleWrap(int cycleLimit) { @@ -166,10 +167,11 @@ void ActorData::setWalkStepsPointsSize(int size, bool forceRealloc) { } _walkStepsAlloced = size; Point *tmp = (Point*)realloc(_walkStepsPoints, _walkStepsAlloced * sizeof(*_walkStepsPoints)); - if (tmp) + if ((tmp != NULL) || (_walkStepsAlloced == 0)) { _walkStepsPoints = tmp; - else + } else { error("ActorData::setWalkStepsPointsSize(): Error while reallocating memory"); + } } void ActorData::addWalkStepPoint(const Point &point) { diff --git a/engines/saga/saga.cpp b/engines/saga/saga.cpp index 13567f67b5..1643ecfa17 100644 --- a/engines/saga/saga.cpp +++ b/engines/saga/saga.cpp @@ -426,10 +426,11 @@ void SagaEngine::loadStrings(StringsTable &stringsTable, const byte *stringsPoin if (offset == stringsLength) { stringsCount = i; const char **tmp = (const char **)realloc(stringsTable.strings, stringsCount * sizeof(*stringsTable.strings)); - if (tmp) + if ((tmp != NULL) || (stringsCount == 0)) { stringsTable.strings = tmp; - else + } else { error("SagaEngine::loadStrings() Error while reallocating memory"); + } break; } if (offset > stringsLength) { @@ -438,10 +439,11 @@ void SagaEngine::loadStrings(StringsTable &stringsTable, const byte *stringsPoin warning("SagaEngine::loadStrings wrong strings table"); stringsCount = i; const char **tmp = (const char **)realloc(stringsTable.strings, stringsCount * sizeof(*stringsTable.strings)); - if (tmp) + if ((tmp != NULL) || (stringsCount == 0)) { stringsTable.strings = tmp; - else + } else { error("SagaEngine::loadStrings() Error while reallocating memory"); + } break; } stringsTable.strings[i] = (const char *)stringsTable.stringsPointer + offset; diff --git a/engines/saga/shorten.cpp b/engines/saga/shorten.cpp index 684b3a86bb..8353c7dbad 100644 --- a/engines/saga/shorten.cpp +++ b/engines/saga/shorten.cpp @@ -367,10 +367,11 @@ byte *loadShortenFromStream(Common::ReadStream &stream, int &size, int &rate, by warning("Safeguard: maxLPC < lpcNum (should never happen)"); maxLPC = lpcNum; int32 *tmp = (int32 *) realloc(lpc, maxLPC * 4); - if (tmp) + if ((tmp != NULL) || (maxLPC == 0)) { lpc = tmp; - else + } else { error("loadShortenFromStream(): Error while reallocating memory"); + } } for (i = 0; i < lpcNum; i++) @@ -435,10 +436,11 @@ byte *loadShortenFromStream(Common::ReadStream &stream, int &size, int &rate, by prevSize = size; size += (blockSize * dataSize); byte *tmp = (byte *) realloc(unpackedBuffer, size); - if (tmp) + if ((tmp != NULL) || (size == 0)) { unpackedBuffer = tmp; - else + } else { error("loadShortenFromStream(): Error while reallocating memory"); + } pBuf = unpackedBuffer + prevSize; if (flags & Audio::FLAG_16BITS) { @@ -473,10 +475,11 @@ byte *loadShortenFromStream(Common::ReadStream &stream, int &size, int &rate, by prevSize = size; size += vLen; byte *tmp = (byte *) realloc(unpackedBuffer, size); - if (tmp) + if ((tmp != NULL) || (size == 0)) { unpackedBuffer = tmp; - else + } else { error("loadShortenFromStream(): Error while reallocating memory"); + } pBuf = unpackedBuffer + prevSize; while (vLen--) { diff --git a/engines/saga/sprite.cpp b/engines/saga/sprite.cpp index 969faae737..a62f7c57eb 100644 --- a/engines/saga/sprite.cpp +++ b/engines/saga/sprite.cpp @@ -116,13 +116,10 @@ void Sprite::loadList(int resourceId, SpriteList &spriteList) { newSpriteCount = spriteList.spriteCount + spriteCount; SpriteInfo *tmp = (SpriteInfo *)realloc(spriteList.infoList, newSpriteCount * sizeof(*spriteList.infoList)); - if (tmp) + if ((tmp != NULL) || (newSpriteCount == 0)) { spriteList.infoList = tmp; - else + } else { error("Sprite::loadList(): Error while reallocating memory"); - - if (spriteList.infoList == NULL) { - memoryError("Sprite::loadList"); } spriteList.spriteCount = newSpriteCount; -- cgit v1.2.3