public inbox for linux-kselftest@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kunit: Protect string comparisons against NULL
@ 2023-12-20 15:52 Richard Fitzgerald
  2023-12-22  8:39 ` David Gow
  2023-12-30  7:17 ` Muhammad Usama Anjum
  0 siblings, 2 replies; 4+ messages in thread
From: Richard Fitzgerald @ 2023-12-20 15:52 UTC (permalink / raw)
  To: brendan.higgins, davidgow, rmoar
  Cc: linux-kselftest, kunit-dev, linux-kernel, patches,
	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.

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
---
 include/kunit/test.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index b163b9984b33..c2ce379c329b 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;							       \
 									       \
 									       \
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] kunit: Protect string comparisons against NULL
  2023-12-20 15:52 [PATCH] kunit: Protect string comparisons against NULL Richard Fitzgerald
@ 2023-12-22  8:39 ` David Gow
  2024-01-04 11:52   ` Richard Fitzgerald
  2023-12-30  7:17 ` Muhammad Usama Anjum
  1 sibling, 1 reply; 4+ messages in thread
From: David Gow @ 2023-12-22  8:39 UTC (permalink / raw)
  To: Richard Fitzgerald
  Cc: brendan.higgins, rmoar, linux-kselftest, kunit-dev, linux-kernel,
	patches

[-- Attachment #1: Type: text/plain, Size: 790 bytes --]

On Wed, 20 Dec 2023 at 23:52, Richard Fitzgerald
<rf@opensource.cirrus.com> wrote:
>
> 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>
> ---

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.

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4003 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kunit: Protect string comparisons against NULL
  2023-12-20 15:52 [PATCH] kunit: Protect string comparisons against NULL Richard Fitzgerald
  2023-12-22  8:39 ` David Gow
@ 2023-12-30  7:17 ` Muhammad Usama Anjum
  1 sibling, 0 replies; 4+ messages in thread
From: Muhammad Usama Anjum @ 2023-12-30  7:17 UTC (permalink / raw)
  To: Richard Fitzgerald, brendan.higgins, davidgow, rmoar
  Cc: Muhammad Usama Anjum, linux-kselftest, kunit-dev, linux-kernel,
	patches

On 12/20/23 8:52 PM, Richard Fitzgerald wrote:
> 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: Muhammad Usama Anjum <usama.anjum@collabora.com>

> ---
>  include/kunit/test.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/kunit/test.h b/include/kunit/test.h
> index b163b9984b33..c2ce379c329b 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;							       \
>  									       \
>  									       \

-- 
BR,
Muhammad Usama Anjum

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kunit: Protect string comparisons against NULL
  2023-12-22  8:39 ` David Gow
@ 2024-01-04 11:52   ` Richard Fitzgerald
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Fitzgerald @ 2024-01-04 11:52 UTC (permalink / raw)
  To: David Gow
  Cc: brendan.higgins, rmoar, linux-kselftest, kunit-dev, linux-kernel,
	patches

On 22/12/23 08:39, David Gow wrote:
> On Wed, 20 Dec 2023 at 23:52, Richard Fitzgerald
> <rf@opensource.cirrus.com> wrote:
>>
>> 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>
>> ---
> 
> 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.

> Reviewed-by: David Gow <davidgow@google.com>
> 
> Cheers,
> -- David


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-04 11:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-20 15:52 [PATCH] kunit: Protect string comparisons against NULL Richard Fitzgerald
2023-12-22  8:39 ` David Gow
2024-01-04 11:52   ` Richard Fitzgerald
2023-12-30  7:17 ` Muhammad Usama Anjum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox