[PATCH] tools: Fix missing check for return value of malloc()

From: Chenyuan Mi
Date: Wed Jun 14 2023 - 10:45:04 EST


The malloc() function may return NULL when it fails,
which may cause null pointer deference in kmalloc(),
add Null check for return value of malloc().

Found by our static analysis tool.

Signed-off-by: Chenyuan Mi <cymi20@xxxxxxxxxxxx>
---
tools/lib/slab.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/tools/lib/slab.c b/tools/lib/slab.c
index 959997fb0652..cee98eb0a6d6 100644
--- a/tools/lib/slab.c
+++ b/tools/lib/slab.c
@@ -19,6 +19,8 @@ void *kmalloc(size_t size, gfp_t gfp)
return NULL;

ret = malloc(size);
+ if (!ret)
+ return NULL;
uatomic_inc(&kmalloc_nr_allocated);
if (kmalloc_verbose)
printf("Allocating %p from malloc\n", ret);
--
2.17.1