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

From: Chenyuan Mi
Date: Wed Jun 14 2023 - 10:53:05 EST


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

Found by our static analysis tool.

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

diff --git a/tools/lib/subcmd/exec-cmd.c b/tools/lib/subcmd/exec-cmd.c
index 5dbea456973e..2e45c0cd4b68 100644
--- a/tools/lib/subcmd/exec-cmd.c
+++ b/tools/lib/subcmd/exec-cmd.c
@@ -168,6 +168,8 @@ static const char **prepare_exec_cmd(const char **argv)
for (argc = 0; argv[argc]; argc++)
; /* just counting */
nargv = malloc(sizeof(*nargv) * (argc + 2));
+ if (!nargv)
+ return NULL;

nargv[0] = subcmd_config.exec_name;
for (argc = 0; argv[argc]; argc++)
--
2.17.1