add more nonnull attributes

Mon, 20 Dec 2021 11:58:36 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Dec 2021 11:58:36 +0100
changeset 478
599770bb6314
parent 477
73a93c7a56ae
child 479
a29bdd703e02

add more nonnull attributes

This also changes the contract for last/first in the sense that these
functions now also require a valid pointer.

src/cx/linked_list.h file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
test/test_list.c file | annotate | diff | comparison | revisions
--- a/src/cx/linked_list.h	Mon Dec 20 11:26:39 2021 +0100
+++ b/src/cx/linked_list.h	Mon Dec 20 11:58:36 2021 +0100
@@ -94,7 +94,8 @@
  * @param index the search index
  * @return the node found at the specified index
  */
-void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index);
+void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index)
+__attribute__((__nonnull__));
 
 /**
  * Finds the first node in a linked list.
@@ -105,9 +106,9 @@
  *
  * @param node a pointer to a node in the list
  * @param loc_prev the location of the \c prev pointer
- * @return a pointer to the first node or \c NULL if \p node is \c NULL
  */
-void *cx_linked_list_first(void *node, ptrdiff_t loc_prev);
+void *cx_linked_list_first(void *node, ptrdiff_t loc_prev)
+__attribute__((__nonnull__));
 
 /**
  * Finds the last node in a linked list.
@@ -118,9 +119,10 @@
  *
  * @param node a pointer to a node in the list
  * @param loc_next the location of the \c next pointer
- * @return a pointer to the last node or \c NULL if \p begin is \c NULL
+ * @return a pointer to the last node
  */
-void *cx_linked_list_last(void *node, ptrdiff_t loc_next);
+void *cx_linked_list_last(void *node, ptrdiff_t loc_next)
+__attribute__((__nonnull__));
 
 /**
  * Finds the predecessor of a node in case it is not linked.
@@ -132,13 +134,14 @@
  * @param node the successor of the node to find
  * @return the node or \c NULL if \p node has no predecessor
  */
-void *cx_linked_list_prev(void *begin, ptrdiff_t loc_next, void *node);
+void *cx_linked_list_prev(void *begin, ptrdiff_t loc_next, void *node)
+__attribute__((__nonnull__));
 
 /**
  * Adds a new node to a linked list.
  * The node must not be part of any list already.
  *
- * \remark One of the pointers \p begin and \p end may be \c NULL, but not both.
+ * \remark One of the pointers \p begin or \p end may be \c NULL, but not both.
  *
  * @param begin a pointer to the begin node pointer (if your list has one)
  * @param end a pointer to the end node pointer (if your list has one)
@@ -146,13 +149,14 @@
  * @param loc_next the location of a \c next pointer within your node struct (required)
  * @param new_node a pointer to the node that shall be appended
  */
-void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node);
+void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node)
+__attribute__((__nonnull__(5)));
 
 /**
  * Prepends a new node to a linked list.
  * The node must not be part of any list already.
  *
- * \remark One of the pointers \p begin and \p end may be \c NULL, but not both.
+ * \remark One of the pointers \p begin or \p end may be \c NULL, but not both.
  *
  * @param begin a pointer to the begin node pointer (if your list has one)
  * @param end a pointer to the end node pointer (if your list has one)
@@ -160,7 +164,8 @@
  * @param loc_next the location of a \c next pointer within your node struct (required)
  * @param new_node a pointer to the node that shall be prepended
  */
-void cx_linked_list_prepend(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node);
+void cx_linked_list_prepend(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node)
+__attribute__((__nonnull__(5)));
 
 /**
  * Removes a node from the linked list.
@@ -181,8 +186,8 @@
  * @param loc_next the location of a \c next pointer within your node struct (required)
  * @param node the node to remove
  */
- void cx_linked_list_remove(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node)
- __attribute__ ((nonnull(5)));
+void cx_linked_list_remove(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node)
+__attribute__((__nonnull__(5)));
 
 
 /**
@@ -225,7 +230,8 @@
  * @param cmp_func the compare function defining the sort order
  */
 void cx_linked_list_sort(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next,
-                         ptrdiff_t loc_data, int follow_ptr, CxListComparator cmp_func);
+                         ptrdiff_t loc_data, int follow_ptr, CxListComparator cmp_func)
+__attribute__((__nonnull__(1, 7)));
 
 
 /**
@@ -236,7 +242,8 @@
  * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
  * @param loc_next the location of a \c next pointer within your node struct (required)
  */
-void cx_linked_list_reverse(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next);
+void cx_linked_list_reverse(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next)
+__attribute__((__nonnull__(1)));
 
 #ifdef __cplusplus
 } /* extern "C" */
--- a/src/linked_list.c	Mon Dec 20 11:26:39 2021 +0100
+++ b/src/linked_list.c	Mon Dec 20 11:58:36 2021 +0100
@@ -52,9 +52,8 @@
 }
 
 void *cx_linked_list_last(void *node, ptrdiff_t loc_next) {
+    assert(node != NULL);
     assert(loc_next >= 0);
-    if (node == NULL)
-        return NULL;
 
     void *cur = node;
     void *last;
@@ -67,6 +66,7 @@
 
 void *cx_linked_list_prev(void *begin, ptrdiff_t loc_next, void *node) {
     assert(begin != NULL);
+    assert(node != NULL);
     assert(loc_next >= 0);
     if (begin == node) return NULL;
     void *cur = begin;
@@ -79,12 +79,13 @@
 }
 
 void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
+    assert(new_node != NULL);
     assert(loc_next >= 0);
     assert(CX_LL_PTR(new_node, loc_next) == NULL);
     void *last;
     if (end == NULL) {
         assert(begin != NULL);
-        last = cx_linked_list_last(*begin, loc_next);
+        last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
     } else {
         last = *end;
     }
@@ -110,13 +111,14 @@
 }
 
 void cx_linked_list_prepend(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
+    assert(new_node != NULL);
     assert(loc_next >= 0);
     assert(CX_LL_PTR(new_node, loc_next) == NULL);
     void *first;
     if (begin == NULL) {
         assert(end != NULL);
         assert(loc_prev >= 0);
-        first = cx_linked_list_first(*end, loc_prev);
+        first = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
     } else {
         first = *begin;
     }
--- a/test/test_list.c	Mon Dec 20 11:26:39 2021 +0100
+++ b/test/test_list.c	Mon Dec 20 11:58:36 2021 +0100
@@ -225,8 +225,6 @@
 }
 
 void test_linked_list_first(void) {
-    CU_ASSERT_PTR_NULL(cx_linked_list_first(NULL, 0))
-
     struct node {
         int data;
         void *prev;
@@ -243,8 +241,6 @@
 }
 
 void test_linked_list_last(void) {
-    CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0))
-
     struct node {
         int data;
         void *next;

mercurial