AK: Fix accidentally-quadratic behavior in StringBuilder

Found by OSS Fuzz:

Related commit: 3908a49661

Co-authored-by: Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
This commit is contained in:
Gunnar Beutner 2021-05-31 00:55:51 +02:00 committed by Ali Mohammad Pur
parent 8f755c9d07
commit 4c32a128ef

View file

@ -21,10 +21,11 @@ inline void StringBuilder::will_append(size_t size)
Checked<size_t> needed_capacity = m_length;
needed_capacity += size;
VERIFY(!needed_capacity.has_overflow());
// Prefer to completely use the existing capacity first
if (needed_capacity <= m_buffer.capacity())
return;
Checked<size_t> expanded_capacity = needed_capacity;
// Prefer to completely use the inline buffer first
if (needed_capacity > inline_capacity)
expanded_capacity *= 2;
expanded_capacity *= 2;
VERIFY(!expanded_capacity.has_overflow());
m_buffer.ensure_capacity(expanded_capacity.value());
}