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

Fix crash when using String::move on empty string (#10938) #10940

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Keep allocated memory when rhs fits
Use case: Appending to a String with pre-allocated memory (e.g. from `reserve()`)
TD-er authored Feb 6, 2025
commit 7948954cbc88829141b409e4825150d5ddc4b320
8 changes: 6 additions & 2 deletions cores/esp32/WString.cpp
Original file line number Diff line number Diff line change
@@ -238,8 +238,12 @@ String &String::copy(const char *cstr, unsigned int length) {
#ifdef __GXX_EXPERIMENTAL_CXX0X__
void String::move(String &rhs) {
if (buffer()) {
if (capacity() >= rhs.len() && rhs.len() && rhs.buffer()) {
memmove(wbuffer(), rhs.buffer(), rhs.length() + 1);
if (capacity() >= rhs.len()) {
// Use case: When 'reserve()' was called and the first
// assignment/append is the return value of a function.
if (rhs.len() && rhs.buffer()) {
memmove(wbuffer(), rhs.buffer(), rhs.length() + 1);
}
setLen(rhs.len());
rhs.invalidate();
return;