From 4bd7b4d29e3ebb43cf6495c3d074eea25197e8dd Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Thu, 10 Feb 2022 13:22:00 -0800 Subject: [PATCH] Free String memory on assign of "" or clear() Fixes #8485 Whenever the empty C string is assigned or the .clear() method called, free any heap allocated with the String. --- cores/esp8266/WString.cpp | 2 +- cores/esp8266/WString.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index c6b3c6ae0a..9e77408887 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -258,7 +258,7 @@ String &String::operator =(String &&rval) noexcept { } String &String::operator =(const char *cstr) { - if (cstr) + if (cstr && cstr[0]) copy(cstr, strlen(cstr)); else invalidate(); diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index d028c0abfb..ab9e241489 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -88,7 +88,7 @@ class String { return buffer() ? len() : 0; } void clear(void) { - setLen(0); + invalidate(); } bool isEmpty(void) const { return length() == 0;