REVIEWED: TextReplace() and TextLength(), avoid using strcpy()

This commit is contained in:
Ray
2026-02-20 16:27:08 +01:00
parent d996bf2bbd
commit 2454b3ed4b

View File

@ -1497,15 +1497,14 @@ void UnloadTextLines(char **lines, int lineCount)
} }
// Get text length in bytes, check for \0 character // Get text length in bytes, check for \0 character
// NOTE: Alternative: use strlen(text)
unsigned int TextLength(const char *text) unsigned int TextLength(const char *text)
{ {
unsigned int length = 0; unsigned int length = 0;
if (text != NULL) if (text != NULL)
{ {
// NOTE: Alternative: use strlen(text) while (text[length] != '\0') length++;
while (*text++) length++;
} }
return length; return length;
@ -1718,7 +1717,7 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
{ {
char *result = NULL; char *result = NULL;
if ((text != NULL) && (search != NULL)) if ((text != NULL) && (search != NULL) && (search[0] != '\0'))
{ {
if (replacement == NULL) replacement = ""; if (replacement == NULL) replacement = "";
@ -1732,8 +1731,6 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
textLen = TextLength(text); textLen = TextLength(text);
searchLen = TextLength(search); searchLen = TextLength(search);
if (searchLen == 0) return NULL; // Empty search causes infinite loop during count
replaceLen = TextLength(replacement); replaceLen = TextLength(replacement);
// Count the number of replacements needed // Count the number of replacements needed
@ -1742,15 +1739,15 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
// Allocate returning string and point temp to it // Allocate returning string and point temp to it
int tempLen = textLen + (replaceLen - searchLen)*count + 1; int tempLen = textLen + (replaceLen - searchLen)*count + 1;
temp = result = (char *)RL_MALLOC(tempLen); temp = result = (char *)RL_CALLOC(tempLen, sizeof(char));
if (!result) return NULL; // Memory could not be allocated
if (result != NULL) // Memory was allocated
{
// First time through the loop, all the variable are set correctly from here on, // First time through the loop, all the variable are set correctly from here on,
// - 'temp' points to the end of the result string // - 'temp' points to the end of the result string
// - 'insertPoint' points to the next occurrence of replace in text // - 'insertPoint' points to the next occurrence of replace in text
// - 'text' points to the remainder of text after "end of replace" // - 'text' points to the remainder of text after "end of replace"
while (count--) while (count > 0)
{ {
insertPoint = (char *)strstr(text, search); insertPoint = (char *)strstr(text, search);
lastReplacePos = (int)(insertPoint - text); lastReplacePos = (int)(insertPoint - text);
@ -1764,12 +1761,15 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
temp += replaceLen; temp += replaceLen;
} }
text += lastReplacePos + searchLen; // Move to next "end of replace" text += (lastReplacePos + searchLen); // Move to next "end of replace"
count--;
} }
// Copy remaind text part after replacement to result (pointed by moving temp) // Copy remaind text part after replacement to result (pointed by moving temp)
strcpy(temp, text); // OK // NOTE: Text pointer internal copy has been updated along the process
//strncpy(temp, text, tempLen - 1); // WRONG strncpy(temp, text, TextLength(text));
}
} }
return result; return result;