Skip to contentSkip to conversation

list: test: potential dereference of null pointer

PATCH In review Ma Ke 2 participants 2 messages Sep 23, 2023 – Sep 27, 2023

Subsystems (2)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH v3 1/1 list: test: potential dereference of null pointer

To avoid the failure of alloc, we could check the return value of kmalloc() and kzalloc().

---
 lib/list-test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/list-test.c +3 -2
index 0cc27de9cec8..a0b478042477 100644
@@ -26,10 +26,12 @@ static void list_test_list_init(struct kunit *test)
 
 	INIT_LIST_HEAD(&list2);
 
-	list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
+	list4 = kunit_kzalloc(test, sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
+	KUNIT_ASSERT_NOT_NULL(test, list4);
 	INIT_LIST_HEAD(list4);
 
 	list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
+	KUNIT_ASSERT_NOT_NULL(test, list5);
 	memset(list5, 0xFF, sizeof(*list5));
 	INIT_LIST_HEAD(list5);
 
@@ -40,7 +42,6 @@ static void list_test_list_init(struct kunit *test)
 	KUNIT_EXPECT_TRUE(test, list_empty_careful(list4));
 	KUNIT_EXPECT_TRUE(test, list_empty_careful(list5));
 
-	kfree(list4);
 	kfree(list5);
 }
 
Signature · 1 line
2.37.2
Reviewed-by on v3 1/1
#1 lore raw
Quoted 6 lines · Ma Ke

To avoid the failure of alloc, we could check the return value of kmalloc() and kzalloc().

---

Thanks very much. This looks good to me. We could change the rest of the allocations to also use kunit_kzalloc(), but equally that can be left to another patch.

Cheers,
-- David

Quoted 32 lines
 lib/list-test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/list-test.c +3 -3
index 0cc27de9cec8..a0b478042477 100644
@@ -26,10 +26,12 @@ static void list_test_list_init(struct kunit *test)
 
        INIT_LIST_HEAD(&list2);
 
-       list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
+       list4 = kunit_kzalloc(test, sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
+       KUNIT_ASSERT_NOT_NULL(test, list4);
        INIT_LIST_HEAD(list4);
 
        list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
+       KUNIT_ASSERT_NOT_NULL(test, list5);
        memset(list5, 0xFF, sizeof(*list5));
        INIT_LIST_HEAD(list5);
 
@@ -40,7 +42,6 @@ static void list_test_list_init(struct kunit *test)
        KUNIT_EXPECT_TRUE(test, list_empty_careful(list4));
        KUNIT_EXPECT_TRUE(test, list_empty_careful(list5));
 
-       kfree(list4);
        kfree(list5);
 }
 
--

2.37.2