Skip to content

Commit

Permalink
Fix String::clear() not clearing the string properly
Browse files Browse the repository at this point in the history
Fixes: #4893
  • Loading branch information
me-no-dev committed Mar 4, 2021
1 parent 6e7cc52 commit 22a488c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cores/esp32/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ String::~String() {

inline void String::init(void) {
setSSO(false);
setBuffer(nullptr);
setCapacity(0);
setLen(0);
setBuffer(nullptr);
}

void String::invalidate(void) {
Expand Down
14 changes: 12 additions & 2 deletions cores/esp32/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,19 @@ class String {
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 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 setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; }
inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; }
// Buffer accessor functions
inline const char *buffer() const { return (const char *)(isSSO() ? sso.buff : ptr.buff); }
inline char *wbuffer() const { return isSSO() ? const_cast<char *>(sso.buff) : ptr.buff; } // Writable version of buffer
Expand Down

0 comments on commit 22a488c

Please sign in to comment.