Skip to content

Commit

Permalink
Bugfix: String::replace not using memmove (#2384)
Browse files Browse the repository at this point in the history
Can fail when regions overlap
  • Loading branch information
mikee47 authored Oct 11, 2021
1 parent 333af2e commit 14366be
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sming/Wiring/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,14 +860,14 @@ bool String::replace(const char* find_buf, size_t find_len, const char* replace_
char* writeTo = buf;
while((foundAt = (char*)memmem(readFrom, end - readFrom, find_buf, find_len)) != nullptr) {
size_t n = foundAt - readFrom;
memcpy(writeTo, readFrom, n);
memmove(writeTo, readFrom, n);
writeTo += n;
memcpy(writeTo, replace_buf, replace_len);
writeTo += replace_len;
readFrom = foundAt + find_len;
len += diff;
}
memcpy(writeTo, readFrom, end - readFrom);
memmove(writeTo, readFrom, end - readFrom);
setlen(len);
} else {
size_t size = len; // compute size needed for result
Expand Down

0 comments on commit 14366be

Please sign in to comment.