2023-06-14 15:27:33

by Chenyuan Mi

[permalink] [raw]
Subject: [PATCH] perf subcmd: Fix missing check for return value of malloc() in add_cmdname()

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

Found by our static analysis tool.

Signed-off-by: Chenyuan Mi <[email protected]>
---
tools/lib/subcmd/help.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/tools/lib/subcmd/help.c b/tools/lib/subcmd/help.c
index bf02d62a3b2b..510a3eccb60f 100644
--- a/tools/lib/subcmd/help.c
+++ b/tools/lib/subcmd/help.c
@@ -16,6 +16,8 @@
void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
{
struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
+ if (!ent)
+ return;

ent->len = len;
memcpy(ent->name, name, len);
--
2.17.1



2023-06-14 16:36:14

by Ian Rogers

[permalink] [raw]
Subject: Re: [PATCH] perf subcmd: Fix missing check for return value of malloc() in add_cmdname()

On Wed, Jun 14, 2023 at 8:01 AM Chenyuan Mi <[email protected]> wrote:
>
> The malloc() function may return NULL when it fails,
> which may cause null pointer deference in add_cmdname(),
> add Null check for return value of malloc().
>
> Found by our static analysis tool.
>
> Signed-off-by: Chenyuan Mi <[email protected]>

Acked-by: Ian Rogers <[email protected]>

Thanks,
Ian

> ---
> tools/lib/subcmd/help.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/tools/lib/subcmd/help.c b/tools/lib/subcmd/help.c
> index bf02d62a3b2b..510a3eccb60f 100644
> --- a/tools/lib/subcmd/help.c
> +++ b/tools/lib/subcmd/help.c
> @@ -16,6 +16,8 @@
> void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
> {
> struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
> + if (!ent)
> + return;
>
> ent->len = len;
> memcpy(ent->name, name, len);
> --
> 2.17.1
>

2023-06-21 17:35:26

by Namhyung Kim

[permalink] [raw]
Subject: Re: [PATCH] perf subcmd: Fix missing check for return value of malloc() in add_cmdname()

On Wed, Jun 14, 2023 at 9:21 AM Ian Rogers <[email protected]> wrote:
>
> On Wed, Jun 14, 2023 at 8:01 AM Chenyuan Mi <[email protected]> wrote:
> >
> > The malloc() function may return NULL when it fails,
> > which may cause null pointer deference in add_cmdname(),
> > add Null check for return value of malloc().
> >
> > Found by our static analysis tool.
> >
> > Signed-off-by: Chenyuan Mi <[email protected]>
>
> Acked-by: Ian Rogers <[email protected]>

Applied to perf-tools-next, thanks!