diff mbox series

kunit: Fix potential null dereference in kunit_device_driver_test()

Message ID 20241115051745.14404-1-zichenxie0106@gmail.com
State Accepted
Commit 435c20eed572a95709b1536ff78832836b2f91b1
Headers show
Series kunit: Fix potential null dereference in kunit_device_driver_test() | expand

Commit Message

Gax-c Nov. 15, 2024, 5:17 a.m. UTC
From: Zichen Xie <zichenxie0106@gmail.com>

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")
Signed-off-by: Zichen Xie <zichenxie0106@gmail.com>
---
 lib/kunit/kunit-test.c | 2 ++
 1 file changed, 2 insertions(+)
diff mbox series

Patch

diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
index 37e02be1e710..d9c781c859fd 100644
--- a/lib/kunit/kunit-test.c
+++ b/lib/kunit/kunit-test.c
@@ -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");