Commit dd2f0a0a authored by Rae Moar's avatar Rae Moar Committed by Shuah Khan
Browse files

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: b8a926be ("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: default avatarkernel test robot <lkp@intel.com>

Signed-off-by: default avatarRae Moar <rmoar@google.com>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent db105c37
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -683,6 +683,7 @@ do { \
		.right_text = #right,					       \
	};								       \
									       \
	if (likely(__left && __right))					       \
		if (likely(memcmp(__left, __right, __size) op 0))	       \
			break;						       \
									       \
+25 −15
Original line number Diff line number Diff line
@@ -241,6 +241,15 @@ void kunit_mem_assert_format(const struct kunit_assert *assert,
	mem_assert = container_of(assert, struct kunit_mem_assert,
				  assert);

	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,
@@ -261,4 +270,5 @@ void kunit_mem_assert_format(const struct kunit_assert *assert,

		kunit_assert_print_msg(message, stream);
	}
}
EXPORT_SYMBOL_GPL(kunit_mem_assert_format);