arc80 / plywood

A multimedia development kit for C++

Home Page:https://plywood.arc80.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Assigning a view of a String to itself corrupts the String

jlaumon opened this issue · comments

Hi,

I just noticed doing something like:

String s = "something";
s = s.shortenedBy(1);

gets you a dangling pointer inside s, because the operator= destroys this before assigning the new value

Copy assignment operator. If this `String` already owns a memory block, it is destroyed. A new
memory block is allocated on the heap and the contents of `other` are copied into it.
*/
template <typename = void>
PLY_INLINE void operator=(StringView other) {
this->~String();
new (this) String{other};
}

Actually, now that I'm writing this, I see that Array does the same (and Array::append too, the parameter is a ref that might point inside the array that gets resized 🙊).

Sorry, I feel bad for reporting this without a PR for fixing it 😅

Great catch! Will fix soon, thanks.

For Array::append and Array::extend, I decided to disallow passing the Array's own items back into itself.

Sounds fair enough!

Thanks a lot for fixing that so fast!