From 297c479989efb6bd9d4011a43d6c0dc92596761b Mon Sep 17 00:00:00 2001 From: Pat Riehecky Date: Fri, 21 Sep 2018 10:05:24 -0500 Subject: [PATCH] cmd: bail if out of memory while printing syntax Bail with an error message to stderr if we are unable to format the command syntax due to a string allocation error. Found via scan-build. [mmeffie: updated commit] Change-Id: Ib3bc7f53c295d8dde6c07b9c4990cd1b3bcee58c Reviewed-on: https://gerrit.openafs.org/13335 Tested-by: BuildBot Reviewed-by: Benjamin Kaduk --- src/cmd/cmd.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/cmd/cmd.c b/src/cmd/cmd.c index ffe49aa7..10b2b1b 100644 --- a/src/cmd/cmd.c +++ b/src/cmd/cmd.c @@ -176,7 +176,7 @@ ParmHelpString(struct cmd_parmdesc *aparm) aparm->help?aparm->help:"arg", aparm->type == CMD_LIST?"+":"", aparm->type == CMD_SINGLE_OR_FLAG?"]":"") < 0) - return "<< OUT OF MEMORY >>"; + return NULL; return str; } } @@ -220,6 +220,10 @@ PrintSyntax(struct cmd_syndesc *as) /* The parameter name is the real name, plus any aliases */ if (!tp->aliases) { name = strdup(tp->name); + if (!name) { + fprintf(stderr, "Out of memory.\n"); + return; + } } else { size_t namelen; struct cmd_item *alias; @@ -228,6 +232,10 @@ PrintSyntax(struct cmd_syndesc *as) namelen+=strlen(alias->data) + 3; name = malloc(namelen); + if (!name) { + fprintf(stderr, "Out of memory.\n"); + return; + } strlcpy(name, tp->name, namelen); for (alias = tp->aliases; alias != NULL; alias = alias->next) { @@ -239,6 +247,11 @@ PrintSyntax(struct cmd_syndesc *as) /* Work out if we can fit what we want to on this line, or if we need to * start a new one */ str = ParmHelpString(tp); + if (!str) { + fprintf(stderr, "Out of memory.\n"); + free(name); + return; + } xtralen = 1 + strlen(name) + strlen(str) + ((tp->flags & CMD_OPTIONAL)? 2: 0); -- 1.9.4