Skip to content

Commit a7a9d2d

Browse files
authored
Fix loop iterations
1 parent 76f999b commit a7a9d2d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

cores/arduino/WString.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,14 +696,16 @@ void String::remove(unsigned int index, unsigned int count){
696696
// removes characters from the middle of a string.
697697
if (count <= 0) { return; } // exit if nothing to remove
698698
if (index >= len) { return; } // ensure start is within string length; thus, ensures (len-index >= 1)
699-
if (count > len - index) {
699+
if (count > len - index) { // ensure characters to remove is no larger than total length remaining
700700
count = len - index;
701701
}
702702
char *writeTo = buffer + index;
703703
char *copyFrom = buffer + index + count;
704704
len = len - count;
705+
705706
// strncpy() cannot be used with overlapping buffers, so copy one char at a time
706-
for (unsigned int i = 0; i < count; i++, writeTo++, copyFrom++) {
707+
unsigned int charactersToMove = len - index; // yes, uses post-adjusted length
708+
for (unsigned int i = 0; i < charactersToMove; i++, writeTo++, copyFrom++) {
707709
*writeTo = *copyFrom;
708710
}
709711
buffer[len] = 0;

0 commit comments

Comments
 (0)