Fix String::clear() not clearing the string properly

Fixes: https://github.com/espressif/arduino-esp32/issues/4893
This commit is contained in:
me-no-dev 2021-03-04 21:20:27 +02:00
parent 6e7cc5210d
commit 22a488cf23
2 changed files with 13 additions and 3 deletions

View File

@ -130,9 +130,9 @@ String::~String() {
inline void String::init(void) { inline void String::init(void) {
setSSO(false); setSSO(false);
setBuffer(nullptr);
setCapacity(0); setCapacity(0);
setLen(0); setLen(0);
setBuffer(nullptr);
} }
void String::invalidate(void) { void String::invalidate(void) {

View File

@ -301,7 +301,17 @@ class String {
inline unsigned int len() const { return isSSO() ? sso.len : ptr.len; } inline unsigned int len() const { return isSSO() ? sso.len : ptr.len; }
inline unsigned int capacity() const { return isSSO() ? (unsigned int)SSOSIZE - 1 : ptr.cap; } // Size of max string not including terminal NUL inline unsigned int capacity() const { return isSSO() ? (unsigned int)SSOSIZE - 1 : ptr.cap; } // Size of max string not including terminal NUL
inline void setSSO(bool set) { sso.isSSO = set; } inline void setSSO(bool set) { sso.isSSO = set; }
inline void setLen(int len) { if (isSSO()) sso.len = len; else ptr.len = len; } inline void setLen(int len) {
if (isSSO()) {
sso.len = len;
sso.buff[len] = 0;
} else {
ptr.len = len;
if (ptr.buff) {
ptr.buff[len] = 0;
}
}
}
inline void setCapacity(int cap) { if (!isSSO()) ptr.cap = cap; } inline void setCapacity(int cap) { if (!isSSO()) ptr.cap = cap; }
inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; } inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; }
// Buffer accessor functions // Buffer accessor functions