REVIEWED: LoadFileData()
potential issues with dataSize
This commit is contained in:
parent
83d82b6697
commit
a3a5aa7c63
1 changed files with 21 additions and 6 deletions
27
src/utils.c
27
src/utils.c
|
@ -200,7 +200,7 @@ unsigned char *LoadFileData(const char *fileName, int *dataSize)
|
||||||
// WARNING: On binary streams SEEK_END could not be found,
|
// WARNING: On binary streams SEEK_END could not be found,
|
||||||
// using fseek() and ftell() could not work in some (rare) cases
|
// using fseek() and ftell() could not work in some (rare) cases
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file, 0, SEEK_END);
|
||||||
int size = ftell(file);
|
int size = ftell(file); // WARNING: ftell() returns 'long int', maximum size returned is INT_MAX (2147483647 bytes)
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
if (size > 0)
|
if (size > 0)
|
||||||
|
@ -210,11 +210,24 @@ unsigned char *LoadFileData(const char *fileName, int *dataSize)
|
||||||
if (data != NULL)
|
if (data != NULL)
|
||||||
{
|
{
|
||||||
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
// 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);
|
size_t count = fread(data, sizeof(unsigned char), size, file);
|
||||||
*dataSize = count;
|
|
||||||
|
|
||||||
if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
|
// WARNING: fread() returns a size_t value, usually 'unsigned int' (32bit compilation) and 'unsigned long long' (64bit compilation)
|
||||||
else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
// dataSize is unified along raylib as a 'int' type, so, for file-sizes > INT_MAX (2147483647 bytes) we have a limitation
|
||||||
|
if (count > 2147483647)
|
||||||
|
{
|
||||||
|
TRACELOG(LOG_WARNING, "FILEIO: [%s] File is bigger than 2147483647 bytes, avoid using LoadFileData()", fileName);
|
||||||
|
|
||||||
|
RL_FREE(data);
|
||||||
|
data = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*dataSize = (int)count;
|
||||||
|
|
||||||
|
if ((*dataSize) != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded (%i bytes out of %i)", fileName, dataSize, count);
|
||||||
|
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 allocated memory for file reading", fileName);
|
||||||
}
|
}
|
||||||
|
@ -254,7 +267,9 @@ bool SaveFileData(const char *fileName, void *data, int dataSize)
|
||||||
|
|
||||||
if (file != NULL)
|
if (file != NULL)
|
||||||
{
|
{
|
||||||
unsigned int count = (unsigned int)fwrite(data, sizeof(unsigned char), dataSize, file);
|
// WARNING: fwrite() returns a size_t value, usually 'unsigned int' (32bit compilation) and 'unsigned long long' (64bit compilation)
|
||||||
|
// and expects a size_t input value but as dataSize is limited to INT_MAX (2147483647 bytes), there shouldn't be a problem
|
||||||
|
int count = (int)fwrite(data, sizeof(unsigned char), dataSize, file);
|
||||||
|
|
||||||
if (count == 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write file", fileName);
|
if (count == 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write file", fileName);
|
||||||
else if (count != dataSize) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName);
|
else if (count != dataSize) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue