projects/linux·commit 7ece381a

kunit: Protect string comparisons against NULL

7ece381a · Richard Fitzgerald <[email protected]> · 2023-12-20 15:52:56 +00:00

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 <[email protected]>
Reviewed-by: David Gow <[email protected]>
Reviewed-by: Muhammad Usama Anjum <[email protected]>
Signed-off-by: Shuah Khan <[email protected]>
Commit
7ece381aa72d430ee117958abb5bb23e21d72f1d
Parents
5fb1a8c6

Changes

1 files changed · +1 −1

FileStatusAddDelContent
include/kunit/test.hmodified+1−1View file

include/kunit/test.h

View file
diff --git a/include/kunit/test.h b/include/kunit/test.h
index b163b99..c2ce379 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -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; \
\
\