Skip to content

Commit

Permalink
data/buffer.c: buffer_realloc() now handles alloc_size == 0 specially.
Browse files Browse the repository at this point in the history
When shrinking to alloc_size == 0, we now free the actual buffer completely.
  • Loading branch information
mity committed Dec 5, 2023
1 parent cf79a3c commit db02db8
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions data/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,22 @@ buffer_realloc(BUFFER* buf, size_t alloc)
{
void* tmp;

if(alloc == buf->alloc)
return 0;

if(alloc == 0) {
free(buf->data);
buffer_init(buf);
return 0;
}

tmp = realloc(buf->data, alloc);
if(tmp == NULL && alloc > 0)
if(tmp == NULL)
return -1;

buf->data = tmp;
buf->alloc = alloc;
if(buf->size > alloc)
if(alloc < buf->size)
buf->size = alloc;
return 0;
}
Expand Down

0 comments on commit db02db8

Please sign in to comment.