arduino / arduino/ArduinoCore-API

String move() and String(String &&rval) breaks operation of reserve()

Open
#161 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
306
Forks
150
PR merge metrics
No merged PRs in 30d

Description

When #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
operator = uses move() to just update the buffer pointer of the destination
This ignores any reserve() the user has made to ensure the memory is not unnecessarily fragmented.
String(String &&rval) has a similar problem

move() should first check the capacity of the destination and if there is sufficient space copy the source to the destination
String(String &&rval) should use move()

A suggested move() is

```
void String::move(String &rhs) {
if (this != &rhs) {
if (capacity > rhs.size) {
copy(rhs.buffer,rhs.size);
} else {
free(buffer);
buffer = rhs.buffer;
len = rhs.len;
capacity = rhs.capacity;
}
rhs.buffer = NULL;
rhs.len = 0;
rhs.capacity = 0;
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Locate the C++11 String implementation and inspect move(), operator=, the String(String &&rval) constructor, and reserve() behavior. Verify how a reserved destination behaves during a move, then confirm that the constructor follows the intended move path and that moved-from strings remain valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.