From 80944fe78bf93cb2d42d136d73749dbdf2a66109 Mon Sep 17 00:00:00 2001 From: TD-er Date: Thu, 6 Feb 2025 12:45:42 +0100 Subject: [PATCH 1/2] Simplify calls to String::copy A lot of the same checks were done before calling `copy()` which should be done in the `copy()` function itself. --- cores/esp32/WString.cpp | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/cores/esp32/WString.cpp b/cores/esp32/WString.cpp index 71183213ac2..7c42e3f9a4d 100644 --- a/cores/esp32/WString.cpp +++ b/cores/esp32/WString.cpp @@ -226,7 +226,7 @@ bool String::changeBuffer(unsigned int maxStrLen) { /*********************************************/ String &String::copy(const char *cstr, unsigned int length) { - if (!reserve(length)) { + if (cstr == nullptr || !reserve(length)) { invalidate(); return *this; } @@ -270,12 +270,7 @@ String &String::operator=(const String &rhs) { if (this == &rhs) { return *this; } - if (rhs.buffer()) { - copy(rhs.buffer(), rhs.len()); - } else { - invalidate(); - } - return *this; + return copy(rhs.buffer(), rhs.len()); } #ifdef __GXX_EXPERIMENTAL_CXX0X__ @@ -295,12 +290,7 @@ String &String::operator=(StringSumHelper &&rval) { #endif String &String::operator=(const char *cstr) { - if (cstr) { - copy(cstr, strlen(cstr)); - } else { - invalidate(); - } - return *this; + return copy(cstr, strlen(cstr)); } /*********************************************/ From 782c32d15d0ca23d6a675f4c213ff487e9e90985 Mon Sep 17 00:00:00 2001 From: TD-er Date: Thu, 6 Feb 2025 13:58:41 +0100 Subject: [PATCH 2/2] String::copy() Should not copy more than given length --- cores/esp32/WString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/WString.cpp b/cores/esp32/WString.cpp index 7c42e3f9a4d..06f32e3c76b 100644 --- a/cores/esp32/WString.cpp +++ b/cores/esp32/WString.cpp @@ -230,7 +230,7 @@ String &String::copy(const char *cstr, unsigned int length) { invalidate(); return *this; } - memmove(wbuffer(), cstr, length + 1); + memmove(wbuffer(), cstr, length); setLen(length); return *this; }