[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
TRACELOG(LOG_WARNING,"FILEIO: Position in bytes (%u) bigger than actual size of file [%s] (%u) Realloc function FAIL",position*sizeof(int),path,dataSize);
//Westoretheoldsizeofthefile.
newFileData=fileData;
newDataSize=dataSize;
}
}
else
{
//Westoretheoldsizeofthefile.
newFileData=fileData;
newDataSize=dataSize;
//Replacevalueonselectedposition
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=(unsignedchar*)RL_MALLOC(dataSize);
int*dataPtr=(int*)fileData;
@ -2242,7 +2264,7 @@ void SaveStorageValue(int position, int value)