2023-09-22 17:17:54

by David Gow

[permalink] [raw]
Subject: Re: [PATCH] list: test: potential dereference of null pointer

On Thu, 21 Sept 2023 at 16:18, Ma Ke <[email protected]> wrote:
>
> To avoid the failure of alloc, we could check the return value of
> kmalloc() and kzalloc().
>
> Signed-off-by: Ma Ke <[email protected]>
> ---

Fair enough, though I'd want the test to fail in this case (or, at the
very least, be skipped).

Could we use KUNIT_ASSERT_NOT_NULL() here?

Furthermore, there are a few bugs in the patch, see below.

Cheers,
-- David

> lib/list-test.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/lib/list-test.c b/lib/list-test.c
> index 0cc27de9cec8..9f82cac3a822 100644
> --- a/lib/list-test.c
> +++ b/lib/list-test.c
> @@ -27,9 +27,14 @@ static void list_test_list_init(struct kunit *test)
> INIT_LIST_HEAD(&list2);
>
> list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
> + if (!list4)
> + return;

Instead, let's use:
KUNIT_ASSERT_NOT_NULL(test, list4)

> INIT_LIST_HEAD(list4);
>
> list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
> + if (!list5)

Shouldn't this be in {}s? We don't want to return unconditionally.

> + kfree(list5);

We shouldn't free a NULL pointer. Should this be kfree(list4)?

Either way, maybe we should swap the allocations out for
kunit_kzalloc(), which will automatically free everything on test
exit.

> + return;

Again, let's use KUNIT_ASSERT_NOT_NULL() here. Or at the very least,
call KUNIT_FAIL() to make sure we're noting the test has failed.

> memset(list5, 0xFF, sizeof(*list5));
> INIT_LIST_HEAD(list5);
>
> --
> 2.37.2
>


Attachments:
smime.p7s (3.91 kB)
S/MIME Cryptographic Signature