]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
kunit: Protect string comparisons against NULL
authorRichard Fitzgerald <rf@opensource.cirrus.com>
Wed, 20 Dec 2023 15:52:56 +0000 (15:52 +0000)
committerShuah Khan <skhan@linuxfoundation.org>
Wed, 3 Jan 2024 16:10:04 +0000 (09:10 -0700)
Add NULL checks to KUNIT_BINARY_STR_ASSERTION() so that it will fail
cleanly if either pointer is NULL, instead of causing a NULL pointer
dereference in the strcmp().

A test failure could be that a string is unexpectedly NULL. This could
be trapped by KUNIT_ASSERT_NOT_NULL() but that would terminate the test
at that point. It's preferable that the KUNIT_EXPECT_STR*() macros can
handle NULL pointers as a failure.

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
include/kunit/test.h

index b163b9984b334f77f6bedeaac1fad840f819b60c..c2ce379c329b998079bb70d5dad1ebe46f465e19 100644 (file)
@@ -758,7 +758,7 @@ do {                                                                               \
                .right_text = #right,                                          \
        };                                                                     \
                                                                               \
-       if (likely(strcmp(__left, __right) op 0))                              \
+       if (likely((__left) && (__right) && (strcmp(__left, __right) op 0)))   \
                break;                                                         \
                                                                               \
                                                                               \