[PATCH] initramfs_test: add NULL check for kmalloc in initramfs_test_fname_overrun

From: longlong yan

Date: Tue Jul 14 2026 - 22:08:56 EST


Add missing NULL check for kmalloc return value in
initramfs_test_fname_overrun(). Without this check, if kmalloc fails,
the subsequent memset() will dereference a NULL pointer, causing a
kernel crash.

Fixes: 83c0b27266ec ("initramfs_test: kunit tests for initramfs unpacking")
Signed-off-by: longlong yan <yanlonglong@xxxxxxxxxx>
---
init/initramfs_test.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/init/initramfs_test.c b/init/initramfs_test.c
index 1fc990a66563..c7aab7e5383e 100644
--- a/init/initramfs_test.c
+++ b/init/initramfs_test.c
@@ -189,6 +189,9 @@ static void __init initramfs_test_fname_overrun(struct kunit *test)
* are already available (e.g. no compression).
*/
cpio_srcbuf = kmalloc(CPIO_HDRLEN + PATH_MAX + 3, GFP_KERNEL);
+ if (!cpio_srcbuf)
+ return;
+
memset(cpio_srcbuf, 'B', CPIO_HDRLEN + PATH_MAX + 3);
/* limit overrun to avoid crashes / filp_open() ENAMETOOLONG */
cpio_srcbuf[CPIO_HDRLEN + strlen(c[0].fname) + 20] = '\0';
--
2.43.0