From c8851cbda7da8ff579efb6603d2dff2576768863 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 24 Aug 2025 23:02:25 +0900 Subject: fix: *_reserve() may not reserve enough capacity --- src/common.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/common.c') diff --git a/src/common.c b/src/common.c index 4e747bb..8fb73f7 100644 --- a/src/common.c +++ b/src/common.c @@ -36,7 +36,9 @@ void strbuilder_init(StrBuilder* b) { void strbuilder_reserve(StrBuilder* b, size_t size) { if (size <= b->capacity) return; - b->capacity *= 2; + while (b->capacity < size) { + b->capacity *= 2; + } b->buf = realloc(b->buf, b->capacity * sizeof(char)); memset(b->buf + b->len, 0, (b->capacity - b->len) * sizeof(char)); } @@ -62,7 +64,9 @@ void strings_init(StrArray* strings) { void strings_reserve(StrArray* strings, size_t size) { if (size <= strings->capacity) return; - strings->capacity *= 2; + while (strings->capacity < size) { + strings->capacity *= 2; + } strings->data = realloc(strings->data, strings->capacity * sizeof(const char*)); memset(strings->data + strings->len, 0, (strings->capacity - strings->len) * sizeof(const char*)); } -- cgit v1.2.3-70-g09d2