[PATCH] kunit: test: Assert parsed_filters allocation in parse_filter_attr_test()

0 views
Skip to first unread message

Guangshuo Li

unread,
Sep 18, 2025, 9:42:56 AM (6 days ago) Sep 18
to Brendan Higgins, David Gow, Rae Moar, Shuah Khan, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org, Guangshuo Li
kunit_kcalloc() may fail. parse_filter_attr_test() uses the returned
pointer without checking it, and then writes to parsed_filters[j],
which can lead to a NULL pointer dereference under low-memory
conditions.

Use KUNIT_ASSERT_NOT_ERR_OR_NULL() to abort the test on allocation
failure, per KUnit guidance for unsafe-to-continue cases.

Fixes: 1c9fd080dffe ("kunit: fix uninitialized variables bug in attributes filtering")
Fixes: 76066f93f1df ("kunit: add tests for filtering attributes")
Signed-off-by: Guangshuo Li <lgs2019...@gmail.com>
---
lib/kunit/executor_test.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/lib/kunit/executor_test.c b/lib/kunit/executor_test.c
index f0090c2729cd..084636ad8a72 100644
--- a/lib/kunit/executor_test.c
+++ b/lib/kunit/executor_test.c
@@ -127,6 +127,10 @@ static void parse_filter_attr_test(struct kunit *test)

parsed_filters = kunit_kcalloc(test, filter_count, sizeof(*parsed_filters),
GFP_KERNEL);
+
+ /* Abort test if allocation failed. */
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parsed_filters);
+
for (j = 0; j < filter_count; j++) {
parsed_filters[j] = kunit_next_attr_filter(&filter, &err);
KUNIT_ASSERT_EQ_MSG(test, err, 0, "failed to parse filter from '%s'", filters);
--
2.43.0

Reply all
Reply to author
Forward
0 new messages