Skip to contentSkip to conversation

kunit: Protect string comparisons against NULL

PATCH In review Richard Fitzgerald 3 participants 4 messages Dec 20, 2023 – Jan 4, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kunit: Protect string comparisons against NULL

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.

---
 include/kunit/test.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/kunit/test.h +1 -1
index b163b9984b33..c2ce379c329b 100644
@@ -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;							       \
 									       \
 									       \
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Richard Fitzgerald

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().

Quoted 7 lines

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.

---

I think this is the right thing to do. There's possibly an argument that this should succeed if both are NULL, but I prefer it this way.

Cheers,
-- David

Reviewed-by on v1 1/1
#3 lore raw
In reply to Richard Fitzgerald
Quoted 10 lines · Richard Fitzgerald

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.

Quoted 17 lines
---
 include/kunit/test.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/kunit/test.h +1 -1
index b163b9984b33..c2ce379c329b 100644
@@ -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;							       \
 									       \
 									       \
Signature · 2 lines
BR,
Muhammad Usama Anjum
#2 lore raw
In reply to David Gow
Quoted 12 lines · David Gow
Quoted 10 lines · Richard Fitzgerald

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.

Quoted 6 lines
Quoted 2 lines

---

I think this is the right thing to do. There's possibly an argument that this should succeed if both are NULL, but I prefer it this way.

Maybe an _OR_NULL() variant of the string test macros would be better to be explicit that NULL is acceptable.

Quoted 4 lines

Cheers,
-- David