# HG changeset patch # User Matti Hamalainen # Date 1579281724 -7200 # Node ID 140e2272471c716ec01e4895569ff1d99afc077c # Parent 964657ad0960f25078b51ddbe1671b4a7e522c90 Add some error checking in th_ringbuf_new(). diff -r 964657ad0960 -r 140e2272471c th_datastruct.c --- 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;