[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
This commit is contained in:
parent
41582f0448
commit
3789752a5d
36
src/core.c
36
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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user