2023-06-14 14:53:07

by Chenyuan Mi

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

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 <[email protected]>
---
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