Mercurial > hg > th-libs
changeset 628:140e2272471c
Add some error checking in th_ringbuf_new().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 17 Jan 2020 19:22:04 +0200 |
parents | 964657ad0960 |
children | b695eb769e30 |
files | th_datastruct.c |
diffstat | 1 files changed, 14 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/th_datastruct.c Fri Jan 17 19:14:36 2020 +0200 +++ b/th_datastruct.c Fri Jan 17 19:22:04 2020 +0200 @@ -324,9 +324,21 @@ */ th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data)) { - th_ringbuf_t *res = th_malloc0(sizeof(th_ringbuf_t)); + th_ringbuf_t *res; + + // Must have at least 2 elements + if (size < 2) + return NULL; - res->data = (char **) th_calloc(size, sizeof(char *)); + if ((res = th_malloc0(sizeof(th_ringbuf_t))) == NULL) + return NULL; + + if ((res->data = (char **) th_calloc(size, sizeof(char *))) == NULL) + { + th_free(res); + return NULL; + } + res->size = size; res->n = 0; res->deallocator = mdeallocator;