From ffe60c0f58ae4b385ad733fad4455efaebdd1342 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Tue, 6 Dec 2022 18:45:03 -0800 Subject: Make malloc error checking/reporting more consistent Signed-off-by: Alan Coopersmith --- include.c | 15 +++++++++++++-- main.c | 19 ++++++++++++++++--- parse.c | 4 ++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include.c b/include.c index b3a17ab..f07fb70 100644 --- a/include.c +++ b/include.c @@ -54,7 +54,10 @@ issymbolic(const char *dir, const char *component) if (strcmp(*pp, buf) == 0) return (TRUE); if (lstat(buf, &st) == 0 && (st.st_mode & S_IFMT) == S_IFLNK) { - *pp++ = strdup(buf); + char *p = strdup(buf); + if (p == NULL) + fatalerr("strdup() failure in %s()\n", __func__); + *pp++ = p; if (pp >= ¬dotdot[MAXDIRS]) fatalerr("out of .. dirs, increase MAXDIRS\n"); return (TRUE); @@ -151,11 +154,16 @@ newinclude(const char *newfile, const char *incstring) if (inclistp == inclist + MAXFILES - 1) fatalerr("out of space: increase MAXFILES\n"); ip->i_file = strdup(newfile); + if (ip->i_file == NULL) + fatalerr("strdup() failure in %s()\n", __func__); if (incstring == NULL) ip->i_incstring = ip->i_file; - else + else { ip->i_incstring = strdup(incstring); + if (ip->i_incstring == NULL) + fatalerr("strdup() failure in %s()\n", __func__); + } inclistnext = inclistp; return (ip); @@ -205,6 +213,9 @@ included_by(struct inclist *ip, struct inclist *newfile) ip->i_merged = reallocarray(ip->i_merged, ip->i_listlen, sizeof(boolean)); } + if ((ip->i_list == NULL) || (ip->i_merged == NULL)) + fatalerr("malloc()/realloc() failure in %s()\n", __func__); + ip->i_list[ip->i_listlen - 1] = newfile; ip->i_merged[ip->i_listlen - 1] = FALSE; } diff --git a/main.c b/main.c index 1c378e6..0fcd0c2 100644 --- a/main.c +++ b/main.c @@ -188,6 +188,8 @@ main(int argc, char *argv[]) fatalerr("cannot open \"%s\"\n", argv[1] + 1); fstat(afd, &ast); args = malloc(ast.st_size + 1); + if (args == NULL) + fatalerr("malloc()/realloc() failure in %s()\n", __func__); if ((ast.st_size = read(afd, args, ast.st_size)) < 0) fatalerr("failed to read %s\n", argv[1] + 1); args[ast.st_size] = '\0'; @@ -215,6 +217,8 @@ main(int argc, char *argv[]) if (p[-1]) nargc++; nargv = mallocarray(nargc, sizeof(char *)); + if (nargv == NULL) + fatalerr("malloc()/realloc() failure in %s()\n", __func__); nargv[0] = argv[0]; argc = 1; for (p = args; argc < nargc; p += strlen(p) + 1) @@ -277,6 +281,8 @@ main(int argc, char *argv[]) undeflist = malloc(sizeof(char *)); else undeflist = reallocarray(undeflist, numundefs, sizeof(char *)); + if (undeflist == NULL) + fatalerr("malloc()/realloc() failure in %s()\n", __func__); if (argv[0][2] == '\0') { if (argc < 2) fatalerr("Missing argument for -U\n"); @@ -382,7 +388,7 @@ main(int argc, char *argv[]) char *buf; if (argc < 2) - fatalerr("option -include is a " "missing its parameter\n"); + fatalerr("option -include is missing its parameter\n"); if (cmdinc_count >= MAXINCFILES) fatalerr("Too many -include flags.\n"); argc--; @@ -390,7 +396,7 @@ main(int argc, char *argv[]) buf = malloc(strlen(DASH_INC_PRE) + strlen(argv[0]) + strlen(DASH_INC_POST) + 1); if (!buf) - fatalerr("out of memory at " "-include string\n"); + fatalerr("out of memory at -include string\n"); cmdinc_list[2 * cmdinc_count + 0] = argv[0]; cmdinc_list[2 * cmdinc_count + 1] = buf; cmdinc_count++; @@ -532,17 +538,21 @@ getfile(const char *file) struct stat st; content = malloc(sizeof(struct filepointer)); + if (content == NULL) + fatalerr("malloc()/realloc() failure in %s()\n", __func__); content->f_name = file; if ((fd = open(file, O_RDONLY)) < 0) { warning("cannot open \"%s\"\n", file); content->f_p = content->f_base = content->f_end = malloc(1); + if (content->f_p == NULL) + fatalerr("malloc()/realloc() failure in %s()\n", __func__); *content->f_p = '\0'; return (content); } fstat(fd, &st); content->f_base = malloc(st.st_size + 1); if (content->f_base == NULL) - fatalerr("cannot allocate mem\n"); + fatalerr("malloc()/realloc() failure in %s()\n", __func__); if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0) fatalerr("failed to read %s\n", file); close(fd); @@ -715,6 +725,9 @@ base_name(const char *in_file) char *p; char *file = strdup(in_file); + if (file == NULL) + fatalerr("strdup() failure in %s()\n", __func__); + for (p = file + strlen(file); p > file && *p != '.'; p--); if (*p == '.') diff --git a/parse.c b/parse.c index d1b67b6..5915a81 100644 --- a/parse.c +++ b/parse.c @@ -367,6 +367,8 @@ define2(const char *name, const char *val, struct inclist *file) name, (*sp)->s_value, val, file->i_file)); free((*sp)->s_value); (*sp)->s_value = strdup(val); + if ((*sp)->s_value == NULL) + fatalerr("strdup() failure in %s()\n", __func__); return; } @@ -383,6 +385,8 @@ define2(const char *name, const char *val, struct inclist *file) debug(1, ("defining %s to %s in file %s\n", name, val, file->i_file)); stab->s_name = strdup(name); stab->s_value = strdup(val); + if ((stab->s_name == NULL) || (stab->s_value == NULL)) + fatalerr("strdup() failure in %s()\n", __func__); *sp = stab; } -- cgit v1.2.3