Skip to content

Commit 22a488c

Browse files
committed
Fix String::clear() not clearing the string properly
Fixes: #4893
1 parent 6e7cc52 commit 22a488c

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Diff for: cores/esp32/WString.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ String::~String() {
130130

131131
inline void String::init(void) {
132132
setSSO(false);
133+
setBuffer(nullptr);
133134
setCapacity(0);
134135
setLen(0);
135-
setBuffer(nullptr);
136136
}
137137

138138
void String::invalidate(void) {

Diff for: cores/esp32/WString.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,19 @@ class String {
301301
inline unsigned int len() const { return isSSO() ? sso.len : ptr.len; }
302302
inline unsigned int capacity() const { return isSSO() ? (unsigned int)SSOSIZE - 1 : ptr.cap; } // Size of max string not including terminal NUL
303303
inline void setSSO(bool set) { sso.isSSO = set; }
304-
inline void setLen(int len) { if (isSSO()) sso.len = len; else ptr.len = len; }
304+
inline void setLen(int len) {
305+
if (isSSO()) {
306+
sso.len = len;
307+
sso.buff[len] = 0;
308+
} else {
309+
ptr.len = len;
310+
if (ptr.buff) {
311+
ptr.buff[len] = 0;
312+
}
313+
}
314+
}
305315
inline void setCapacity(int cap) { if (!isSSO()) ptr.cap = cap; }
306-
inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; }
316+
inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; }
307317
// Buffer accessor functions
308318
inline const char *buffer() const { return (const char *)(isSSO() ? sso.buff : ptr.buff); }
309319
inline char *wbuffer() const { return isSSO() ? const_cast<char *>(sso.buff) : ptr.buff; } // Writable version of buffer

0 commit comments

Comments
 (0)