Skip to contentSkip to conversation

kunit: Fix potential null dereference in kunit_device_driver_test()

PATCH In review Gax-c 2 participants 2 messages Nov 15, 2024

Subsystems (3)
lore
Share & tools
lore

Gax-c

author
#0 lore raw

PATCH v2 1/1 kunit: Fix potential null dereference in kunit_device_driver_test()

From: Zichen Xie <[email protected]>

kunit_kzalloc() may return a NULL pointer, dereferencing it without NULL check may lead to NULL dereference.
Add a NULL check for test_state.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")

---
v2: Add Cc tag.

---
 lib/kunit/kunit-test.c | 2 ++
 1 file changed, 2 insertions(+)
lib/kunit/kunit-test.c +2 -0
index 37e02be1e710..d9c781c859fd 100644
@@ -805,6 +805,8 @@ static void kunit_device_driver_test(struct kunit *test)
 	struct device *test_device;
 	struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);
 
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_state);
+
 	test->priv = test_state;
 	test_driver = kunit_driver_create(test, "my_driver");
 
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Gax-c

From: Zichen Xie <[email protected]>

kunit_kzalloc() may return a NULL pointer, dereferencing it without NULL check may lead to NULL dereference.
Add a NULL check for test_state.

Quoted 6 lines
Fixes d03c720e03bd ("kunit: Add APIs for managing devices")

---
v2: Add Cc tag.
---

Nice catch. This looks good to me.

Cheers,
-- David

Quoted 19 lines
 lib/kunit/kunit-test.c | 2 ++
 1 file changed, 2 insertions(+)
lib/kunit/kunit-test.c +2 -1
index 37e02be1e710..d9c781c859fd 100644
@@ -805,6 +805,8 @@ static void kunit_device_driver_test(struct kunit *test)
        struct device *test_device;
        struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);
 
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_state);
+
        test->priv = test_state;
        test_driver = kunit_driver_create(test, "my_driver");
 
--

2.34.1