Skip to content

Commit db88c82

Browse files
committed
Fix Small String Optimization misplaced 0 terminator
A fix for some issue introduced in PR #5690 See discussion in #5883
1 parent d19fc3b commit db88c82

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

cores/esp8266/WString.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ String::~String() {
129129
// /*********************************************/
130130

131131
inline void String::init(void) {
132+
memset(sso_buf, 0, sizeof(sso_buf));
132133
setSSO(false);
133134
setCapacity(0);
134135
setLen(0);
@@ -161,11 +162,11 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
161162
return 1;
162163
} else { // if bufptr && !sso()
163164
// Using bufptr, need to shrink into sso_buff
164-
char temp[sizeof(sso_buf)];
165+
char temp[sizeof(sso_buf)] = {0};
165166
memcpy(temp, buffer(), maxStrLen);
166167
free(wbuffer());
167168
setSSO(true);
168-
memcpy(wbuffer(), temp, maxStrLen);
169+
memcpy(wbuffer(), temp, sizeof(sso_buf));
169170
return 1;
170171
}
171172
}

tests/host/core/test_string.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ TEST_CASE("String constructors", "[core][String]")
8383
REQUIRE(ssh == "3.14159_abcd");
8484
String flash = (F("hello from flash"));
8585
REQUIRE(flash == "hello from flash");
86+
const char textarray[6] = {'h', 'e', 'l', 'l', 'o', 0};
87+
String hello(textarray);
88+
REQUIRE(hello == "hello");
89+
String hello2;
90+
hello2 = textarray;
91+
REQUIRE(hello2 == "hello");
8692
}
8793

8894
TEST_CASE("String concantenation", "[core][String]")
@@ -360,4 +366,8 @@ TEST_CASE("String SSO works", "[core][String]")
360366
REQUIRE(s == "0123456789abcdefghijklmnopqrstu");
361367
REQUIRE(s.length() == 31);
362368
}
369+
s = "0123456789abcde";
370+
s = s.substring(s.indexOf('a'));
371+
REQUIRE(s == "abcde");
372+
REQUIRE(s.length() == 5);
363373
}

0 commit comments

Comments
 (0)