summaryrefslogtreecommitdiff
path: root/lib/kunit
diff options
context:
space:
mode:
authorDaniel Latypov <dlatypov@google.com>2022-07-22 17:15:34 +0000
committerShuah Khan <skhan@linuxfoundation.org>2022-10-07 10:15:56 -0600
commit185d57797c5ea82e941befc2489dba0cf162b9c4 (patch)
treea410492b1fb6ccdfef80f39c916cb02fe697ac2c /lib/kunit
parente562e309d1d4ac05457c1454b6007071f13b5684 (diff)
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>
Diffstat (limited to 'lib/kunit')
-rw-r--r--lib/kunit/test.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 0e9ff5d8fe84..46471bda351e 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -722,6 +722,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);
}