kunit: fix bug in KUNIT_EXPECT_MEMEQ
PATCH 1/1 kunit: fix bug in KUNIT_EXPECT_MEMEQ
In KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ, add check if one of the inputs is NULL and fail if this is the case.
Currently, the kernel crashes if one of the inputs is NULL. Instead, fail the test and add an appropriate error message.
This was found by the kernel test robot:
https://lore.kernel.org/all/[email protected]/
--- include/kunit/test.h | 7 ++++--- lib/kunit/assert.c | 40 +++++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 18 deletions(-)
include/kunit/test.h +4 -3
@@ -683,9 +683,10 @@ do { \ .right_text = #right, \ }; \ \ - if (likely(memcmp(__left, __right, __size) op 0)) \ - break; \ - \ + if (likely(__left && __right)) \ + if (likely(memcmp(__left, __right, __size) op 0)) \ + break; \ + \ _KUNIT_FAILED(test, \ assert_type, \ kunit_mem_assert, \
lib/kunit/assert.c +25 -15
@@ -241,24 +241,34 @@ void kunit_mem_assert_format(const struct kunit_assert *assert, mem_assert = container_of(assert, struct kunit_mem_assert, assert); -string_stream_add (stream, -KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n" , - mem_assert->text->left_text, - mem_assert->text->operation, - mem_assert->text->right_text); +if (!mem_assert->left_value) { +string_stream_add (stream, +KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n" , + mem_assert->text->left_text); + }else if (!mem_assert->right_value) { +string_stream_add (stream, +KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n" , + mem_assert->text->right_text); + }else { +string_stream_add (stream, +KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n" , + mem_assert->text->left_text, + mem_assert->text->operation, + mem_assert->text->right_text); -string_stream_add (stream,KUNIT_SUBSUBTEST_INDENT "%s ==\n" , - mem_assert->text->left_text); -kunit_assert_hexdump (stream, mem_assert->left_value, - mem_assert->right_value, mem_assert->size); +string_stream_add (stream,KUNIT_SUBSUBTEST_INDENT "%s ==\n" , + mem_assert->text->left_text); +kunit_assert_hexdump (stream, mem_assert->left_value, + mem_assert->right_value, mem_assert->size); -string_stream_add (stream,"\n" ); +string_stream_add (stream,"\n" ); -string_stream_add (stream,KUNIT_SUBSUBTEST_INDENT "%s ==\n" , - mem_assert->text->right_text); -kunit_assert_hexdump (stream, mem_assert->right_value, - mem_assert->left_value, mem_assert->size); +string_stream_add (stream,KUNIT_SUBSUBTEST_INDENT "%s ==\n" , + mem_assert->text->right_text); +kunit_assert_hexdump (stream, mem_assert->right_value, + mem_assert->left_value, mem_assert->size); -kunit_assert_print_msg (message, stream); +kunit_assert_print_msg (message, stream); + } }EXPORT_SYMBOL_GPL (kunit_mem_assert_format);
base-commit: 5cb26c298ffde271d9bd1dd1b87ad028218f77fe
Signature · 1 line
2.39.1.405.gd4c25cc71f-goog
Quoted 7 lines · Rae Moar
In
KUNIT_EXPECT_MEMEQandKUNIT_EXPECT_MEMNEQ, add check if one of the inputs is NULL and fail if this is the case.Currently, the kernel crashes if one of the inputs is NULL. Instead, fail the test and add an appropriate error message.
Quoted 7 lines
This was found by the kernel test robot:
https://lore.kernel.org/all/[email protected]/Reported-by kernel test robot <[email protected]>Signed-off-by Rae Moar <[email protected]>---
This looks good to me, modulo a small formatting issue with the continuation backslashes (see below).
It might be worth considering this as a fix to the patch which first introduced the MEMEQ macros with a Fixes tag, e.g.:
KUNIT_EXPECT_MEMNEQ macros")
Otherwise, this is:
Cheers,
-- David
Quoted 9 lines
include/kunit/test.h | 7 ++++--- lib/kunit/assert.c | 40 +++++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 18 deletions(-)include/kunit/test.h +0
-0
include/kunit/test.h do { \ · line 689
Quoted 10 lines
include/kunit/test.h +4
-3@@ -683,9 +683,10 @@ do { \ .right_text = #right, \ }; \ \ - if (likely(memcmp(__left, __right, __size) op 0)) \ - break; \ - \ + if (likely(__left && __right)) \ + if (likely(memcmp(__left, __right, __size) op 0)) \ + break; \ + \
The backslashes no longer line up here.
Quoted 62 lines
include/kunit/test.h +0
-0_KUNIT_FAILED(test, \ assert_type, \ kunit_mem_assert, \lib/kunit/assert.c +25
-15@@ -241,24 +241,34 @@ void kunit_mem_assert_format(const struct kunit_assert *assert, mem_assert = container_of(assert, struct kunit_mem_assert, assert); -string_stream_add (stream, -KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n" , - mem_assert->text->left_text, - mem_assert->text->operation, - mem_assert->text->right_text); +if (!mem_assert->left_value) { +string_stream_add (stream, +KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n" , + mem_assert->text->left_text); + }else if (!mem_assert->right_value) { +string_stream_add (stream, +KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n" , + mem_assert->text->right_text); + }else { +string_stream_add (stream, +KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n" , + mem_assert->text->left_text, + mem_assert->text->operation, + mem_assert->text->right_text); -string_stream_add (stream,KUNIT_SUBSUBTEST_INDENT "%s ==\n" , - mem_assert->text->left_text); -kunit_assert_hexdump (stream, mem_assert->left_value, - mem_assert->right_value, mem_assert->size); +string_stream_add (stream,KUNIT_SUBSUBTEST_INDENT "%s ==\n" , + mem_assert->text->left_text); +kunit_assert_hexdump (stream, mem_assert->left_value, + mem_assert->right_value, mem_assert->size); -string_stream_add (stream,"\n" ); +string_stream_add (stream,"\n" ); -string_stream_add (stream,KUNIT_SUBSUBTEST_INDENT "%s ==\n" , - mem_assert->text->right_text); -kunit_assert_hexdump (stream, mem_assert->right_value, - mem_assert->left_value, mem_assert->size); +string_stream_add (stream,KUNIT_SUBSUBTEST_INDENT "%s ==\n" , + mem_assert->text->right_text); +kunit_assert_hexdump (stream, mem_assert->right_value, + mem_assert->left_value, mem_assert->size); -kunit_assert_print_msg (message, stream); +kunit_assert_print_msg (message, stream); + } }EXPORT_SYMBOL_GPL (kunit_mem_assert_format);base-commit:
5cb26c298ffde271d9bd1dd1b87ad028218f77fe--
2.39.1.405.gd4c25cc71f-goog