Skip to content

Commit 5d7594b

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 means this commit also makes remove work for strings that contain embedded nul bytes.
1 parent c21bea8 commit 5d7594b

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

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

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

hardware/arduino/sam/cores/arduino/WString.cpp

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

0 commit comments

Comments
 (0)