Prevent double-free in rb_destroy
This commit is contained in:
parent
4a90d24069
commit
20b6e5c2fd
1 changed files with 14 additions and 1 deletions
13
ringbuf.c
13
ringbuf.c
|
@ -29,7 +29,18 @@ rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn,
|
||||||
void
|
void
|
||||||
rb_destroy(struct RingBuf *rb, FREE_T free_fn)
|
rb_destroy(struct RingBuf *rb, FREE_T free_fn)
|
||||||
{
|
{
|
||||||
|
if(rb->buffer) { // Prevent double-free
|
||||||
free_fn(rb->buffer);
|
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
|
void
|
||||||
|
@ -46,6 +57,8 @@ rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
|
||||||
if(rb->count == rb->capacity)
|
if(rb->count == rb->capacity)
|
||||||
return Full;
|
return Full;
|
||||||
|
|
||||||
|
assert(rb->buffer != NULL);
|
||||||
|
|
||||||
memcpy_fn(rb->write_head, item, rb->struct_size);
|
memcpy_fn(rb->write_head, item, rb->struct_size);
|
||||||
|
|
||||||
// Advance the write head
|
// Advance the write head
|
||||||
|
|
Loading…
Reference in a new issue