changeset 752:3710c117b7c7

Add some NULL pointer checks.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 11 Jan 2023 12:00:52 +0200
parents 3091fd1987e9
children 64cb2b1777a9
files th_datastruct.c
diffstat 1 files changed, 34 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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);
+    }
 }