File tree Expand file tree Collapse file tree 1 file changed +4
-2
lines changed Expand file tree Collapse file tree 1 file changed +4
-2
lines changed Original file line number Diff line number Diff line change @@ -696,14 +696,16 @@ void String::remove(unsigned int index, unsigned int count){
696
696
// removes characters from the middle of a string.
697
697
if (count <= 0 ) { return ; } // exit if nothing to remove
698
698
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
700
700
count = len - index;
701
701
}
702
702
char *writeTo = buffer + index;
703
703
char *copyFrom = buffer + index + count;
704
704
len = len - count;
705
+
705
706
// 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++) {
707
709
*writeTo = *copyFrom;
708
710
}
709
711
buffer[len] = 0 ;
You can’t perform that action at this time.
0 commit comments