* [PATCH] kunit: Protect KUNIT_BINARY_STR_ASSERTION against ERR_PTR values
@ 2025-12-19 16:12 Richard Fitzgerald
2025-12-20 6:59 ` David Gow
0 siblings, 1 reply; 2+ messages in thread
From: Richard Fitzgerald @ 2025-12-19 16:12 UTC (permalink / raw)
To: brendan.higgins, davidgow, raemoar63
Cc: linux-kselftest, kunit-dev, linux-kernel, patches
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_.
Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
---
include/kunit/test.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/include/kunit/test.h b/include/kunit/test.h
index 5ec5182b5e57..9cd1594ab697 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] kunit: Protect KUNIT_BINARY_STR_ASSERTION against ERR_PTR values
2025-12-19 16:12 [PATCH] kunit: Protect KUNIT_BINARY_STR_ASSERTION against ERR_PTR values Richard Fitzgerald
@ 2025-12-20 6:59 ` David Gow
0 siblings, 0 replies; 2+ messages in thread
From: David Gow @ 2025-12-20 6:59 UTC (permalink / raw)
To: Richard Fitzgerald
Cc: brendan.higgins, raemoar63, linux-kselftest, kunit-dev,
linux-kernel, patches
[-- Attachment #1: Type: text/plain, Size: 2213 bytes --]
On Sat, 20 Dec 2025 at 00:12, Richard Fitzgerald
<rf@opensource.cirrus.com> wrote:
>
> 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_.
>
> Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
> ---
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.
Reviewed-by: David Gow <davidgow@google.com>
Cheers,
-- David
[1]: https://staticthinking.wordpress.com/2022/08/01/mixing-error-pointers-and-null/
> include/kunit/test.h | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/include/kunit/test.h b/include/kunit/test.h
> index 5ec5182b5e57..9cd1594ab697 100644
> --- a/include/kunit/test.h
> +++ b/include/kunit/test.h
> @@ -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
>
[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 5281 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-12-20 6:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-19 16:12 [PATCH] kunit: Protect KUNIT_BINARY_STR_ASSERTION against ERR_PTR values Richard Fitzgerald
2025-12-20 6:59 ` David Gow
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®