Mon, 20 Dec 2021 11:58:36 +0100
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 |
1.1 --- a/src/cx/linked_list.h Mon Dec 20 11:26:39 2021 +0100 1.2 +++ b/src/cx/linked_list.h Mon Dec 20 11:58:36 2021 +0100 1.3 @@ -94,7 +94,8 @@ 1.4 * @param index the search index 1.5 * @return the node found at the specified index 1.6 */ 1.7 -void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index); 1.8 +void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index) 1.9 +__attribute__((__nonnull__)); 1.10 1.11 /** 1.12 * Finds the first node in a linked list. 1.13 @@ -105,9 +106,9 @@ 1.14 * 1.15 * @param node a pointer to a node in the list 1.16 * @param loc_prev the location of the \c prev pointer 1.17 - * @return a pointer to the first node or \c NULL if \p node is \c NULL 1.18 */ 1.19 -void *cx_linked_list_first(void *node, ptrdiff_t loc_prev); 1.20 +void *cx_linked_list_first(void *node, ptrdiff_t loc_prev) 1.21 +__attribute__((__nonnull__)); 1.22 1.23 /** 1.24 * Finds the last node in a linked list. 1.25 @@ -118,9 +119,10 @@ 1.26 * 1.27 * @param node a pointer to a node in the list 1.28 * @param loc_next the location of the \c next pointer 1.29 - * @return a pointer to the last node or \c NULL if \p begin is \c NULL 1.30 + * @return a pointer to the last node 1.31 */ 1.32 -void *cx_linked_list_last(void *node, ptrdiff_t loc_next); 1.33 +void *cx_linked_list_last(void *node, ptrdiff_t loc_next) 1.34 +__attribute__((__nonnull__)); 1.35 1.36 /** 1.37 * Finds the predecessor of a node in case it is not linked. 1.38 @@ -132,13 +134,14 @@ 1.39 * @param node the successor of the node to find 1.40 * @return the node or \c NULL if \p node has no predecessor 1.41 */ 1.42 -void *cx_linked_list_prev(void *begin, ptrdiff_t loc_next, void *node); 1.43 +void *cx_linked_list_prev(void *begin, ptrdiff_t loc_next, void *node) 1.44 +__attribute__((__nonnull__)); 1.45 1.46 /** 1.47 * Adds a new node to a linked list. 1.48 * The node must not be part of any list already. 1.49 * 1.50 - * \remark One of the pointers \p begin and \p end may be \c NULL, but not both. 1.51 + * \remark One of the pointers \p begin or \p end may be \c NULL, but not both. 1.52 * 1.53 * @param begin a pointer to the begin node pointer (if your list has one) 1.54 * @param end a pointer to the end node pointer (if your list has one) 1.55 @@ -146,13 +149,14 @@ 1.56 * @param loc_next the location of a \c next pointer within your node struct (required) 1.57 * @param new_node a pointer to the node that shall be appended 1.58 */ 1.59 -void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node); 1.60 +void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) 1.61 +__attribute__((__nonnull__(5))); 1.62 1.63 /** 1.64 * Prepends a new node to a linked list. 1.65 * The node must not be part of any list already. 1.66 * 1.67 - * \remark One of the pointers \p begin and \p end may be \c NULL, but not both. 1.68 + * \remark One of the pointers \p begin or \p end may be \c NULL, but not both. 1.69 * 1.70 * @param begin a pointer to the begin node pointer (if your list has one) 1.71 * @param end a pointer to the end node pointer (if your list has one) 1.72 @@ -160,7 +164,8 @@ 1.73 * @param loc_next the location of a \c next pointer within your node struct (required) 1.74 * @param new_node a pointer to the node that shall be prepended 1.75 */ 1.76 -void cx_linked_list_prepend(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node); 1.77 +void cx_linked_list_prepend(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) 1.78 +__attribute__((__nonnull__(5))); 1.79 1.80 /** 1.81 * Removes a node from the linked list. 1.82 @@ -181,8 +186,8 @@ 1.83 * @param loc_next the location of a \c next pointer within your node struct (required) 1.84 * @param node the node to remove 1.85 */ 1.86 - void cx_linked_list_remove(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node) 1.87 - __attribute__ ((nonnull(5))); 1.88 +void cx_linked_list_remove(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node) 1.89 +__attribute__((__nonnull__(5))); 1.90 1.91 1.92 /** 1.93 @@ -225,7 +230,8 @@ 1.94 * @param cmp_func the compare function defining the sort order 1.95 */ 1.96 void cx_linked_list_sort(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, 1.97 - ptrdiff_t loc_data, int follow_ptr, CxListComparator cmp_func); 1.98 + ptrdiff_t loc_data, int follow_ptr, CxListComparator cmp_func) 1.99 +__attribute__((__nonnull__(1, 7))); 1.100 1.101 1.102 /** 1.103 @@ -236,7 +242,8 @@ 1.104 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 1.105 * @param loc_next the location of a \c next pointer within your node struct (required) 1.106 */ 1.107 -void cx_linked_list_reverse(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next); 1.108 +void cx_linked_list_reverse(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next) 1.109 +__attribute__((__nonnull__(1))); 1.110 1.111 #ifdef __cplusplus 1.112 } /* extern "C" */
2.1 --- a/src/linked_list.c Mon Dec 20 11:26:39 2021 +0100 2.2 +++ b/src/linked_list.c Mon Dec 20 11:58:36 2021 +0100 2.3 @@ -52,9 +52,8 @@ 2.4 } 2.5 2.6 void *cx_linked_list_last(void *node, ptrdiff_t loc_next) { 2.7 + assert(node != NULL); 2.8 assert(loc_next >= 0); 2.9 - if (node == NULL) 2.10 - return NULL; 2.11 2.12 void *cur = node; 2.13 void *last; 2.14 @@ -67,6 +66,7 @@ 2.15 2.16 void *cx_linked_list_prev(void *begin, ptrdiff_t loc_next, void *node) { 2.17 assert(begin != NULL); 2.18 + assert(node != NULL); 2.19 assert(loc_next >= 0); 2.20 if (begin == node) return NULL; 2.21 void *cur = begin; 2.22 @@ -79,12 +79,13 @@ 2.23 } 2.24 2.25 void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { 2.26 + assert(new_node != NULL); 2.27 assert(loc_next >= 0); 2.28 assert(CX_LL_PTR(new_node, loc_next) == NULL); 2.29 void *last; 2.30 if (end == NULL) { 2.31 assert(begin != NULL); 2.32 - last = cx_linked_list_last(*begin, loc_next); 2.33 + last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next); 2.34 } else { 2.35 last = *end; 2.36 } 2.37 @@ -110,13 +111,14 @@ 2.38 } 2.39 2.40 void cx_linked_list_prepend(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { 2.41 + assert(new_node != NULL); 2.42 assert(loc_next >= 0); 2.43 assert(CX_LL_PTR(new_node, loc_next) == NULL); 2.44 void *first; 2.45 if (begin == NULL) { 2.46 assert(end != NULL); 2.47 assert(loc_prev >= 0); 2.48 - first = cx_linked_list_first(*end, loc_prev); 2.49 + first = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev); 2.50 } else { 2.51 first = *begin; 2.52 }
3.1 --- a/test/test_list.c Mon Dec 20 11:26:39 2021 +0100 3.2 +++ b/test/test_list.c Mon Dec 20 11:58:36 2021 +0100 3.3 @@ -225,8 +225,6 @@ 3.4 } 3.5 3.6 void test_linked_list_first(void) { 3.7 - CU_ASSERT_PTR_NULL(cx_linked_list_first(NULL, 0)) 3.8 - 3.9 struct node { 3.10 int data; 3.11 void *prev; 3.12 @@ -243,8 +241,6 @@ 3.13 } 3.14 3.15 void test_linked_list_last(void) { 3.16 - CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0)) 3.17 - 3.18 struct node { 3.19 int data; 3.20 void *next;