diff options
| author | Dani Martin <[email protected]> | 2020-03-30 13:51:36 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-03-30 13:51:36 +0200 |
| commit | 62cdb2299bd2a9f96f854b06fee5e4429a5f9e25 (patch) | |
| tree | b26b3404c3d7ab81ade9978186e28081914eb71e /src/core.c | |
| parent | 9b66883e0b1278958420a7e6259402db15f3ac18 (diff) | |
| download | raylib-62cdb2299bd2a9f96f854b06fee5e4429a5f9e25.tar.gz raylib-62cdb2299bd2a9f96f854b06fee5e4429a5f9e25.zip | |
[cppcheck] Improvements in SaveStorageValue() in core.c (#1160)
* [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
* [cppcheck] Change functions header to accept only positive position in files
Changes:
* Functions SaveStorageValue(), LoadStorageValue() (core.c)
* Functions LoadFileData(), SaveFileData() (utils.c)
* Headers in raylib.h
Validation:
* Tested examples/core/core_storage_values.c
* Launched Unit Test for these functions
* Rerun CPPCHECK afer fix
Diffstat (limited to 'src/core.c')
| -rw-r--r-- | src/core.c | 46 |
1 files changed, 34 insertions, 12 deletions
@@ -16,7 +16,7 @@ * * #define PLATFORM_DESKTOP * Windowing and input system configured for desktop platforms: Windows, Linux, OSX, FreeBSD, OpenBSD, NetBSD, DragonFly -* NOTE: Oculus Rift CV1 requires PLATFORM_DESKTOP for mirror rendering - View [rlgl] module to enable it +* NOTE: Oculus Ritf CV1 requires PLATFORM_DESKTOP for mirror rendering - View [rlgl] module to enable it * * #define PLATFORM_ANDROID * Windowing and input system configured for Android device, app activity managed internally in this module. @@ -2192,7 +2192,7 @@ unsigned char *DecompressData(unsigned char *compData, int compDataLength, int * // Save integer value to storage file (to defined position) // NOTE: Storage positions is directly related to file memory layout (4 bytes each integer) -void SaveStorageValue(int position, int value) +void SaveStorageValue(unsigned int position, int value) { #if defined(SUPPORT_DATA_STORAGE) char path[512] = { 0 }; @@ -2204,31 +2204,53 @@ void SaveStorageValue(int position, int value) strcpy(path, STORAGE_DATA_FILE); #endif - int dataSize = 0; + unsigned int dataSize = 0; + unsigned 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 (%u) bigger than actual size of file [%s] (%u) 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; @@ -2242,7 +2264,7 @@ void SaveStorageValue(int position, int value) // Load integer value from storage file (from defined position) // NOTE: If requested position could not be found, value 0 is returned -int LoadStorageValue(int position) +int LoadStorageValue(unsigned int position) { int value = 0; #if defined(SUPPORT_DATA_STORAGE) @@ -2255,7 +2277,7 @@ int LoadStorageValue(int position) strcpy(path, STORAGE_DATA_FILE); #endif - int dataSize = 0; + unsigned int dataSize = 0; unsigned char *fileData = LoadFileData(path, &dataSize); if (fileData != NULL) |
