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