Added security check to file reading (memory allocations)
This commit is contained in:
parent
2937f2010c
commit
bf69b38056
1 changed files with 21 additions and 12 deletions
33
src/utils.c
33
src/utils.c
|
@ -207,12 +207,16 @@ unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
|
||||||
{
|
{
|
||||||
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
|
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
|
||||||
|
|
||||||
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
if (data != NULL)
|
||||||
unsigned int count = (unsigned int)fread(data, sizeof(unsigned char), size, file);
|
{
|
||||||
*bytesRead = count;
|
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
||||||
|
unsigned int count = (unsigned int)fread(data, sizeof(unsigned char), size, file);
|
||||||
|
*bytesRead = count;
|
||||||
|
|
||||||
if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
|
if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
|
||||||
else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
||||||
|
}
|
||||||
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to allocated memory for file reading", fileName);
|
||||||
}
|
}
|
||||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
||||||
|
|
||||||
|
@ -344,16 +348,21 @@ char *LoadFileText(const char *fileName)
|
||||||
if (size > 0)
|
if (size > 0)
|
||||||
{
|
{
|
||||||
text = (char *)RL_MALLOC((size + 1)*sizeof(char));
|
text = (char *)RL_MALLOC((size + 1)*sizeof(char));
|
||||||
unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
|
|
||||||
|
if (text != NULL)
|
||||||
|
{
|
||||||
|
unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
|
||||||
|
|
||||||
// WARNING: \r\n is converted to \n on reading, so,
|
// WARNING: \r\n is converted to \n on reading, so,
|
||||||
// read bytes count gets reduced by the number of lines
|
// read bytes count gets reduced by the number of lines
|
||||||
if (count < size) text = RL_REALLOC(text, count + 1);
|
if (count < size) text = RL_REALLOC(text, count + 1);
|
||||||
|
|
||||||
// Zero-terminate the string
|
// Zero-terminate the string
|
||||||
text[count] = '\0';
|
text[count] = '\0';
|
||||||
|
|
||||||
TRACELOG(LOG_INFO, "FILEIO: [%s] Text file loaded successfully", fileName);
|
TRACELOG(LOG_INFO, "FILEIO: [%s] Text file loaded successfully", fileName);
|
||||||
|
}
|
||||||
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to allocated memory for file reading", fileName);
|
||||||
}
|
}
|
||||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName);
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue