From: Tapasweni Pathak <tapaswenipathak@gmail.com>
To: gregkh@linuxfoundation.org, kstewart@linuxfoundation.org,
pombredanne@nexb.com, tglx@linutronix.de,
tapaswenipathak@gmail.com, linux-kernel@vger.kernel.org
Cc: tapaswenipathak@gmail.com
Subject: [PATCH] tools: lib: subcmd: Fix null pointer dereference
Date: Sat, 29 Sep 2018 22:33:25 +0530 [thread overview]
Message-ID: <5bafb060.1c69fb81.ebebd.f24c@mx.google.com> (raw)
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
reply other threads:[~2018-09-29 17:03 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5bafb060.1c69fb81.ebebd.f24c@mx.google.com \
--to=tapaswenipathak@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=kstewart@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pombredanne@nexb.com \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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