113113
114114#include <stdlib.h> // Required for: srand(), rand(), atexit()
115115#include <stdio.h> // Required for: sprintf() [Used in OpenURL()]
116- #include <string.h> // Required for: strlen(), strcpy (), strcmp(), strrchr(), memset()
116+ #include <string.h> // Required for: strlen(), strncpy (), strcmp(), strrchr(), memset()
117117#include <time.h> // Required for: time() [Used in InitTimer()]
118118#include <math.h> // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()]
119119
@@ -1837,8 +1837,8 @@ void TakeScreenshot(const char *fileName)
18371837 unsigned char * imgData = rlReadScreenPixels ((int )((float )CORE .Window .render .width * scale .x ), (int )((float )CORE .Window .render .height * scale .y ));
18381838 Image image = { imgData , (int )((float )CORE .Window .render .width * scale .x ), (int )((float )CORE .Window .render .height * scale .y ), 1 , PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 };
18391839
1840- char path [512 ] = { 0 };
1841- strcpy (path , TextFormat ("%s/%s" , CORE .Storage .basePath , fileName ));
1840+ char path [MAX_FILEPATH_LENGTH ] = { 0 };
1841+ strncpy (path , TextFormat ("%s/%s" , CORE .Storage .basePath , fileName ), MAX_FILEPATH_LENGTH - 1 );
18421842
18431843 ExportImage (image , path ); // WARNING: Module required: rtextures
18441844 RL_FREE (imgData );
@@ -2022,7 +2022,7 @@ bool IsFileExtension(const char *fileName, const char *ext)
20222022 int extLen = (int )strlen (ext );
20232023 char * extList = (char * )RL_CALLOC (extLen + 1 , 1 );
20242024 char * extListPtrs [MAX_FILE_EXTENSIONS ] = { 0 };
2025- strcpy (extList , ext );
2025+ strncpy (extList , ext , extLen );
20262026 extListPtrs [0 ] = extList ;
20272027
20282028 for (int i = 0 ; i < extLen ; i ++ )
@@ -2130,11 +2130,11 @@ const char *GetFileExtension(const char *fileName)
21302130}
21312131
21322132// String pointer reverse break: returns right-most occurrence of charset in s
2133- static const char * strprbrk (const char * s , const char * charset )
2133+ static const char * strprbrk (const char * text , const char * charset )
21342134{
21352135 const char * latestMatch = NULL ;
21362136
2137- for (; s = strpbrk (s , charset ), s != NULL ; latestMatch = s ++ ) { }
2137+ for (; ( text != NULL ) && ( text = strpbrk (text , charset )) ; latestMatch = text ++ ) { }
21382138
21392139 return latestMatch ;
21402140}
@@ -2161,7 +2161,7 @@ const char *GetFileNameWithoutExt(const char *filePath)
21612161
21622162 if (filePath != NULL )
21632163 {
2164- strcpy (fileName , GetFileName (filePath )); // Get filename.ext without path
2164+ strncpy (fileName , GetFileName (filePath ), MAX_FILENAME_LENGTH - 1 ); // Get filename.ext without path
21652165 int size = (int )strlen (fileName ); // Get size in bytes
21662166
21672167 for (int i = size ; i > 0 ; i -- ) // Reverse search '.'
@@ -2233,7 +2233,7 @@ const char *GetPrevDirectoryPath(const char *dirPath)
22332233 memset (prevDirPath , 0 , MAX_FILEPATH_LENGTH );
22342234 int pathLen = (int )strlen (dirPath );
22352235
2236- if (pathLen <= 3 ) strcpy (prevDirPath , dirPath );
2236+ if (pathLen <= 3 ) strncpy (prevDirPath , dirPath , MAX_FILEPATH_LENGTH - 1 );
22372237
22382238 for (int i = (pathLen - 1 ); (i >= 0 ) && (pathLen > 3 ); i -- )
22392239 {
@@ -2472,12 +2472,12 @@ int MakeDirectory(const char *dirPath)
24722472}
24732473
24742474// Change working directory, returns true on success
2475- bool ChangeDirectory (const char * dir )
2475+ bool ChangeDirectory (const char * dirPath )
24762476{
2477- bool result = CHDIR (dir );
2477+ bool result = CHDIR (dirPath );
24782478
2479- if (result != 0 ) TRACELOG (LOG_WARNING , "SYSTEM: Failed to change to directory: %s" , dir );
2480- else TRACELOG (LOG_INFO , "SYSTEM: Working Directory: %s" , dir );
2479+ if (result != 0 ) TRACELOG (LOG_WARNING , "SYSTEM: Failed to change to directory: %s" , dirPath );
2480+ else TRACELOG (LOG_INFO , "SYSTEM: Working Directory: %s" , dirPath );
24812481
24822482 return (result == 0 );
24832483}
@@ -2708,6 +2708,9 @@ unsigned char *DecodeDataBase64(const char *text, int *outputSize)
27082708 ['0' ] = 52 , ['1' ] = 53 , ['2' ] = 54 , ['3' ] = 55 , ['4' ] = 56 , ['5' ] = 57 , ['6' ] = 58 , ['7' ] = 59 ,
27092709 ['8' ] = 60 , ['9' ] = 61 , ['+' ] = 62 , ['/' ] = 63
27102710 };
2711+
2712+ * outputSize = 0 ;
2713+ if (text == NULL ) return NULL ;
27112714
27122715 // Compute expected size and padding
27132716 int dataSize = (int )strlen (text ); // WARNING: Expecting NULL terminated strings!
@@ -3952,22 +3955,22 @@ static void ScanDirectoryFiles(const char *basePath, FilePathList *files, const
39523955 {
39533956 if (IsFileExtension (path , filter ))
39543957 {
3955- strcpy (files -> paths [files -> count ], path );
3958+ strncpy (files -> paths [files -> count ], path , MAX_FILEPATH_LENGTH - 1 );
39563959 files -> count ++ ;
39573960 }
39583961 }
39593962 else
39603963 {
39613964 if (strstr (filter , DIRECTORY_FILTER_TAG ) != NULL )
39623965 {
3963- strcpy (files -> paths [files -> count ], path );
3966+ strncpy (files -> paths [files -> count ], path , MAX_FILEPATH_LENGTH - 1 );
39643967 files -> count ++ ;
39653968 }
39663969 }
39673970 }
39683971 else
39693972 {
3970- strcpy (files -> paths [files -> count ], path );
3973+ strncpy (files -> paths [files -> count ], path , MAX_FILEPATH_LENGTH - 1 );
39713974 files -> count ++ ;
39723975 }
39733976 }
@@ -4011,13 +4014,13 @@ static void ScanDirectoryFilesRecursively(const char *basePath, FilePathList *fi
40114014 {
40124015 if (IsFileExtension (path , filter ))
40134016 {
4014- strcpy (files -> paths [files -> count ], path );
4017+ strncpy (files -> paths [files -> count ], path , MAX_FILEPATH_LENGTH - 1 );
40154018 files -> count ++ ;
40164019 }
40174020 }
40184021 else
40194022 {
4020- strcpy (files -> paths [files -> count ], path );
4023+ strncpy (files -> paths [files -> count ], path , MAX_FILEPATH_LENGTH - 1 );
40214024 files -> count ++ ;
40224025 }
40234026
@@ -4031,7 +4034,7 @@ static void ScanDirectoryFilesRecursively(const char *basePath, FilePathList *fi
40314034 {
40324035 if ((filter != NULL ) && (strstr (filter , DIRECTORY_FILTER_TAG ) != NULL ))
40334036 {
4034- strcpy (files -> paths [files -> count ], path );
4037+ strncpy (files -> paths [files -> count ], path , MAX_FILEPATH_LENGTH - 1 );
40354038 files -> count ++ ;
40364039 }
40374040
@@ -4334,23 +4337,26 @@ const char *TextFormat(const char *text, ...)
43344337
43354338 char * currentBuffer = buffers [index ];
43364339 memset (currentBuffer , 0 , MAX_TEXT_BUFFER_LENGTH ); // Clear buffer before using
4340+
4341+ if (text != NULL )
4342+ {
4343+ va_list args ;
4344+ va_start (args , text );
4345+ int requiredByteCount = vsnprintf (currentBuffer , MAX_TEXT_BUFFER_LENGTH , text , args );
4346+ va_end (args );
43374347
4338- va_list args ;
4339- va_start (args , text );
4340- int requiredByteCount = vsnprintf (currentBuffer , MAX_TEXT_BUFFER_LENGTH , text , args );
4341- va_end (args );
4348+ // If requiredByteCount is larger than the MAX_TEXT_BUFFER_LENGTH, then overflow occurred
4349+ if (requiredByteCount >= MAX_TEXT_BUFFER_LENGTH )
4350+ {
4351+ // Inserting "..." at the end of the string to mark as truncated
4352+ char * truncBuffer = buffers [index ] + MAX_TEXT_BUFFER_LENGTH - 4 ; // Adding 4 bytes = "...\0"
4353+ snprintf (truncBuffer , 4 , "..." );
4354+ }
43424355
4343- // If requiredByteCount is larger than the MAX_TEXT_BUFFER_LENGTH, then overflow occurred
4344- if (requiredByteCount >= MAX_TEXT_BUFFER_LENGTH )
4345- {
4346- // Inserting "..." at the end of the string to mark as truncated
4347- char * truncBuffer = buffers [index ] + MAX_TEXT_BUFFER_LENGTH - 4 ; // Adding 4 bytes = "...\0"
4348- snprintf (truncBuffer , 4 , "..." );
4356+ index += 1 ; // Move to next buffer for next function call
4357+ if (index >= MAX_TEXTFORMAT_BUFFERS ) index = 0 ;
43494358 }
43504359
4351- index += 1 ; // Move to next buffer for next function call
4352- if (index >= MAX_TEXTFORMAT_BUFFERS ) index = 0 ;
4353-
43544360 return currentBuffer ;
43554361}
43564362
0 commit comments