From f987feab84fd3292ecadfb6af47ff072f086bc9d Mon Sep 17 00:00:00 2001 From: Cheyenne Wills Date: Mon, 5 Dec 2022 15:37:00 -0700 Subject: [PATCH] cmd: Assert that *alloc() returns success Several static analysis tools have identified various problems: - missing checks to ensure *alloc was successful (infer) To resolve the above problems: - Add asserts to verify that the *alloc was successful before using the memory. This commit is a reorganization of commits developed by Pat Riehecky, who ran the static analysis tools and developed the fixes. Note: Most of the callers to cmd_AddParmAlias and cmd_OptionAsList do not check the returned value, so asserts are used instead of returning ENOMEM. Change-Id: Ic44f6d34240c13c2626567f9d698d2480d2e8672 Reviewed-on: https://gerrit.openafs.org/14680 Tested-by: BuildBot Reviewed-by: Michael Meffie --- src/cmd/cmd.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/cmd/cmd.c b/src/cmd/cmd.c index 41ae78cdd3..fd4fc78803 100644 --- a/src/cmd/cmd.c +++ b/src/cmd/cmd.c @@ -620,7 +620,9 @@ cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias) return CMD_EXCESSPARMS; item = calloc(1, sizeof(struct cmd_item)); + assert(item != NULL); item->data = strdup(alias); + assert(item->data != NULL); item->next = as->parms[pos].aliases; as->parms[pos].aliases = item; @@ -1445,8 +1447,10 @@ cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value) start = str; while ((end = strchr(start, ' '))) { item = calloc(1, sizeof(struct cmd_item)); + assert(item != NULL); len = end - start + 1; item->data = malloc(len); + assert(item->data != NULL); strlcpy(item->data, start, len); *last = item; last = &item->next; @@ -1456,8 +1460,10 @@ cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value) /* Catch the final element */ if (*start != '\0') { item = calloc(1, sizeof(struct cmd_item)); + assert(item != NULL); len = strlen(start) + 1; item->data = malloc(len); + assert(item->data != NULL); strlcpy(item->data, start, len); *last = item; }