[PATCH v3 2/2] list: add KUnit test for list_cut_before() empty list case
From: Ziran Zhang
Date: Fri Sep 18 2026 - 04:15:27 EST
Add a test case covering list_cut_before() with an empty head
and an entry not on the list. Without the empty list guard,
the call would corrupt the head list.
Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
Signed-off-by: Ziran Zhang <zhangcoder@xxxxxxxx>
---
lib/tests/list-test.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/lib/tests/list-test.c b/lib/tests/list-test.c
index 6d9227a2b..f54facdb7 100644
--- a/lib/tests/list-test.c
+++ b/lib/tests/list-test.c
@@ -446,6 +446,23 @@ static void list_test_list_cut_before(struct kunit *test)
KUNIT_EXPECT_EQ(test, i, 3);
}
+static void list_test_list_cut_before_empty(struct kunit *test)
+{
+ LIST_HEAD(entry);
+ LIST_HEAD(head);
+ LIST_HEAD(list);
+
+ /*
+ * entry is initialized but not on head. With an empty head
+ * this used to corrupt the list.
+ */
+ list_cut_before(&list, &head, &entry);
+
+ KUNIT_EXPECT_TRUE(test, list_empty(&head));
+ KUNIT_EXPECT_TRUE(test, list_empty(&list));
+ KUNIT_EXPECT_TRUE(test, list_empty(&entry));
+}
+
static void list_test_list_splice(struct kunit *test)
{
struct list_head entries[5], *cur;
@@ -788,6 +805,7 @@ static struct kunit_case list_test_cases[] = {
KUNIT_CASE(list_test_list_is_singular),
KUNIT_CASE(list_test_list_cut_position),
KUNIT_CASE(list_test_list_cut_before),
+ KUNIT_CASE(list_test_list_cut_before_empty),
KUNIT_CASE(list_test_list_splice),
KUNIT_CASE(list_test_list_splice_tail),
KUNIT_CASE(list_test_list_splice_init),
--
2.51.0