From b9ee6745502012d5c7399fc9996ed2d8007d43cd Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 22 Feb 2026 06:09:16 +0300 Subject: fix size counting bug in arena__coalesce_from_fast --- src/arena.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/arena.c b/src/arena.c index bd93eeb..d02694d 100644 --- a/src/arena.c +++ b/src/arena.c @@ -23,6 +23,12 @@ chunk_size (struct arena__chunk *chunk) return chunk->end - chunk->data; } +static size_t +chunk_used (struct arena__chunk *chunk) +{ + return chunk->free - chunk->data; +} + static struct arena__chunk * new_chunk (size_t size) { @@ -206,11 +212,13 @@ arena__coalesce_from_fast (ARENA arena, struct arena__checkpoint checkpoint, // copy old chunks into new chunk size_t other_size = checkpoint.tail->free - checkpoint.free; - memcpy (chunk->free, checkpoint.free, other_size); - chunk->free += other_size; + if (other_size) { + memcpy (chunk->free, checkpoint.free, other_size); + chunk->free += other_size; + } for (struct arena__chunk *other_chunk = checkpoint.tail->next; other_chunk; other_chunk = other_chunk->next) { - other_size = chunk_size (other_chunk); + other_size = chunk_used (other_chunk); memcpy (chunk->free, other_chunk->data, other_size); chunk->free += other_size; } -- cgit v1.3