1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:47:35 +00:00

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
parent 759c8ccc55
commit 507a5d8a07
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;