Skip to content

Commit 168e2c4

Browse files
zhaob1nHuang Zhaobin
andauthored
fix(rcore): correct string boundary handling (#5812)
Co-authored-by: Huang Zhaobin <xcpky@proton.me>
1 parent 638c1cb commit 168e2c4

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

src/rcore.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,7 @@ unsigned char *LoadFileData(const char *fileName, int *dataSize)
20042004
{
20052005
*dataSize = (int)count;
20062006

2007-
if ((*dataSize) != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded (%i bytes out of %i)", fileName, dataSize, count);
2007+
if ((*dataSize) != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded (%i bytes out of %i)", fileName, *dataSize, size);
20082008
else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
20092009
}
20102010
}
@@ -2365,8 +2365,7 @@ bool IsFileExtension(const char *fileName, const char *ext)
23652365
{
23662366
int fileExtLength = (int)strlen(fileExt);
23672367
char fileExtLower[16] = { 0 };
2368-
char *fileExtLowerPtr = fileExtLower;
2369-
for (int i = 0; (i < fileExtLength) && (i < 16); i++)
2368+
for (int i = 0; (i < fileExtLength) && (i < 15); i++)
23702369
{
23712370
// Copy and convert to lower-case
23722371
if ((fileExt[i] >= 'A') && (fileExt[i] <= 'Z')) fileExtLower[i] = fileExt[i] + 32;
@@ -2377,7 +2376,7 @@ bool IsFileExtension(const char *fileName, const char *ext)
23772376
int extLength = (int)strlen(ext);
23782377
char *extList = (char *)RL_CALLOC(extLength + 1, 1);
23792378
char *extListPtrs[MAX_FILE_EXTENSIONS] = { 0 };
2380-
strncpy(extList, ext, extLength);
2379+
memcpy(extList, ext, extLength);
23812380
extListPtrs[0] = extList;
23822381

23832382
for (int i = 0; i < extLength; i++)
@@ -2386,19 +2385,23 @@ bool IsFileExtension(const char *fileName, const char *ext)
23862385
if ((extList[i] >= 'A') && (extList[i] <= 'Z')) extList[i] += 32;
23872386

23882387
// Get pointer to next extension and add null-terminator
2389-
if ((extList[i] == ';') && (extCount < (MAX_FILE_EXTENSIONS - 1)))
2388+
if (extList[i] == ';')
23902389
{
23912390
extList[i] = '\0';
2392-
extListPtrs[extCount] = extList + i + 1;
2393-
extCount++;
2391+
2392+
if (extCount < MAX_FILE_EXTENSIONS)
2393+
{
2394+
extListPtrs[extCount] = extList + i + 1;
2395+
extCount++;
2396+
}
23942397
}
23952398
}
23962399

23972400
for (int i = 0; i < extCount; i++)
23982401
{
23992402
// Consider the case where extension provided
24002403
// does not start with the '.'
2401-
fileExtLowerPtr = fileExtLower;
2404+
char *fileExtLowerPtr = fileExtLower;
24022405
if (extListPtrs[i][0] != '.') fileExtLowerPtr++;
24032406

24042407
if (strcmp(fileExtLowerPtr, extListPtrs[i]) == 0)
@@ -2611,7 +2614,7 @@ const char *GetWorkingDirectory(void)
26112614
static char currentDir[MAX_FILEPATH_LENGTH] = { 0 };
26122615
memset(currentDir, 0, MAX_FILEPATH_LENGTH);
26132616

2614-
char *path = GETCWD(currentDir, MAX_FILEPATH_LENGTH - 1);
2617+
char *path = GETCWD(currentDir, MAX_FILEPATH_LENGTH);
26152618

26162619
return path;
26172620
}
@@ -2958,9 +2961,9 @@ unsigned int GetDirectoryFileCountEx(const char *basePath, const char *filter, b
29582961
{
29592962
// Construct new path from our base path
29602963
#if defined(_WIN32)
2961-
int pathLength = snprintf(path, MAX_FILEPATH_LENGTH - 1, "%s\\%s", basePath, entity->d_name);
2964+
int pathLength = snprintf(path, MAX_FILEPATH_LENGTH, "%s\\%s", basePath, entity->d_name);
29622965
#else
2963-
int pathLength = snprintf(path, MAX_FILEPATH_LENGTH - 1, "%s/%s", basePath, entity->d_name);
2966+
int pathLength = snprintf(path, MAX_FILEPATH_LENGTH, "%s/%s", basePath, entity->d_name);
29642967
#endif
29652968
// Don't add to count if path too long
29662969
if ((pathLength < 0) || (pathLength >= MAX_FILEPATH_LENGTH))
@@ -4286,9 +4289,9 @@ static void ScanDirectoryFiles(const char *basePath, FilePathList *files, const
42864289
{
42874290
// Construct new path from our base path
42884291
#if defined(_WIN32)
4289-
int pathLength = snprintf(path, MAX_FILEPATH_LENGTH - 1, "%s\\%s", basePath, dp->d_name);
4292+
int pathLength = snprintf(path, MAX_FILEPATH_LENGTH, "%s\\%s", basePath, dp->d_name);
42904293
#else
4291-
int pathLength = snprintf(path, MAX_FILEPATH_LENGTH - 1, "%s/%s", basePath, dp->d_name);
4294+
int pathLength = snprintf(path, MAX_FILEPATH_LENGTH, "%s/%s", basePath, dp->d_name);
42924295
#endif
42934296

42944297
if ((pathLength < 0) || (pathLength >= MAX_FILEPATH_LENGTH))
@@ -4300,15 +4303,15 @@ static void ScanDirectoryFiles(const char *basePath, FilePathList *files, const
43004303
if ((filter == NULL) || (strstr(filter, FILE_FILTER_TAG_ALL) != NULL) ||
43014304
(strstr(filter, FILE_FILTER_TAG_FILE_ONLY) != NULL) || IsFileExtension(path, filter))
43024305
{
4303-
strncpy(files->paths[files->count], path, MAX_FILEPATH_LENGTH - 1);
4306+
memcpy(files->paths[files->count], path, pathLength);
43044307
files->count++;
43054308
}
43064309
}
43074310
else
43084311
{
43094312
if ((filter != NULL) && ((strstr(filter, FILE_FILTER_TAG_DIR_ONLY) != NULL) || (strstr(filter, FILE_FILTER_TAG_ALL) != NULL)))
43104313
{
4311-
strncpy(files->paths[files->count], path, MAX_FILEPATH_LENGTH - 1);
4314+
memcpy(files->paths[files->count], path, pathLength);
43124315
files->count++;
43134316
}
43144317

0 commit comments

Comments
 (0)