ext4: fix potential null dereference in ext4 kunit test
authorCharles Han <hanchunchao@inspur.com>
Fri, 10 Jan 2025 09:24:21 +0000 (17:24 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 13 Mar 2025 14:01:14 +0000 (10:01 -0400)
kunit_kzalloc() may return a NULL pointer, dereferencing it
without NULL check may lead to NULL dereference.
Add a NULL check for grp.

Fixes: ac96b56a2fbd ("ext4: Add unit test for mb_mark_used")
Fixes: b7098e1fa7bc ("ext4: Add unit test for mb_free_blocks")
Signed-off-by: Charles Han <hanchunchao@inspur.com>
Reviewed-by: Kemeng Shi <shikemeng@huaweicloud.com>
Link: https://patch.msgid.link/20250110092421.35619-1-hanchunchao@inspur.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/mballoc-test.c

index bb2a223b207c19e39b66485bc9a4cb47c82b21c6..d634c12f1984749ce7ea9332b77906799e4af21c 100644 (file)
@@ -796,6 +796,7 @@ static void test_mb_mark_used(struct kunit *test)
        KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buddy);
        grp = kunit_kzalloc(test, offsetof(struct ext4_group_info,
                                bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
 
        ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
        KUNIT_ASSERT_EQ(test, ret, 0);
@@ -860,6 +861,7 @@ static void test_mb_free_blocks(struct kunit *test)
        KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buddy);
        grp = kunit_kzalloc(test, offsetof(struct ext4_group_info,
                                bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
 
        ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
        KUNIT_ASSERT_EQ(test, ret, 0);