[PATCH v2] list: test: potential dereference of null pointer
From: Ma Ke
Date: Fri Sep 22 2023 - 07:40:21 EST
To avoid the failure of alloc, we could check the return value of
kmalloc() and kzalloc().
Signed-off-by: Ma Ke <make_ruc2021@xxxxxxx>
---
lib/list-test.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/lib/list-test.c b/lib/list-test.c
index 0cc27de9cec8..70e898976dbf 100644
--- a/lib/list-test.c
+++ b/lib/list-test.c
@@ -27,9 +27,18 @@ static void list_test_list_init(struct kunit *test)
INIT_LIST_HEAD(&list2);
list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
+ if (!list4) {
+ KUNIT_FAIL(test, "Initialising list4 failed.\n");
+ return;
+ }
INIT_LIST_HEAD(list4);
list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
+ if (!list5) {
+ kfree(list4);
+ KUNIT_FAIL(test, "Initialising list5 failed.\n");
+ return;
+ }
memset(list5, 0xFF, sizeof(*list5));
INIT_LIST_HEAD(list5);
--
2.37.2