Skip to content

Commit e38e02d

Browse files
Fix String.replace overlapping strcpy
Fixes #5949 Adds a test from the issue above and fixes the problem valgrind found.
1 parent 2e75e88 commit e38e02d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

cores/esp8266/WString.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ void String::replace(const String& find, const String& replace) {
744744
readFrom = foundAt + find.len();
745745
setLen(len() + diff);
746746
}
747-
strcpy(writeTo, readFrom);
747+
memmove(writeTo, readFrom, strlen(readFrom)+1);
748748
} else {
749749
unsigned int size = len(); // compute size needed for result
750750
while((foundAt = strstr(readFrom, find.buffer())) != NULL) {

tests/host/core/test_string.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,15 @@ TEST_CASE("String SSO handles junk in memory", "[core][String]")
428428
REQUIRE(*s == "CO2_defect");
429429
s->~String();
430430
}
431+
432+
433+
TEST_CASE("Issue #5949 - Overlapping src/dest in replace", "[core][String]")
434+
{
435+
String blah = "blah";
436+
blah.replace("xx", "y");
437+
REQUIRE(blah == "blah");
438+
blah.replace("x", "yy");
439+
REQUIRE(blah == "blah");
440+
blah.replace(blah, blah);
441+
REQUIRE(blah == "blah");
442+
}

0 commit comments

Comments
 (0)