diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-08-24 23:02:25 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-08-24 23:02:25 +0900 |
| commit | c8851cbda7da8ff579efb6603d2dff2576768863 (patch) | |
| tree | 27e0b824548db547ae94866575c33089f00399d4 /src/common.c | |
| parent | 917ce0573ea9cc07195cc27a21b5b5374abbf6f2 (diff) | |
| download | ducc-c8851cbda7da8ff579efb6603d2dff2576768863.tar.gz ducc-c8851cbda7da8ff579efb6603d2dff2576768863.tar.zst ducc-c8851cbda7da8ff579efb6603d2dff2576768863.zip | |
fix: *_reserve() may not reserve enough capacity
Diffstat (limited to 'src/common.c')
| -rw-r--r-- | src/common.c | 8 |
1 files changed, 6 insertions, 2 deletions
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*)); } |
