commit f88627142306a660af3420ddc42ceda8c2b148bf
parent 476ac4e261203747d3f343ff0cb1c6895ee8e91c
Author: finwo <finwo@pm.me>
Date: Wed, 11 Nov 2026 14:25:33 +0100
Fix heap overflow when appending more than double the buffer capacity
Diffstat:
2 files changed, 66 insertions(+), 2 deletions(-)
diff --git a/src/buf.c b/src/buf.c
@@ -79,9 +79,9 @@ int buf_append(struct buf *subject, const char *source, const size_t length) {
subject->dat = subject->alloc;
}
- // Grow buffer if still not enough space
+ // Grow buffer if still not enough space: double, but never less than needed
if ((subject->len + length) > subject->cap) {
- subject->cap = MAX(32, subject->cap * 2);
+ subject->cap = MAX(subject->len + length, MAX(32, subject->cap * 2));
subject->alloc = _realloc(subject->alloc, subject->cap);
subject->dat = subject->alloc;
}
diff --git a/test/0001-large-append.c b/test/0001-large-append.c
@@ -0,0 +1,64 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "assert.h"
+#include "buf.h"
+
+#define CHUNK_SIZE 4096
+
+static char chunk[CHUNK_SIZE];
+
+// Appending a block larger than double the current capacity must grow the
+// buffer to fit it, not just double the old capacity. The growth path in
+// buf_append used to compute MAX(32, cap * 2) without regard for the pending
+// append length, overflowing the allocation on every such append.
+void test_large_append_grows_to_fit() {
+ struct buf subject;
+ memset(&subject, 0, sizeof subject);
+
+ ASSERT("Small append allocates initial capacity", buf_append(&subject, "hello", 5) == 0);
+ ASSERT("Initial capacity is 32", subject.cap == 32);
+
+ memset(chunk, 'A', sizeof chunk);
+ ASSERT("Large append succeeds", buf_append(&subject, chunk, sizeof chunk) == 0);
+
+ ASSERT("Capacity covers length after large append", subject.cap >= subject.len);
+ ASSERT("Length includes large append", subject.len == 5 + CHUNK_SIZE);
+ ASSERT("Original data survived large append", memcmp(subject.dat, "hello", 5) == 0);
+ ASSERT("Large appended data is intact", memcmp(subject.dat + 5, chunk, CHUNK_SIZE) == 0);
+
+ buf_clear(&subject);
+}
+
+// Same bug via the consume path: after buf_consume, the eject step reclaims
+// head space but may still leave too little capacity, hitting the grow step.
+void test_large_append_after_consume() {
+ struct buf subject;
+ memset(&subject, 0, sizeof subject);
+
+ memset(chunk, 'B', sizeof chunk);
+ ASSERT("Fill to initial capacity", buf_append(&subject, chunk, 5) == 0);
+ ASSERT("Fill past initial capacity", buf_append(&subject, chunk, 40) == 0);
+ ASSERT("Capacity doubled on overflow", subject.cap == 64);
+
+ ASSERT("Consume head of buffer", buf_consume(&subject, 30) == 30);
+ ASSERT("Data pointer moved past consumed bytes", subject.dat - subject.alloc == 30);
+
+ ASSERT("Large append into consumed buffer succeeds", buf_append(&subject, chunk, sizeof chunk) == 0);
+
+ ASSERT("Capacity covers length after large append", subject.cap >= subject.len);
+ ASSERT("Length includes large append", subject.len == 15 + CHUNK_SIZE);
+ ASSERT("Data pointer reset to allocation start", subject.dat == subject.alloc);
+ ASSERT("Remaining data survived large append", memcmp(subject.dat, chunk, 15) == 0);
+ ASSERT("Large appended data is intact", memcmp(subject.dat + 15, chunk, CHUNK_SIZE) == 0);
+
+ buf_clear(&subject);
+}
+
+int main(int argc, const char *argv[]) {
+ buf_set_allocator(realloc, free);
+
+ RUN(test_large_append_grows_to_fit);
+ RUN(test_large_append_after_consume);
+ return TEST_REPORT();
+}