Skip to contentSkip to conversation

lib: kunit_iov_iter: fix NULL deref in iov_kunit_create_xarray()

PATCH New Jackie Liu 1 participant 1 message Sep 18, 2025

lore
Share & tools
lore

Jackie Liu

author
#0 lore raw

PATCH 1/1 lib: kunit_iov_iter: fix NULL deref in iov_kunit_create_xarray()

From: Jackie Liu <[email protected]>

iov_kunit_create_xarray() allocates memory for an xarray using kzalloc() and then calls xa_init() on the result. However, xa_init() was invoked before checking the return value of kzalloc(). If kzalloc() fails, this would lead to a NULL pointer dereference.

Move the KUNIT_ASSERT_NOT_ERR_OR_NULL() check before xa_init() to ensure the pointer is valid before initialization.

Fixes 2d71340ff1d4 ("iov_iter: Kunit tests for copying to/from an iterator")
---
 lib/tests/kunit_iov_iter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tests/kunit_iov_iter.c +1 -1
index 48342736d016..a9d6db89247d 100644
@@ -566,8 +566,8 @@ static struct xarray *iov_kunit_create_xarray(struct kunit *test)
 	struct xarray *xarray;
 
 	xarray = kzalloc(sizeof(struct xarray), GFP_KERNEL);
-	xa_init(xarray);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xarray);
+	xa_init(xarray);
 	kunit_add_action_or_reset(test, iov_kunit_destroy_xarray, xarray);
 	return xarray;
 }
Signature · 1 line
2.51.0