From c068fa47e9246dcaac1f76bd76f7ea406c550114 Mon Sep 17 00:00:00 2001 From: Kuan-Wei Chiu Date: Thu, 9 May 2024 01:11:20 +0800 Subject: [PATCH] vinput: Fix NULL pointer dereference caused by failed kzalloc allocation When kzalloc fails to allocate memory and returns NULL, it leads to a NULL pointer dereference error later on. Add a check for the return value of kzalloc. When kzalloc fails to allocate memory, it prints an error message and returns ERR_PTR(-ENOMEM). --- examples/vinput.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/vinput.c b/examples/vinput.c index d7d2d23..51a1396 100644 --- a/examples/vinput.c +++ b/examples/vinput.c @@ -177,6 +177,11 @@ static struct vinput *vinput_alloc_vdevice(void) int err; struct vinput *vinput = kzalloc(sizeof(struct vinput), GFP_KERNEL); + if (!vinput) { + pr_err("vinput: Cannot allocate vinput input device\n"); + return ERR_PTR(-ENOMEM); + } + try_module_get(THIS_MODULE); spin_lock_init(&vinput->lock);