From 76f999b82597dfaf7ea551411e5af1ba3c30edd5 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Thu, 25 Mar 2021 15:09:54 -0700 Subject: [PATCH 1/2] Fix [-Wrestrict] bug Untested... --- cores/arduino/WString.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index 71bbc07d1..74d0e03dd 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -693,12 +693,19 @@ void String::remove(unsigned int index){ } void String::remove(unsigned int index, unsigned int count){ - if (index >= len) { return; } - if (count <= 0) { return; } - if (count > len - index) { count = len - index; } - char *writeTo = buffer + index; + // removes characters from the middle of a string. + if (count <= 0) { return; } // exit if nothing to remove + if (index >= len) { return; } // ensure start is within string length; thus, ensures (len-index >= 1) + if (count > len - index) { + count = len - index; + } + char *writeTo = buffer + index; + char *copyFrom = buffer + index + count; len = len - count; - strncpy(writeTo, buffer + index + count,len - index); + // strncpy() cannot be used with overlapping buffers, so copy one char at a time + for (unsigned int i = 0; i < count; i++, writeTo++, copyFrom++) { + *writeTo = *copyFrom; + } buffer[len] = 0; } From a7a9d2dbc12e8516f54eaaec5abf9458be2da670 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Thu, 8 Apr 2021 17:51:06 -0700 Subject: [PATCH 2/2] Fix loop iterations --- cores/arduino/WString.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index 74d0e03dd..ed4e706df 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -696,14 +696,16 @@ void String::remove(unsigned int index, unsigned int count){ // removes characters from the middle of a string. if (count <= 0) { return; } // exit if nothing to remove if (index >= len) { return; } // ensure start is within string length; thus, ensures (len-index >= 1) - if (count > len - index) { + if (count > len - index) { // ensure characters to remove is no larger than total length remaining count = len - index; } char *writeTo = buffer + index; char *copyFrom = buffer + index + count; len = len - count; + // strncpy() cannot be used with overlapping buffers, so copy one char at a time - for (unsigned int i = 0; i < count; i++, writeTo++, copyFrom++) { + unsigned int charactersToMove = len - index; // yes, uses post-adjusted length + for (unsigned int i = 0; i < charactersToMove; i++, writeTo++, copyFrom++) { *writeTo = *copyFrom; } buffer[len] = 0;