diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-01 23:04:25 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-01 23:04:25 -0500 |
commit | 3a18a6dcab45467e779e91c7b346aa3b148e8b9c (patch) | |
tree | ba0d7d521179c2d3fbd7d989eb2033cd2a86dbaf /source/fud_string.cpp | |
parent | 4ef88103f74a3a6e8e36ae9eff80f641e20bd1a1 (diff) |
Fix move assignment operators or delete them to prevent leaks.
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r-- | source/fud_string.cpp | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 97acb52..b10f6ee 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -95,14 +95,17 @@ String::String(String&& rhs) : m_length{rhs.m_length}, m_capacity{rhs.m_capacity String::~String() { - if (isLarge() && m_data != nullptr) { - fudFree(m_data); - m_data = nullptr; - } + cleanup(); } String& String::operator=(const String& rhs) { + if (this == &rhs) { + return *this; + } + + cleanup(); + m_length = rhs.m_length; m_capacity = rhs.m_capacity; if (rhs.valid()) { @@ -118,6 +121,8 @@ String& String::operator=(const String& rhs) String& String::operator=(String&& rhs) { + cleanup(); + m_length = rhs.m_length; m_capacity = rhs.m_capacity; if (rhs.isLarge()) { @@ -130,6 +135,14 @@ String& String::operator=(String&& rhs) return *this; } +void String::cleanup() +{ + if (isLarge() && m_data != nullptr) { + fudFree(m_data); + m_data = nullptr; + } +} + bool String::nullTerminated() const { return data() != nullptr && m_length < m_capacity && data()[m_length] == '\0'; |