From a317c3de5d6107e4b7dbd9b6bcf7ed39fb2a3ad4 Mon Sep 17 00:00:00 2001 From: matt335672 <30179339+matt335672@users.noreply.github.com> Date: Mon, 6 Mar 2023 15:42:33 +0000 Subject: [PATCH] Fix regression in list module --- common/list.c | 13 ++++++------- tests/common/test_list_calls.c | 5 +++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/common/list.c b/common/list.c index 73035ad8..79375e03 100644 --- a/common/list.c +++ b/common/list.c @@ -204,6 +204,7 @@ list_remove_item(struct list *self, int index) int list_insert_item(struct list *self, int index, tbus item) { + int i; if (index > self->count) { @@ -219,16 +220,14 @@ list_insert_item(struct list *self, int index, tbus item) return 0; } - self->count++; - if (self->count >= 2) + // Move all the items above this location up one + for (i = self->count ; i > index ; --i) { - unsigned int i; - for (i = (self->count - 2); i >= (unsigned int)index; i--) - { - self->items[i + 1] = self->items[i]; - } + self->items[i] = self->items[i - 1]; } + self->count++; + self->items[index] = item; return 1; } diff --git a/tests/common/test_list_calls.c b/tests/common/test_list_calls.c index 917c09a7..a4a48fd7 100644 --- a/tests/common/test_list_calls.c +++ b/tests/common/test_list_calls.c @@ -42,6 +42,11 @@ START_TEST(test_list__simple) val = list_get_item(lst, 10); ck_assert_int_eq(val, 10); + list_insert_item(lst, 0, 99); + ck_assert_int_eq(lst->count, TEST_LIST_SIZE + 1); + val = list_get_item(lst, 10); + ck_assert_int_eq(val, 9); + list_clear(lst); ck_assert_int_eq(lst->count, 0); list_delete(lst);