Skip to content

Commit bd57ff4

Browse files
earlephilhowerme-no-dev
authored andcommitted
StreamString SSO fix (espressif#2736)
As found by @mongozmaki in esp8266/Arduino#6035 With SSO implementation in String, StreamString::write generates wrong strings under some circumstances. Reason is that String::len() returns strlen(sso_buf) if SSO=true but with newly written data (in StreamString::write) the null-termination missing at the time len() is called. Furthermore, len() is called twice which is inefficient if SSO=true.
1 parent 43bf393 commit bd57ff4

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

cores/esp32/StreamString.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626
size_t StreamString::write(const uint8_t *data, size_t size) {
2727
if(size && data) {
28-
if(reserve(length() + size + 1)) {
28+
const unsigned int newlen = length() + size;
29+
if(reserve(newlen + 1)) {
2930
memcpy((void *) (wbuffer() + len()), (const void *) data, size);
30-
setLen(len() + size);
31-
*(wbuffer() + len()) = 0x00; // add null for string end
31+
setLen(newlen);
32+
*(wbuffer() + newlen) = 0x00; // add null for string end
3233
return size;
3334
}
3435
}

0 commit comments

Comments
 (0)