AK: Add an option to zero-fill ByteBuffer data upon growth

This is to avoid UB in cases where we need to be able to read from the
buffer immediately after resizing it.
This commit is contained in:
Timothy Flynn 2023-12-27 10:11:20 -05:00 committed by Andreas Kling
commit 507a5d8a07
Notes: sideshowbarker 2024-07-17 01:27:18 +09:00
2 changed files with 30 additions and 3 deletions

View file

@ -46,6 +46,23 @@ TEST_CASE(byte_buffer_vector_contains_slow_bytes)
EXPECT_EQ(vector.contains_slow(c), true);
}
TEST_CASE(zero_fill_new_elements_on_growth)
{
auto buffer = MUST(ByteBuffer::create_uninitialized(5));
buffer.span().fill(1);
EXPECT_EQ(buffer.span(), (Array<u8, 5> { 1, 1, 1, 1, 1 }));
buffer.resize(8, ByteBuffer::ZeroFillNewElements::Yes);
EXPECT_EQ(buffer.span(), (Array<u8, 8> { 1, 1, 1, 1, 1, 0, 0, 0 }));
buffer.span().fill(2);
EXPECT_EQ(buffer.span(), (Array<u8, 8> { 2, 2, 2, 2, 2, 2, 2, 2 }));
buffer.resize(10, ByteBuffer::ZeroFillNewElements::Yes);
EXPECT_EQ(buffer.span(), (Array<u8, 10> { 2, 2, 2, 2, 2, 2, 2, 2, 0, 0 }));
}
BENCHMARK_CASE(append)
{
ByteBuffer bb;