kunit: fix bug in KUNIT_EXPECT_MEMEQ
authorRae Moar <rmoar@google.com>
Fri, 27 Jan 2023 20:39:50 +0000 (20:39 +0000)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 30 Jan 2023 21:46:46 +0000 (14:46 -0700)
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>
include/kunit/test.h
lib/kunit/assert.c

index 87ea90576b50749ddac8d7d17e6a2848b6bbad9e..a20bff149bdf3488d11cac1c3dea7111460c05ba 100644 (file)
@@ -683,8 +683,9 @@ 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,                                             \
index f5b50babe38d46586bd9057c4386c8f02ec0bb23..05a09652f5a105f401874366ce74476eebc0e0e0 100644 (file)
@@ -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);