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){
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 ;
You can’t perform that action at this time.
0 commit comments