Skip to content

Commit 2454b3e

Browse files
committed
REVIEWED: TextReplace() and TextLength(), avoid using strcpy()
1 parent d996bf2 commit 2454b3e

1 file changed

Lines changed: 29 additions & 29 deletions

File tree

src/rtext.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,15 +1497,14 @@ void UnloadTextLines(char **lines, int lineCount)
14971497
}
14981498

14991499
// Get text length in bytes, check for \0 character
1500+
// NOTE: Alternative: use strlen(text)
15001501
unsigned int TextLength(const char *text)
15011502
{
15021503
unsigned int length = 0;
15031504

15041505
if (text != NULL)
1505-
{
1506-
// NOTE: Alternative: use strlen(text)
1507-
1508-
while (*text++) length++;
1506+
{
1507+
while (text[length] != '\0') length++;
15091508
}
15101509

15111510
return length;
@@ -1718,7 +1717,7 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
17181717
{
17191718
char *result = NULL;
17201719

1721-
if ((text != NULL) && (search != NULL))
1720+
if ((text != NULL) && (search != NULL) && (search[0] != '\0'))
17221721
{
17231722
if (replacement == NULL) replacement = "";
17241723

@@ -1732,8 +1731,6 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
17321731

17331732
textLen = TextLength(text);
17341733
searchLen = TextLength(search);
1735-
if (searchLen == 0) return NULL; // Empty search causes infinite loop during count
1736-
17371734
replaceLen = TextLength(replacement);
17381735

17391736
// Count the number of replacements needed
@@ -1742,34 +1739,37 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
17421739

17431740
// Allocate returning string and point temp to it
17441741
int tempLen = textLen + (replaceLen - searchLen)*count + 1;
1745-
temp = result = (char *)RL_MALLOC(tempLen);
1746-
1747-
if (!result) return NULL; // Memory could not be allocated
1742+
temp = result = (char *)RL_CALLOC(tempLen, sizeof(char));
17481743

1749-
// First time through the loop, all the variable are set correctly from here on,
1750-
// - 'temp' points to the end of the result string
1751-
// - 'insertPoint' points to the next occurrence of replace in text
1752-
// - 'text' points to the remainder of text after "end of replace"
1753-
while (count--)
1744+
if (result != NULL) // Memory was allocated
17541745
{
1755-
insertPoint = (char *)strstr(text, search);
1756-
lastReplacePos = (int)(insertPoint - text);
1757-
1758-
memcpy(temp, text, lastReplacePos);
1759-
temp += lastReplacePos;
1760-
1761-
if (replaceLen > 0)
1746+
// First time through the loop, all the variable are set correctly from here on,
1747+
// - 'temp' points to the end of the result string
1748+
// - 'insertPoint' points to the next occurrence of replace in text
1749+
// - 'text' points to the remainder of text after "end of replace"
1750+
while (count > 0)
17621751
{
1763-
memcpy(temp, replacement, replaceLen);
1764-
temp += replaceLen;
1752+
insertPoint = (char *)strstr(text, search);
1753+
lastReplacePos = (int)(insertPoint - text);
1754+
1755+
memcpy(temp, text, lastReplacePos);
1756+
temp += lastReplacePos;
1757+
1758+
if (replaceLen > 0)
1759+
{
1760+
memcpy(temp, replacement, replaceLen);
1761+
temp += replaceLen;
1762+
}
1763+
1764+
text += (lastReplacePos + searchLen); // Move to next "end of replace"
1765+
1766+
count--;
17651767
}
17661768

1767-
text += lastReplacePos + searchLen; // Move to next "end of replace"
1769+
// Copy remaind text part after replacement to result (pointed by moving temp)
1770+
// NOTE: Text pointer internal copy has been updated along the process
1771+
strncpy(temp, text, TextLength(text));
17681772
}
1769-
1770-
// Copy remaind text part after replacement to result (pointed by moving temp)
1771-
strcpy(temp, text); // OK
1772-
//strncpy(temp, text, tempLen - 1); // WRONG
17731773
}
17741774

17751775
return result;

0 commit comments

Comments
 (0)