Skip to content

Commit cc042b9

Browse files
mcsprjjsuwa
andauthored
WString: c_str() returns null pointer after move (#7611)
* (test) WString: c_str() returns null pointer target = std::move(source) does not reset buffer pointer back to the sso * wstring: correctly do move invalidation & copy based on the #7553 without isSSO -> isHeap rename and inline optimizations additionally, remove useless pre-c++11 preprocessor checks Co-authored-by: Takayuki 'January June' Suwa <[email protected]>
1 parent a3281fe commit cc042b9

File tree

3 files changed

+20
-39
lines changed

3 files changed

+20
-39
lines changed

cores/esp8266/WString.cpp

+3-32
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ String::String(const __FlashStringHelper *pstr) {
4545
*this = pstr; // see operator =
4646
}
4747

48-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
4948
String::String(String &&rval) noexcept {
5049
init();
5150
move(rval);
@@ -55,7 +54,6 @@ String::String(StringSumHelper &&rval) noexcept {
5554
init();
5655
move(rval);
5756
}
58-
#endif
5957

6058
String::String(char c) {
6159
init();
@@ -223,36 +221,11 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
223221
return *this;
224222
}
225223

226-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
227224
void String::move(String &rhs) noexcept {
228-
if (buffer()) {
229-
if (capacity() >= rhs.len()) {
230-
memmove_P(wbuffer(), rhs.buffer(), rhs.length() + 1);
231-
setLen(rhs.len());
232-
rhs.invalidate();
233-
return;
234-
} else {
235-
if (!isSSO()) {
236-
free(wbuffer());
237-
setBuffer(nullptr);
238-
}
239-
}
240-
}
241-
if (rhs.isSSO()) {
242-
setSSO(true);
243-
memmove_P(sso.buff, rhs.sso.buff, sizeof(sso.buff));
244-
} else {
245-
setSSO(false);
246-
setBuffer(rhs.wbuffer());
247-
}
248-
setCapacity(rhs.capacity());
249-
setLen(rhs.len());
250-
rhs.setSSO(false);
251-
rhs.setCapacity(0);
252-
rhs.setLen(0);
253-
rhs.setBuffer(nullptr);
225+
invalidate();
226+
sso = rhs.sso;
227+
rhs.init();
254228
}
255-
#endif
256229

257230
String & String::operator =(const String &rhs) {
258231
if (this == &rhs)
@@ -266,7 +239,6 @@ String & String::operator =(const String &rhs) {
266239
return *this;
267240
}
268241

269-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
270242
String & String::operator =(String &&rval) noexcept {
271243
if (this != &rval)
272244
move(rval);
@@ -278,7 +250,6 @@ String & String::operator =(StringSumHelper &&rval) noexcept {
278250
move(rval);
279251
return *this;
280252
}
281-
#endif
282253

283254
String & String::operator =(const char *cstr) {
284255
if (cstr)

cores/esp8266/WString.h

+4-7
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ class String {
5353
// if the initial value is null or invalid, or if memory allocation
5454
// fails, the string will be marked as invalid (i.e. "if (s)" will
5555
// be false).
56-
String(const char *cstr = nullptr);
56+
String() {
57+
init();
58+
}
59+
String(const char *cstr);
5760
String(const String &str);
5861
String(const __FlashStringHelper *str);
59-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
6062
String(String &&rval) noexcept;
6163
String(StringSumHelper &&rval) noexcept;
62-
#endif
6364
explicit String(char c);
6465
explicit String(unsigned char, unsigned char base = 10);
6566
explicit String(int, unsigned char base = 10);
@@ -95,10 +96,8 @@ class String {
9596
String & operator =(const String &rhs);
9697
String & operator =(const char *cstr);
9798
String & operator = (const __FlashStringHelper *str);
98-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
9999
String & operator =(String &&rval) noexcept;
100100
String & operator =(StringSumHelper &&rval) noexcept;
101-
#endif
102101

103102
// concatenate (works w/ built-in types)
104103

@@ -316,9 +315,7 @@ class String {
316315
// copy and move
317316
String & copy(const char *cstr, unsigned int length);
318317
String & copy(const __FlashStringHelper *pstr, unsigned int length);
319-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
320318
void move(String &rhs) noexcept;
321-
#endif
322319
};
323320

324321
class StringSumHelper: public String {

tests/host/core/test_string.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@
1919
#include <limits.h>
2020
#include <StreamString.h>
2121

22+
TEST_CASE("String::move", "[core][String]")
23+
{
24+
const char buffer[] = "this string goes over the sso limit";
25+
26+
String target;
27+
String source(buffer);
28+
29+
target = std::move(source);
30+
REQUIRE(source.c_str() != nullptr);
31+
REQUIRE(!source.length());
32+
REQUIRE(target == buffer);
33+
}
34+
2235
TEST_CASE("String::trim", "[core][String]")
2336
{
2437
String str;

0 commit comments

Comments
 (0)