Skip to contentSkip to conversation

kunit: Protect KUNIT_BINARY_STR_ASSERTION against ERR_PTR values

PATCH In review Richard Fitzgerald 2 participants 2 messages Dec 19, 2025 – Dec 20, 2025

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

PATCH 1/1 kunit: Protect KUNIT_BINARY_STR_ASSERTION against ERR_PTR values

Replace the NULL checks with IS_ERR_OR_NULL() in KUNIT_BINARY_STR_ASSERTION() to prevent the strcmp() faulting if a passed pointer is an ERR_PTR.

Commit 7ece381aa72d4 ("kunit: Protect string comparisons against NULL") added the checks for NULL on both pointers so that asserts would fail, instead of faulting, if either pointer is NULL. But either pointer could hold an ERR_PTR value.

This assumes that the assertion is expecting both strings to be valid, and is asserting the equality of their _content_.

---
 include/kunit/test.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/kunit/test.h +2 -1
index 5ec5182b5e57..9cd1594ab697 100644
@@ -906,7 +906,8 @@ do {									       \
 	};								       \
 									       \
 	_KUNIT_SAVE_LOC(test);						       \
-	if (likely((__left) && (__right) && (strcmp(__left, __right) op 0)))   \
+	if (likely(!IS_ERR_OR_NULL(__left) && !IS_ERR_OR_NULL(__right) &&      \
+	    (strcmp(__left, __right) op 0)))				       \
 		break;							       \
 									       \
 									       \
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Richard Fitzgerald

Replace the NULL checks with IS_ERR_OR_NULL() in KUNIT_BINARY_STR_ASSERTION() to prevent the strcmp() faulting if a passed pointer is an ERR_PTR.

Commit 7ece381aa72d4 ("kunit: Protect string comparisons against NULL") added the checks for NULL on both pointers so that asserts would fail, instead of faulting, if either pointer is NULL. But either pointer could hold an ERR_PTR value.

Quoted 5 lines

This assumes that the assertion is expecting both strings to be valid, and is asserting the equality of their _content_.

---

This looks good to me, though I do acknowledge the general hate for IS_ERR_OR_NULL()[1], and particularly how it can annoy things like smatch. For an assertion, though, where we're really asserting that both are valid strings which are equal, I think this is okay.

Cheers,
-- David

[1]: https://staticthinking.wordpress.com/2022/08/01/mixing-error-pointers-and-null/

Quoted 20 lines
 include/kunit/test.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/kunit/test.h +2 -2
index 5ec5182b5e57..9cd1594ab697 100644
@@ -906,7 +906,8 @@ do {                                                                               \
        };                                                                     \
                                                                               \
        _KUNIT_SAVE_LOC(test);                                                 \
-       if (likely((__left) && (__right) && (strcmp(__left, __right) op 0)))   \
+       if (likely(!IS_ERR_OR_NULL(__left) && !IS_ERR_OR_NULL(__right) &&      \
+           (strcmp(__left, __right) op 0)))                                   \
                break;                                                         \
                                                                               \
                                                                               \
--

2.47.3