AK: Use ByteBuffer::append for the StringBuilder class

Previously the StringBuilder class would use memcpy() to write
directly into the ByteBuffer's buffer. Instead we should use the
append() method which ensures we don't overrun the buffer.
This commit is contained in:
Gunnar Beutner 2021-05-31 00:54:43 +02:00 committed by Ali Mohammad Pur
parent 425bfabd66
commit 8f755c9d07
Notes: sideshowbarker 2024-07-18 17:08:11 +09:00

View file

@ -26,12 +26,12 @@ inline void StringBuilder::will_append(size_t size)
if (needed_capacity > inline_capacity)
expanded_capacity *= 2;
VERIFY(!expanded_capacity.has_overflow());
m_buffer.resize(expanded_capacity.value());
m_buffer.ensure_capacity(expanded_capacity.value());
}
StringBuilder::StringBuilder(size_t initial_capacity)
: m_buffer(decltype(m_buffer)::create_uninitialized(initial_capacity))
{
m_buffer.ensure_capacity(initial_capacity);
}
void StringBuilder::append(const StringView& str)
@ -39,7 +39,7 @@ void StringBuilder::append(const StringView& str)
if (str.is_empty())
return;
will_append(str.length());
memcpy(data() + m_length, str.characters_without_null_termination(), str.length());
m_buffer.append(str.characters_without_null_termination(), str.length());
m_length += str.length();
}
@ -51,7 +51,7 @@ void StringBuilder::append(const char* characters, size_t length)
void StringBuilder::append(char ch)
{
will_append(1);
data()[m_length] = ch;
m_buffer.append(&ch, 1);
m_length += 1;
}