diff options
| author | Julien | 2011-06-04 03:43:16 +0800 |
|---|---|---|
| committer | Julien | 2011-06-23 15:11:36 +0800 |
| commit | 2f200ac49322ff8ccd13c5e8b7a22abbf6ff2610 (patch) | |
| tree | d22f6efae6ac234cbe0b73783aa8a8962a84a3b6 /engines/touche | |
| parent | a8b13e8a6be900995c85f08fd527750e2620c215 (diff) | |
| download | scummvm-rg350-2f200ac49322ff8ccd13c5e8b7a22abbf6ff2610.tar.gz scummvm-rg350-2f200ac49322ff8ccd13c5e8b7a22abbf6ff2610.tar.bz2 scummvm-rg350-2f200ac49322ff8ccd13c5e8b7a22abbf6ff2610.zip | |
ANALYSIS: Fix potential memory leak when using realloc
When reallocation is unsuccessful, the passed buffer is not freed. In this case, assigning the result (NULL) will result in a leak of the original memory buffer.
See http://msdn.microsoft.com/en-us/library/kkedhy7c.aspx
Diffstat (limited to 'engines/touche')
| -rw-r--r-- | engines/touche/resource.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/engines/touche/resource.cpp b/engines/touche/resource.cpp index 8f4752e912..6df6fc0e5f 100644 --- a/engines/touche/resource.cpp +++ b/engines/touche/resource.cpp @@ -468,14 +468,22 @@ void ToucheEngine::res_loadSprite(int num, int index) { if (size > spr->size) { debug(8, "Reallocating memory for sprite %d (index %d), %d bytes needed", num, index, size - spr->size); spr->size = size; - if (spr->ptr) { - spr->ptr = (uint8 *)realloc(spr->ptr, size); - } else { - spr->ptr = (uint8 *)malloc(size); - } - if (!spr->ptr) { - error("Unable to reallocate memory for sprite %d (%d bytes)", num, size); + + uint8 *buffer = NULL; + if (spr->ptr) + buffer = (uint8 *)realloc(spr->ptr, size); + + if (!buffer) { + // Free previously allocated sprite (when realloc failed) + free(spr->ptr); + + buffer = (uint8 *)malloc(size); } + + if (!buffer) + error("[ToucheEngine::res_loadSprite] Unable to reallocate memory for sprite %d (%d bytes)", num, size); + + spr->ptr = buffer; } for (int i = 0; i < _currentImageHeight; ++i) { res_decodeScanLineImageRLE(spr->ptr + _currentImageWidth * i, _currentImageWidth); |
