From 3fae01e7fa7562e2ba9341cde9bf78260d2b3cde Mon Sep 17 00:00:00 2001 From: Dave Allison Date: Wed, 30 Sep 2026 10:21:59 -0700 Subject: [PATCH] Fix vector pushes --- MODULE.bazel | 2 +- toolbelt/payload_buffer.h | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 2927f4d..3891aad 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,6 +1,6 @@ module( name = "cpp_toolbelt", - version = "2.1.3", + version = "2.1.4", ) bazel_dep(name = "platforms", version = "1.0.0") diff --git a/toolbelt/payload_buffer.h b/toolbelt/payload_buffer.h index 08b1775..b1e6d6f 100644 --- a/toolbelt/payload_buffer.h +++ b/toolbelt/payload_buffer.h @@ -495,10 +495,14 @@ inline void PayloadBuffer::VectorPush(PayloadBuffer **self, VectorHeader *hdr, // the allocated block header (before the start of the memory) uint32_t *block = (*self)->ToAddress(hdr->data); uint32_t current_size = DecodeSize(block); - if (current_size == total_size) { - // Need to double the size of the memory. - void *vecp = Realloc(self, block, 2 * hdr->num_elements * sizeof(T), true, - enable_small_block); + const uint32_t needed = (hdr->num_elements + 1) * sizeof(T); + if (needed > current_size) { + // Grow to fit the next element; at least double prior capacity. + const uint32_t min_capacity = + 2u * static_cast(sizeof(T)); + const uint32_t new_size = std::max( + needed, std::max(current_size * 2, min_capacity)); + void *vecp = Realloc(self, block, new_size, true, enable_small_block); VectorHeader *new_hdr = (*self)->ToAddress(hdr_offset); new_hdr->data = (*self)->ToOffset(vecp); hdr = new_hdr;