Skip to content

Commit

Permalink
StreamString SSO fix (#2736)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
earlephilhower authored and me-no-dev committed May 11, 2019
1 parent 43bf393 commit bd57ff4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cores/esp32/StreamString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@

size_t StreamString::write(const uint8_t *data, size_t size) {
if(size && data) {
if(reserve(length() + size + 1)) {
const unsigned int newlen = length() + size;
if(reserve(newlen + 1)) {
memcpy((void *) (wbuffer() + len()), (const void *) data, size);
setLen(len() + size);
*(wbuffer() + len()) = 0x00; // add null for string end
setLen(newlen);
*(wbuffer() + newlen) = 0x00; // add null for string end
return size;
}
}
Expand Down

0 comments on commit bd57ff4

Please sign in to comment.