Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WString: c_str() returns null pointer after move #7611

Merged
merged 3 commits into from
Sep 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 3 additions & 32 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ String::String(const __FlashStringHelper *pstr) {
*this = pstr; // see operator =
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
String::String(String &&rval) noexcept {
init();
move(rval);
Expand All @@ -55,7 +54,6 @@ String::String(StringSumHelper &&rval) noexcept {
init();
move(rval);
}
#endif

String::String(char c) {
init();
Expand Down Expand Up @@ -223,36 +221,11 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
return *this;
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
void String::move(String &rhs) noexcept {
if (buffer()) {
if (capacity() >= rhs.len()) {
memmove_P(wbuffer(), rhs.buffer(), rhs.length() + 1);
setLen(rhs.len());
rhs.invalidate();
return;
} else {
if (!isSSO()) {
free(wbuffer());
setBuffer(nullptr);
}
}
}
if (rhs.isSSO()) {
setSSO(true);
memmove_P(sso.buff, rhs.sso.buff, sizeof(sso.buff));
} else {
setSSO(false);
setBuffer(rhs.wbuffer());
}
setCapacity(rhs.capacity());
setLen(rhs.len());
rhs.setSSO(false);
rhs.setCapacity(0);
rhs.setLen(0);
rhs.setBuffer(nullptr);
invalidate();
sso = rhs.sso;
rhs.init();
}
#endif

String & String::operator =(const String &rhs) {
if (this == &rhs)
Expand All @@ -266,7 +239,6 @@ String & String::operator =(const String &rhs) {
return *this;
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & String::operator =(String &&rval) noexcept {
if (this != &rval)
move(rval);
Expand All @@ -278,7 +250,6 @@ String & String::operator =(StringSumHelper &&rval) noexcept {
move(rval);
return *this;
}
#endif

String & String::operator =(const char *cstr) {
if (cstr)
Expand Down
11 changes: 4 additions & 7 deletions cores/esp8266/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ class String {
// if the initial value is null or invalid, or if memory allocation
// fails, the string will be marked as invalid (i.e. "if (s)" will
// be false).
String(const char *cstr = nullptr);
String() {
init();
}
String(const char *cstr);
String(const String &str);
String(const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String(String &&rval) noexcept;
String(StringSumHelper &&rval) noexcept;
#endif
explicit String(char c);
explicit String(unsigned char, unsigned char base = 10);
explicit String(int, unsigned char base = 10);
Expand Down Expand Up @@ -95,10 +96,8 @@ class String {
String & operator =(const String &rhs);
String & operator =(const char *cstr);
String & operator = (const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & operator =(String &&rval) noexcept;
String & operator =(StringSumHelper &&rval) noexcept;
#endif

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

Expand Down Expand Up @@ -316,9 +315,7 @@ class String {
// copy and move
String & copy(const char *cstr, unsigned int length);
String & copy(const __FlashStringHelper *pstr, unsigned int length);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
void move(String &rhs) noexcept;
#endif
};

class StringSumHelper: public String {
Expand Down
13 changes: 13 additions & 0 deletions tests/host/core/test_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
#include <limits.h>
#include <StreamString.h>

TEST_CASE("String::move", "[core][String]")
{
const char buffer[] = "this string goes over the sso limit";

String target;
String source(buffer);

target = std::move(source);
REQUIRE(source.c_str() != nullptr);
REQUIRE(!source.length());
REQUIRE(target == buffer);
}

TEST_CASE("String::trim", "[core][String]")
{
String str;
Expand Down