diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2022-12-06 18:45:03 -0800 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2023-01-01 14:50:25 -0800 |
commit | ffe60c0f58ae4b385ad733fad4455efaebdd1342 (patch) | |
tree | 74df96937360c26b8d2e148dcab23a861a2e6e52 | |
parent | 37371f1cdcf351e29bf542e6b2fc269b29a4fba2 (diff) |
Make malloc error checking/reporting more consistent
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r-- | include.c | 15 | ||||
-rw-r--r-- | main.c | 19 | ||||
-rw-r--r-- | parse.c | 4 |
3 files changed, 33 insertions, 5 deletions
@@ -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; } @@ -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 == '.') @@ -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; } |