Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kunit: make kunit_kfree(NULL) a no-op to match kfree()

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>
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Daniel Latypov and committed by
Shuah Khan
185d5779 e562e309

+3
+3
lib/kunit/test.c
··· 722 722 723 723 void kunit_kfree(struct kunit *test, const void *ptr) 724 724 { 725 + if (!ptr) 726 + return; 727 + 725 728 if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr)) 726 729 KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr); 727 730 }