fix mixed up cases in cx_ll_at()

Mon, 27 Sep 2021 18:57:17 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Sep 2021 18:57:17 +0200
changeset 440
003aa0a78e1e
parent 439
9a5adedd6de6
child 441
7d5a06e32aa8
child 442
310019ddfe4e

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) {

mercurial