linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Gow <davidgow@google.com>
To: Daniel Latypov <dlatypov@google.com>
Cc: Brendan Higgins <brendanhiggins@google.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	KUnit Development <kunit-dev@googlegroups.com>,
	"open list:KERNEL SELFTEST FRAMEWORK" 
	<linux-kselftest@vger.kernel.org>,
	Shuah Khan <skhan@linuxfoundation.org>
Subject: Re: [PATCH v2 5/5] kunit: make kunit_kfree(NULL) a no-op to match kfree()
Date: Sat, 23 Jul 2022 14:33:23 +0800	[thread overview]
Message-ID: <CABVgOS=pbEhUn3Hm7hrtQSQcqmj9a5T1MXNgomtbjsFc1RaphA@mail.gmail.com> (raw)
In-Reply-To: <20220722171534.3576419-5-dlatypov@google.com>

On Sat, Jul 23, 2022 at 1:15 AM Daniel Latypov <dlatypov@google.com> wrote:
>
> The real kfree() function will silently return when given a NULL.
> So a user might reasonably think they can write the following code:
>   char *buffer = NULL;
>   if (param->use_buffer) buffer = kunit_kzalloc(test, 10, GFP_KERNEL);
>   ...
>   kunit_kfree(test, buffer);
>
> As-is, kunit_kfree() will mark the test as FAILED when buffer is NULL.
> (And in earlier times, it would segfault).
>
> Let's match the semantics of kfree().
>
> Suggested-by: David Gow <davidgow@google.com>
> Signed-off-by: Daniel Latypov <dlatypov@google.com>
> ---
> v1 -> v2: add this patch to the series.
> ---

Thanks! This looks good to me, and worked with a basic test.

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

Cheers,
-- David

>  lib/kunit/test.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> index c7ca87484968..879c8db36cb5 100644
> --- a/lib/kunit/test.c
> +++ b/lib/kunit/test.c
> @@ -698,6 +698,9 @@ static inline bool kunit_kfree_match(struct kunit *test,
>
>  void kunit_kfree(struct kunit *test, const void *ptr)
>  {
> +       if (!ptr)
> +               return;
> +
>         if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr))
>                 KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr);
>  }
> --
> 2.37.1.359.gd136c6c3e2-goog
>

  reply	other threads:[~2022-07-23  6:33 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-22 17:15 [PATCH v2 1/5] kunit: string-stream: Simplify resource use Daniel Latypov
2022-07-22 17:15 ` [PATCH v2 2/5] kunit: drop test pointer in string_stream_fragment Daniel Latypov
2022-10-05 20:30   ` Brendan Higgins
2022-07-22 17:15 ` [PATCH v2 3/5] kunit: make kunit_kfree() only work on pointers from kunit_malloc() and friends Daniel Latypov
2022-10-05 20:36   ` Brendan Higgins
2022-07-22 17:15 ` [PATCH v2 4/5] kunit: make kunit_kfree() not segfault on invalid inputs Daniel Latypov
2022-10-05 20:39   ` Brendan Higgins
2022-07-22 17:15 ` [PATCH v2 5/5] kunit: make kunit_kfree(NULL) a no-op to match kfree() Daniel Latypov
2022-07-23  6:33   ` David Gow [this message]
2022-10-05 20:40   ` Brendan Higgins
2022-10-05 20:28 ` [PATCH v2 1/5] kunit: string-stream: Simplify resource use Brendan Higgins

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABVgOS=pbEhUn3Hm7hrtQSQcqmj9a5T1MXNgomtbjsFc1RaphA@mail.gmail.com' \
    --to=davidgow@google.com \
    --cc=brendanhiggins@google.com \
    --cc=dlatypov@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).