-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Crash when using String::move on empty string #10938
Labels
Status: Awaiting triage
Issue is waiting for triage
Comments
TD-er
added a commit
to TD-er/arduino-esp32
that referenced
this issue
Feb 6, 2025
TD-er
added a commit
to TD-er/arduino-esp32
that referenced
this issue
Feb 6, 2025
Fixes: espressif#10938 Keep allocated memory when rhs fits Use case: Appending to a String with pre-allocated memory (e.g. from `reserve()`) No need to move 0-termination char in String::move Simplify calls to String::copy A lot of the same checks were done before calling `copy()` which should be done in the `copy()` function itself. String::copy() Should not copy more than given length Fix potential out of range in String::concat There is no prerequisite the given array has to be a 0-terminated char array. So we should only copy the length that has been given. The `setLen()` function will make sure the internal string is 0-terminated. So no need to dangerously assume there will be 1 more byte to copy Allow String::concat(const String &s) with s.buffer() == nullptr When constructing a String object, the internal buffer is a nullptr. However concatenating this to another String would return `false` while this is perfectly fine to do.
me-no-dev
pushed a commit
that referenced
this issue
Feb 13, 2025
Fixes: #10938 Keep allocated memory when rhs fits Use case: Appending to a String with pre-allocated memory (e.g. from `reserve()`) No need to move 0-termination char in String::move Simplify calls to String::copy A lot of the same checks were done before calling `copy()` which should be done in the `copy()` function itself. String::copy() Should not copy more than given length Fix potential out of range in String::concat There is no prerequisite the given array has to be a 0-terminated char array. So we should only copy the length that has been given. The `setLen()` function will make sure the internal string is 0-terminated. So no need to dangerously assume there will be 1 more byte to copy Allow String::concat(const String &s) with s.buffer() == nullptr When constructing a String object, the internal buffer is a nullptr. However concatenating this to another String would return `false` while this is perfectly fine to do.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Board
ESP32-classic
Device Description
Not tried on several boards, but got it reproducible on ESP32-classic.
Hardware Configuration
Version
latest master (checkout manually)
IDE Name
PlatformIO
Operating System
Windows 11 / WSL2
Flash frequency
PSRAM enabled
yes
Upload speed
Description
Assigning an empty string using
std::move
may cause a crash.So also assigning an empty string as result from a function will also eventually end up in crashes.
The fix is extremely simple; the memmove should only be called when
rhs.len() > 0
Maybe the check should even be extended to
if (rhs.len() && rhs.buffer())
N.B. it is a bit confusing that
buffer()
can return both a nullptr as well as a pointer when it is an empty string.This might cause other issues as well as the rest of the coding-style in the class suggests it is more like checking for a non-empty string.
N.B.2. This bug is likely present for quite a long time, so maybe also needs backporting?
Sketch
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: