[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
|
#endif
|
||||||
|
|
||||||
int dataSize = 0;
|
int dataSize = 0;
|
||||||
|
int newDataSize = 0;
|
||||||
unsigned char *fileData = LoadFileData(path, &dataSize);
|
unsigned char *fileData = LoadFileData(path, &dataSize);
|
||||||
|
unsigned char *newFileData = NULL;
|
||||||
|
|
||||||
if (fileData != NULL)
|
if (fileData != NULL)
|
||||||
{
|
{
|
||||||
if (dataSize <= (position*sizeof(int)))
|
if (dataSize <= (position*sizeof(int)))
|
||||||
{
|
{
|
||||||
// Increase data size up to position and store value
|
// Increase data size up to position and store value
|
||||||
dataSize = (position + 1)*sizeof(int);
|
newDataSize = (position + 1)*sizeof(int);
|
||||||
fileData = (unsigned char *)RL_REALLOC(fileData, dataSize);
|
newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize);
|
||||||
int *dataPtr = (int *)fileData;
|
|
||||||
dataPtr[position] = value;
|
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
|
else
|
||||||
{
|
{
|
||||||
|
// We store the old size of the file.
|
||||||
|
newFileData=fileData;
|
||||||
|
newDataSize=dataSize;
|
||||||
|
|
||||||
// Replace value on selected position
|
// Replace value on selected position
|
||||||
int *dataPtr = (int *)fileData;
|
int *dataPtr = (int *)newFileData;
|
||||||
dataPtr[position] = value;
|
dataPtr[position] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
SaveFileData(path, fileData, dataSize);
|
SaveFileData(path, newFileData, newDataSize);
|
||||||
RL_FREE(fileData);
|
RL_FREE(newFileData);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
TRACELOG(LOG_INFO, "FILEIO: [%s] File not found, creating it.",path);
|
||||||
dataSize = (position + 1)*sizeof(int);
|
dataSize = (position + 1)*sizeof(int);
|
||||||
fileData = (unsigned char *)RL_MALLOC(dataSize);
|
fileData = (unsigned char *)RL_MALLOC(dataSize);
|
||||||
int *dataPtr = (int *)fileData;
|
int *dataPtr = (int *)fileData;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user