summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorRay <[email protected]>2023-09-22 11:58:53 +0200
committerRay <[email protected]>2023-09-22 11:58:53 +0200
commita3a5aa7c639410f4ff8ed776d590f952653f5ed1 (patch)
tree76e56e5419955543841cfcf5f92854f23cb28d76 /src
parent83d82b6697204efa4601455ebb4beaef609bfc86 (diff)
downloadraylib-a3a5aa7c639410f4ff8ed776d590f952653f5ed1.tar.gz
raylib-a3a5aa7c639410f4ff8ed776d590f952653f5ed1.zip
REVIEWED: `LoadFileData()` potential issues with dataSize
Diffstat (limited to 'src')
-rw-r--r--src/utils.c29
1 files changed, 22 insertions, 7 deletions
diff --git a/src/utils.c b/src/utils.c
index 27747b75..51d84cfe 100644
--- a/src/utils.c
+++ b/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,
// using fseek() and ftell() could not work in some (rare) cases
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);
if (size > 0)
@@ -210,11 +210,24 @@ unsigned char *LoadFileData(const char *fileName, int *dataSize)
if (data != NULL)
{
// 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);
- *dataSize = count;
-
- if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
- else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
+ size_t count = fread(data, sizeof(unsigned char), size, file);
+
+ // WARNING: fread() returns a size_t value, usually 'unsigned int' (32bit compilation) and 'unsigned long long' (64bit compilation)
+ // 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);
}
@@ -254,7 +267,9 @@ bool SaveFileData(const char *fileName, void *data, int dataSize)
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);
else if (count != dataSize) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName);