From 3a18a6dcab45467e779e91c7b346aa3b148e8b9c Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Tue, 1 Oct 2024 23:04:25 -0500 Subject: Fix move assignment operators or delete them to prevent leaks. --- source/fud_string.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'source/fud_string.cpp') 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'; -- cgit v1.2.3