From 3789752a5d470175927c6f45003126334e199530 Mon Sep 17 00:00:00 2001 From: danimartin82 Date: Thu, 26 Mar 2020 15:21:28 +0100 Subject: [PATCH] [cppcheck] Improvements in SaveStorageValue() in core.c in file core.c cppcheck shows errors only in function SaveStorageValue(): * Common realloc mistake: 'fileData' nulled but not freed upon failure * Memory pointed to by 'fileData' is freed twice. Validation: * Tested examples/core/core_storage_values.c * Launched Unit Test for this function * Rerun CPPCHECK afer fix --- src/core.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/core.c b/src/core.c index 40f20030a..b33a0015f 100644 --- a/src/core.c +++ b/src/core.c @@ -2205,30 +2205,52 @@ void SaveStorageValue(int position, int value) #endif int dataSize = 0; + int newDataSize = 0; unsigned char *fileData = LoadFileData(path, &dataSize); + unsigned char *newFileData = NULL; if (fileData != NULL) { if (dataSize <= (position*sizeof(int))) { // Increase data size up to position and store value - dataSize = (position + 1)*sizeof(int); - fileData = (unsigned char *)RL_REALLOC(fileData, dataSize); - int *dataPtr = (int *)fileData; - dataPtr[position] = value; + newDataSize = (position + 1)*sizeof(int); + newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize); + + if (newFileData != NULL) + { + // RL_REALLOC succeded + int *dataPtr = (int *)newFileData; + dataPtr[position] = value; + } + else + { + // RL_REALLOC failed + TRACELOG(LOG_WARNING, "FILEIO: Position in bytes (%d) bigger than actual size of file [%s] (%d) Realloc function FAIL",position*sizeof(int),path,dataSize); + + // We store the old size of the file. + newFileData=fileData; + newDataSize=dataSize; + } + } else { + // We store the old size of the file. + newFileData=fileData; + newDataSize=dataSize; + // Replace value on selected position - int *dataPtr = (int *)fileData; + int *dataPtr = (int *)newFileData; dataPtr[position] = value; } - SaveFileData(path, fileData, dataSize); - RL_FREE(fileData); + SaveFileData(path, newFileData, newDataSize); + RL_FREE(newFileData); } else { + TRACELOG(LOG_INFO, "FILEIO: [%s] File not found, creating it.",path); dataSize = (position + 1)*sizeof(int); fileData = (unsigned char *)RL_MALLOC(dataSize); int *dataPtr = (int *)fileData;