Skip to content

Commit dd2f0a0

Browse files
rmr167shuahkh
authored andcommitted
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: b8a926b ("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>
1 parent db105c3 commit dd2f0a0

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

include/kunit/test.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,9 @@ do { \
683683
.right_text = #right, \
684684
}; \
685685
\
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; \
688689
\
689690
_KUNIT_FAILED(test, \
690691
assert_type, \

lib/kunit/assert.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,24 +241,34 @@ void kunit_mem_assert_format(const struct kunit_assert *assert,
241241
mem_assert = container_of(assert, struct kunit_mem_assert,
242242
assert);
243243

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);
249258

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);
254263

255-
string_stream_add(stream, "\n");
264+
string_stream_add(stream, "\n");
256265

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);
261270

262-
kunit_assert_print_msg(message, stream);
271+
kunit_assert_print_msg(message, stream);
272+
}
263273
}
264274
EXPORT_SYMBOL_GPL(kunit_mem_assert_format);

0 commit comments

Comments
 (0)