tools: lib: subcmd: Fix null pointer dereference
PATCH 1/1 tools: lib: subcmd: Fix null pointer dereference
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 <[email protected]>
---
Another option is to dereference only inside if (ent).
tools/lib/subcmd/help.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
tools/lib/subcmd/help.c +6 -1
@@ -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