diff --git a/include/internal/list.h b/include/internal/list.h index 8cb71e1fa4..fdd356c407 100644 --- a/include/internal/list.h +++ b/include/internal/list.h @@ -12,6 +12,13 @@ # pragma once # include +# include + +# ifdef NDEBUG +# define OSSL_LIST_DBG(x) +# else +# define OSSL_LIST_DBG(x) x; +# endif /* Define a list structure */ # define OSSL_LIST(name) OSSL_LIST_ ## name @@ -20,6 +27,7 @@ # define OSSL_LIST_MEMBER(name, type) \ struct { \ type *next, *prev; \ + OSSL_LIST_DBG(struct ossl_list_st_ ## name *list) \ } ossl_list_ ## name # define DEFINE_LIST_OF(name, type) \ @@ -52,26 +60,38 @@ static ossl_unused ossl_inline type * \ ossl_list_##name##_head(const OSSL_LIST(name) *list) \ { \ + assert(list->alpha == NULL \ + || list->alpha->ossl_list_ ## name.list == list); \ return list->alpha; \ } \ static ossl_unused ossl_inline type * \ ossl_list_##name##_tail(const OSSL_LIST(name) *list) \ { \ + assert(list->omega == NULL \ + || list->omega->ossl_list_ ## name.list == list); \ return list->omega; \ } \ static ossl_unused ossl_inline type * \ ossl_list_##name##_next(const type *elem) \ { \ + assert(elem->ossl_list_ ## name.next == NULL \ + || elem->ossl_list_ ## name.next \ + ->ossl_list_ ## name.prev == elem); \ return elem->ossl_list_ ## name.next; \ } \ static ossl_unused ossl_inline type * \ ossl_list_##name##_prev(const type *elem) \ { \ + assert(elem->ossl_list_ ## name.prev == NULL \ + || elem->ossl_list_ ## name.prev \ + ->ossl_list_ ## name.next == elem); \ return elem->ossl_list_ ## name.prev; \ } \ static ossl_unused ossl_inline void \ ossl_list_##name##_remove(OSSL_LIST(name) *list, type *elem) \ { \ + assert(elem->ossl_list_ ## name.list == list); \ + OSSL_LIST_DBG(elem->ossl_list_ ## name.list = NULL) \ if (list->alpha == elem) \ list->alpha = elem->ossl_list_ ## name.next; \ if (list->omega == elem) \ @@ -89,6 +109,8 @@ static ossl_unused ossl_inline void \ ossl_list_##name##_insert_head(OSSL_LIST(name) *list, type *elem) \ { \ + assert(elem->ossl_list_ ## name.list == NULL); \ + OSSL_LIST_DBG(elem->ossl_list_ ## name.list = list) \ if (list->alpha != NULL) \ list->alpha->ossl_list_ ## name.prev = elem; \ elem->ossl_list_ ## name.next = list->alpha; \ @@ -101,6 +123,8 @@ static ossl_unused ossl_inline void \ ossl_list_##name##_insert_tail(OSSL_LIST(name) *list, type *elem) \ { \ + assert(elem->ossl_list_ ## name.list == NULL); \ + OSSL_LIST_DBG(elem->ossl_list_ ## name.list = list) \ if (list->omega != NULL) \ list->omega->ossl_list_ ## name.next = elem; \ elem->ossl_list_ ## name.prev = list->omega; \ @@ -114,6 +138,8 @@ ossl_list_##name##_insert_before(OSSL_LIST(name) *list, type *e, \ type *elem) \ { \ + assert(elem->ossl_list_ ## name.list == NULL); \ + OSSL_LIST_DBG(elem->ossl_list_ ## name.list = list) \ elem->ossl_list_ ## name.next = e; \ elem->ossl_list_ ## name.prev = e->ossl_list_ ## name.prev; \ if (e->ossl_list_ ## name.prev != NULL) \ @@ -127,6 +153,8 @@ ossl_list_##name##_insert_after(OSSL_LIST(name) *list, type *e, \ type *elem) \ { \ + assert(elem->ossl_list_ ## name.list == NULL); \ + OSSL_LIST_DBG(elem->ossl_list_ ## name.list = list) \ elem->ossl_list_ ## name.prev = e; \ elem->ossl_list_ ## name.next = e->ossl_list_ ## name.next; \ if (e->ossl_list_ ## name.next != NULL) \ diff --git a/test/list_test.c b/test/list_test.c index 000f7beec3..db288d1d08 100644 --- a/test/list_test.c +++ b/test/list_test.c @@ -43,6 +43,8 @@ static int test_fizzbuzz(void) return 0; for (i = 1; i < nelem; i++) { + ossl_list_fizz_init_elem(elem + i); + ossl_list_buzz_init_elem(elem + i); elem[i].n = i; if (i % 3 == 0) { ossl_list_fizz_insert_tail(&a, elem + i); @@ -99,8 +101,10 @@ static int test_insert(void) int n = 1; ossl_list_int_init(&l); - for (i = 0; i < OSSL_NELEM(elem); i++) - elem[i].n = i; + for (i = 0; i < OSSL_NELEM(elem); i++) { + ossl_list_int_init_elem(elem + i); + elem[i].n = i; + } /* Check various insert options - head, tail, middle */ ossl_list_int_insert_head(&l, elem + 3); /* 3 */