Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.

Fixes: b8a926bea8b1 ("kunit: Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros")

This was found by the kernel test robot:
https://lore.kernel.org/all/202212191448.D6EDPdOh-lkp@intel.com/

Reported-by: kernel test robot <lkp@intel.com>

Signed-off-by: Rae Moar <rmoar@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Rae Moar and committed by
Shuah Khan
dd2f0a0a db105c37

+28 -17
+3 -2
include/kunit/test.h
··· 683 683 .right_text = #right, \ 684 684 }; \ 685 685 \ 686 - if (likely(memcmp(__left, __right, __size) op 0)) \ 687 - break; \ 686 + if (likely(__left && __right)) \ 687 + if (likely(memcmp(__left, __right, __size) op 0)) \ 688 + break; \ 688 689 \ 689 690 _KUNIT_FAILED(test, \ 690 691 assert_type, \
+25 -15
lib/kunit/assert.c
··· 241 241 mem_assert = container_of(assert, struct kunit_mem_assert, 242 242 assert); 243 243 244 - string_stream_add(stream, 245 - KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n", 246 - mem_assert->text->left_text, 247 - mem_assert->text->operation, 248 - mem_assert->text->right_text); 244 + if (!mem_assert->left_value) { 245 + string_stream_add(stream, 246 + KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n", 247 + mem_assert->text->left_text); 248 + } else if (!mem_assert->right_value) { 249 + string_stream_add(stream, 250 + KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n", 251 + mem_assert->text->right_text); 252 + } else { 253 + string_stream_add(stream, 254 + KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n", 255 + mem_assert->text->left_text, 256 + mem_assert->text->operation, 257 + mem_assert->text->right_text); 249 258 250 - string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s ==\n", 251 - mem_assert->text->left_text); 252 - kunit_assert_hexdump(stream, mem_assert->left_value, 253 - mem_assert->right_value, mem_assert->size); 259 + string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s ==\n", 260 + mem_assert->text->left_text); 261 + kunit_assert_hexdump(stream, mem_assert->left_value, 262 + mem_assert->right_value, mem_assert->size); 254 263 255 - string_stream_add(stream, "\n"); 264 + string_stream_add(stream, "\n"); 256 265 257 - string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s ==\n", 258 - mem_assert->text->right_text); 259 - kunit_assert_hexdump(stream, mem_assert->right_value, 260 - mem_assert->left_value, mem_assert->size); 266 + string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s ==\n", 267 + mem_assert->text->right_text); 268 + kunit_assert_hexdump(stream, mem_assert->right_value, 269 + mem_assert->left_value, mem_assert->size); 261 270 262 - kunit_assert_print_msg(message, stream); 271 + kunit_assert_print_msg(message, stream); 272 + } 263 273 } 264 274 EXPORT_SYMBOL_GPL(kunit_mem_assert_format);