* check strdup for malloc failure * remove obvious /* NOTREACHED */ * return instead of exit from main * err(1, NULL) instead of err(1, "malloc") * mark usage as __dead
Index: mount.c =================================================================== RCS file: /cvs/src/sbin/mount/mount.c,v retrieving revision 1.69 diff -u -p -r1.69 mount.c --- mount.c 24 Jan 2017 23:41:44 -0000 1.69 +++ mount.c 25 Jan 2017 00:26:48 -0000 @@ -164,7 +164,6 @@ main(int argc, char * const argv[]) case '?': default: usage(); - /* NOTREACHED */ } argc -= optind; argv += optind; @@ -212,7 +211,7 @@ main(int argc, char * const argv[]) continue; prmount(&mntbuf[i]); } - exit(rval); + return (rval); } break; case 1: @@ -235,9 +234,8 @@ main(int argc, char * const argv[]) "can't find fstab entry for %s.", *argv); } else { - fs = malloc(sizeof(*fs)); - if (fs == NULL) - err(1, "malloc"); + if ((fs = malloc(sizeof(*fs))) == NULL) + err(1, NULL); fs->fs_vfstype = mntbuf->f_fstypename; fs->fs_spec = mntbuf->f_mntfromname; } @@ -283,7 +281,6 @@ main(int argc, char * const argv[]) break; default: usage(); - /* NOTREACHED */ } /* @@ -299,7 +296,7 @@ main(int argc, char * const argv[]) (void)fclose(mountdfp); } - exit(rval); + return (rval); } int @@ -310,7 +307,8 @@ hasopt(const char *mntopts, const char * if (mntopts == NULL) return (0); - optbuf = strdup(mntopts); + if ((optbuf = strdup(mntopts)) == NULL) + err(1, NULL); found = 0; for (opt = optbuf; !found && opt != NULL; strsep(&opt, ",")) found = !strncmp(opt, option, strlen(option)); @@ -340,6 +338,8 @@ int mountfs(const char *vfstype, const char *spec, const char *name, const char *options, const char *mntopts, int skipmounted) { + char *cp; + /* List of directories containing mount_xxx subcommands. */ static const char *edirs[] = { _PATH_SBIN, @@ -372,7 +372,10 @@ mountfs(const char *vfstype, const char } /* options follows after mntopts, so they get priority over mntopts */ - optbuf = catopt(strdup(mntopts), options); + if ((cp = strdup(mntopts)) == NULL) + err(1, NULL); + optbuf = catopt(cp, options); + free(cp); if (strcmp(name, "/") == 0) { if (!hasopt(optbuf, "update")) @@ -401,7 +404,7 @@ mountfs(const char *vfstype, const char argvsize = 64; if((argv = reallocarray(NULL, argvsize, sizeof(char *))) == NULL) - err(1, "malloc"); + err(1, NULL); argc = 0; argv[argc++] = NULL; /* this should be a full path name */ mangle(optbuf, &argc, argv, argvsize - 4); @@ -440,7 +443,6 @@ mountfs(const char *vfstype, const char if (errno == ENOENT) warn("no mount helper program found for %s", vfstype); _exit(1); - /* NOTREACHED */ default: /* Parent. */ free(optbuf); free(argv); @@ -673,7 +675,7 @@ maketypelist(char *fslist) /* Build an array of that many types. */ if ((av = typelist = reallocarray(NULL, i + 1, sizeof(char *))) == NULL) - err(1, "malloc"); + err(1, NULL); av[0] = fslist; for (i = 1, nextcp = fslist; (nextcp = strchr(nextcp, ',')); i++) { *nextcp = '\0'; @@ -691,8 +693,10 @@ catopt(char *s0, const char *s1) if (s0 && *s0) { if (asprintf(&cp, "%s,%s", s0, s1) == -1) err(1, NULL); - } else - cp = strdup(s1); + } else { + if ((cp = strdup(s1)) == NULL) + err(1, NULL); + } free(s0); return cp; @@ -724,10 +728,9 @@ mangle(char *options, int *argcp, const *argcp = argc; } -void +__dead void usage(void) { - (void)fprintf(stderr, "usage: mount [-AadfNruvw] [-t type]\n" " mount [-dfrsuvw] special | node\n"