Mon, 27 Sep 2021 18:57:17 +0200
fix mixed up cases in cx_ll_at()
src/linked_list.c | file | annotate | diff | comparison | revisions |
--- a/src/linked_list.c Mon Sep 27 18:50:07 2021 +0200 +++ b/src/linked_list.c Mon Sep 27 18:57:17 2021 +0200 @@ -152,12 +152,14 @@ static void *cx_ll_at(cx_list_s *list, size_t index) { cx_linked_list *ll = (cx_linked_list *) list; struct cx_linked_list_node *node; - if (index > list->size / 2) { - node = cx_linked_list_at(ll->begin, 0, CX_LL_LOC_NEXT, index); + if (index >= list->size) { + node = NULL; + } else if (index > list->size / 2) { + node = cx_linked_list_at(ll->end, list->size, CX_LL_LOC_PREV, index); } else { - node = cx_linked_list_at(ll->end, list->size, CX_LL_LOC_PREV, index); + node = cx_linked_list_at(ll->begin, 0, CX_LL_LOC_NEXT, index); } - return &node->payload; + return node == NULL ? NULL : &node->payload; } static size_t cx_ll_find(cx_list_s *list, void *elem) {