add check for replacement,replace strcpy,strncpy with memcpy
This commit is contained in:
parent
afe74c1c70
commit
20b42b9f86
20
src/rtext.c
20
src/rtext.c
|
|
@ -1727,7 +1727,6 @@ char *GetTextBetween(const char *text, const char *begin, const char *end)
|
||||||
|
|
||||||
// Replace text string
|
// Replace text string
|
||||||
// REQUIRES: strstr(), strncpy()
|
// REQUIRES: strstr(), strncpy()
|
||||||
// TODO: If (replacement == "") remove "search" text
|
|
||||||
// WARNING: Allocated memory must be manually freed
|
// WARNING: Allocated memory must be manually freed
|
||||||
char *TextReplace(const char *text, const char *search, const char *replacement)
|
char *TextReplace(const char *text, const char *search, const char *replacement)
|
||||||
{
|
{
|
||||||
|
|
@ -1735,6 +1734,8 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
|
||||||
|
|
||||||
if ((text != NULL) && (search != NULL))
|
if ((text != NULL) && (search != NULL))
|
||||||
{
|
{
|
||||||
|
if(replacement == NULL) replacement = "";
|
||||||
|
|
||||||
char *insertPoint = NULL; // Next insert point
|
char *insertPoint = NULL; // Next insert point
|
||||||
char *temp = NULL; // Temp pointer
|
char *temp = NULL; // Temp pointer
|
||||||
int textLen = 0; // Text string length
|
int textLen = 0; // Text string length
|
||||||
|
|
@ -1768,15 +1769,14 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
|
||||||
insertPoint = (char *)strstr(text, search);
|
insertPoint = (char *)strstr(text, search);
|
||||||
lastReplacePos = (int)(insertPoint - text);
|
lastReplacePos = (int)(insertPoint - text);
|
||||||
|
|
||||||
// TODO: Review logic to avoid strcpy()
|
memcpy(temp,text,lastReplacePos);
|
||||||
// OK - Those lines work
|
temp += lastReplacePos;
|
||||||
temp = strncpy(temp, text, lastReplacePos) + lastReplacePos;
|
|
||||||
temp = strcpy(temp, replacement) + replaceLen;
|
if(replaceLen > 0)
|
||||||
// WRONG - But not those ones
|
{
|
||||||
//temp = strncpy(temp, text, tempLen - 1) + lastReplacePos;
|
memcpy(temp,replacement,replaceLen);
|
||||||
//tempLen -= lastReplacePos;
|
temp += replaceLen;
|
||||||
//temp = strncpy(temp, replacement, tempLen - 1) + replaceLen;
|
}
|
||||||
//tempLen -= replaceLen;
|
|
||||||
|
|
||||||
text += lastReplacePos + searchLen; // Move to next "end of replace"
|
text += lastReplacePos + searchLen; // Move to next "end of replace"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user