diff options
author | Rémi Verschelde <remi@verschelde.fr> | 2022-08-22 22:24:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-22 22:24:12 +0200 |
commit | dac25e5c4c79ca2b977905fb4e27b9ca1156f2ff (patch) | |
tree | 7d7cf163526ef81211e2c33aca260ecb8acad48d | |
parent | 09b012a409ef9ba469444654c20057d6239c95ed (diff) | |
parent | 1324ca5eb050334b2ad2640ed2256bd71bd54bbd (diff) |
Merge pull request #64571 from kleonc/string-fix-self-add-assign
-rw-r--r-- | core/string/ustring.cpp | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index e93375bff7..13be7516d5 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -531,10 +531,12 @@ String &String::operator+=(const String &p_str) { resize(lhs_len + rhs_len + 1); - const char32_t *src = p_str.get_data(); + const char32_t *src = p_str.ptr(); char32_t *dst = ptrw() + lhs_len; - memcpy(dst, src, (rhs_len + 1) * sizeof(char32_t)); + // Don't copy the terminating null with `memcpy` to avoid undefined behavior when string is being added to itself (it would overlap the destination). + memcpy(dst, src, rhs_len * sizeof(char32_t)); + *(dst + rhs_len) = _null; return *this; } |