Prevent double-free in rb_destroy

This commit is contained in:
Imbus 2024-12-25 14:02:49 +01:00
parent 4a90d24069
commit 20b6e5c2fd

View file

@ -29,7 +29,18 @@ rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn,
void
rb_destroy(struct RingBuf *rb, FREE_T free_fn)
{
free_fn(rb->buffer);
if(rb->buffer) { // Prevent double-free
free_fn(rb->buffer);
rb->buffer = NULL;
rb->buffer_end = NULL;
rb->struct_size = 0;
rb->capacity = 0;
rb->count = 0;
rb->write_head = NULL;
rb->read_head = NULL;
}
}
void
@ -46,6 +57,8 @@ rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
if(rb->count == rb->capacity)
return Full;
assert(rb->buffer != NULL);
memcpy_fn(rb->write_head, item, rb->struct_size);
// Advance the write head