# HG changeset patch # User Matti Hamalainen # Date 1673431252 -7200 # Node ID 3710c117b7c77fb1241c92b5b38ab73c559c782d # Parent 3091fd1987e9db2bf8887a3aa8fc5e7ace5b5e9e Add some NULL pointer checks. diff -r 3091fd1987e9 -r 3710c117b7c7 th_datastruct.c --- a/th_datastruct.c Mon Jan 09 09:49:13 2023 +0200 +++ b/th_datastruct.c Wed Jan 11 12:00:52 2023 +0200 @@ -342,15 +342,18 @@ void th_ringbuf_free(th_ringbuf_t *buf) { - for (size_t i = 0; i < buf->size; i++) + if (buf != NULL) { - buf->deallocator(buf->data[i]); - } + for (size_t i = 0; i < buf->size; i++) + { + buf->deallocator(buf->data[i]); + } - th_free(buf->data); + th_free(buf->data); - if (buf->is_allocated) - th_free(buf); + if (buf->is_allocated) + th_free(buf); + } } @@ -399,10 +402,13 @@ void th_growbuf_free(th_growbuf_t *buf) { - th_free(buf->data); + if (buf != NULL) + { + th_free(buf->data); - if (buf->is_allocated) - th_free(buf); + if (buf->is_allocated) + th_free(buf); + } } @@ -593,6 +599,9 @@ int th_ptrlist_init(th_ptrlist_t *list, const size_t ninitial, const size_t ngrow) { + if (list == NULL) + return THERR_NULLPTR; + memset(list, 0, sizeof(*list)); if (ngrow == 0) @@ -613,6 +622,9 @@ { int res; + if (list == NULL) + return THERR_NULLPTR; + if ((*list = th_malloc0(sizeof(th_ptrlist_t))) == NULL) return THERR_MALLOC; @@ -630,6 +642,9 @@ int th_ptrlist_append(th_ptrlist_t *list, void *data) { + if (list == NULL) + return THERR_NULLPTR; + if (list->nitems + 1 >= list->nallocated) { list->nallocated += list->ngrow; @@ -645,14 +660,17 @@ void th_ptrlist_free(th_ptrlist_t *list, void (*mdeallocator)(void *data)) { - if (mdeallocator != NULL) + if (list != NULL) { - for (size_t i = 0; i < list->nitems; i++) - mdeallocator(list->items[i]); - } + if (mdeallocator != NULL) + { + for (size_t i = 0; i < list->nitems; i++) + mdeallocator(list->items[i]); + } - th_free(list->items); + th_free(list->items); - if (list->is_allocated) - th_free(list); + if (list->is_allocated) + th_free(list); + } }