From 2454b3ed4b6b49af46f89b3796ca94facd548ff0 Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 20 Feb 2026 16:27:08 +0100 Subject: [PATCH] REVIEWED: `TextReplace()` and `TextLength()`, avoid using `strcpy()` --- src/rtext.c | 58 ++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index e0aa4fddd..840b452d3 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -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;