diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-02 12:19:53 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-02 12:19:53 -0500 |
commit | c2776f660e00dd9c0ec4cc50369b2a9cf2ed3e70 (patch) | |
tree | 0b15f8bc3c57ab5e65692d542d2709008066c582 /source/fud_string.cpp | |
parent | 7eea7cd5e5b451de9db5bd289f8b5d152d5803f5 (diff) |
Fix error in string reserve.
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r-- | source/fud_string.cpp | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/source/fud_string.cpp b/source/fud_string.cpp index c1937be..10352ad 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -148,6 +148,10 @@ void String::cleanup() FudStatus String::resize(size_t newCapacity) { + if (!valid()) { + return FudStatus::StringInvalid; + } + if (m_length >= newCapacity) { return FudStatus::OperationInvalid; } @@ -164,16 +168,28 @@ FudStatus String::resize(size_t newCapacity) fudFree(m_data); m_data = nullptr; copyMem(m_buffer, temp); - + data()[m_length] = '\0'; return FudStatus::Success; } - auto* newData = fudRealloc(m_data, newCapacity); - if (newData == nullptr) { - return FudStatus::AllocFailure; + utf8* newData; + if (isLarge()) { + newData = static_cast<utf8*>(fudRealloc(m_data, newCapacity)); + if (newData == nullptr) { + return FudStatus::AllocFailure; + } + } else { + newData = static_cast<utf8*>(fudAlloc(newCapacity)); + if (newData == nullptr) { + return FudStatus::AllocFailure; + } + static_cast<void>(copyMem(newData, newCapacity, m_buffer.data(), length())); } + m_capacity = newCapacity; - m_data = static_cast<utf8*>(newData); + m_data = newData; + data()[m_length] = '\0'; + fudAssert(valid()); return FudStatus::Success; } @@ -184,7 +200,7 @@ bool String::nullTerminated() const bool String::valid() const { - return nullTerminated() && m_length < m_capacity; + return nullTerminated(); } bool String::utf8Valid() const @@ -342,13 +358,11 @@ FudStatus String::append(StringView source) } const auto newLength = length() + source.length(); - if (newLength < length()) - { + if (newLength < length()) { return FudStatus::OperationInvalid; } const auto newSize = newLength + 1; - if (newSize < newLength) - { + if (newSize < newLength) { return FudStatus::OperationInvalid; } if (newSize >= m_capacity) { |