[PATCH v3] list: test: potential dereference of null pointer

Ma Ke posted 1 patch 2 years, 4 months ago
lib/list-test.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
[PATCH v3] list: test: potential dereference of null pointer
Posted by Ma Ke 2 years, 4 months ago
To avoid the failure of alloc, we could check the return value of
kmalloc() and kzalloc().

Signed-off-by: Ma Ke <make_ruc2021@163.com>
---
 lib/list-test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/list-test.c b/lib/list-test.c
index 0cc27de9cec8..a0b478042477 100644
--- a/lib/list-test.c
+++ b/lib/list-test.c
@@ -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
Re: [PATCH v3] list: test: potential dereference of null pointer
Posted by David Gow 2 years, 4 months ago
On Sat, 23 Sept 2023 at 08:54, Ma Ke <make_ruc2021@163.com> wrote:
>
> To avoid the failure of alloc, we could check the return value of
> kmalloc() and kzalloc().
>
> Signed-off-by: Ma Ke <make_ruc2021@163.com>
> ---

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.

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David

>  lib/list-test.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/lib/list-test.c b/lib/list-test.c
> index 0cc27de9cec8..a0b478042477 100644
> --- a/lib/list-test.c
> +++ b/lib/list-test.c
> @@ -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
>