Skip to content

Commit 41e5586

Browse files
Use memmove in String::remove
Before, it used strncpy, but that has undefined behaviour when the source and destination strings overlap. memove is guaranteed to work correctly in this case. Note that memmove simply copies all bytes, whereas strncpy stopped at the first nul-byte. This measn this commit also makes remove work for strings that contain embedded nul bytes.
1 parent 8073c48 commit 41e5586

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

hardware/arduino/avr/cores/arduino/WString.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ void String::remove(unsigned int index, unsigned int count){
695695
if (index + count > len) { count = len - index; }
696696
char *writeTo = buffer + index;
697697
len = len - count;
698-
strncpy(writeTo, buffer + index + count,len - index);
698+
memmove(writeTo, buffer + index + count, len - index);
699699
buffer[len] = 0;
700700
}
701701

0 commit comments

Comments
 (0)