* [PATCH] tools: lib: subcmd: Fix null pointer dereference
@ 2018-09-29 17:03 Tapasweni Pathak
0 siblings, 0 replies; only message in thread
From: Tapasweni Pathak @ 2018-09-29 17:03 UTC (permalink / raw)
To: gregkh, kstewart, pombredanne, tglx, tapaswenipathak, linux-kernel
Cc: tapaswenipathak
Add null check before dereferencing ent. ent is pointer to memory
allocated using malloc and is dereferenced immediately without
null check.
Found using Facebook's Infer. Build tested it.
Signed-off-by: Tapasweni Pathak <tapaswenipathak@gmail.com>
---
Another option is to dereference only inside if (ent).
tools/lib/subcmd/help.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/tools/lib/subcmd/help.c b/tools/lib/subcmd/help.c
index 2859f10..b805d1d 100644
--- a/tools/lib/subcmd/help.c
+++ b/tools/lib/subcmd/help.c
@@ -16,13 +16,18 @@
void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
{
struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
-
+ if (!ent) {
+ printf("mem alloc failed\n");
+ goto error;
+ }
ent->len = len;
memcpy(ent->name, name, len);
ent->name[len] = 0;
ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
cmds->names[cmds->cnt++] = ent;
+ error:
+ if (ent) free(ent);
}
void clean_cmdnames(struct cmdnames *cmds)
--
2.7.4
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2018-09-29 17:03 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-29 17:03 [PATCH] tools: lib: subcmd: Fix null pointer dereference Tapasweni Pathak
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome