Skip to content

Commit

Permalink
Fixed code where tests for NULL or '\0' used/assumed 0;
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-stumpf authored and Snaipe committed Apr 30, 2021
1 parent 04ce698 commit 7b368c1
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 44 deletions.
28 changes: 14 additions & 14 deletions enter.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static inline size_t append_argv(char **argv, size_t argc, char *arg)
static void apply_limit(int resource, struct rlimit const *value)
{
struct rlimit new_limit;
if (!value) {
if (value == NULL) {
if (getrlimit(resource, &new_limit)) {
if (errno == EINVAL) {
/* Skip this limit -- it is not supported. */
Expand Down Expand Up @@ -117,7 +117,7 @@ int enter(struct entry_settings *opts)
}
deny_new_capabilities = 1;

const char *root = opts->root ? opts->root : "/";
const char *root = (opts->root != NULL) ? opts->root : "/";

char resolved_root[PATH_MAX];
if (realpath(root, resolved_root) == NULL) {
Expand All @@ -128,7 +128,7 @@ int enter(struct entry_settings *opts)

char cwd[PATH_MAX];
char *workdir = opts->workdir;
if ((!workdir || workdir[0] == '\0')) {
if ((workdir == NULL || workdir[0] == '\0')) {
if (getcwd(cwd, sizeof (cwd)) == NULL) {
err(1, "getcwd");
}
Expand All @@ -149,7 +149,7 @@ int enter(struct entry_settings *opts)
}
/* Our tentative to use the cwd failed, or it worked and the cwd _is_ the
new root. In both cases, the workdir must be /. */
if (!workdir || workdir[0] == '\0') {
if (workdir == NULL || workdir[0] == '\0') {
workdir = "/";
}

Expand Down Expand Up @@ -180,7 +180,7 @@ int enter(struct entry_settings *opts)
err(1, "could not make / private: mount");
}

if (opts->arch && opts->arch[0] != 0) {
if (opts->arch != NULL && opts->arch[0] != '\0') {
setarch(opts->arch);
}

Expand Down Expand Up @@ -217,7 +217,7 @@ int enter(struct entry_settings *opts)
}

if (pid) {
if (opts->pidfile) {
if (opts->pidfile != NULL) {
int pidfile = open(opts->pidfile, O_WRONLY | O_CREAT | O_CLOEXEC | O_NOCTTY , 0666);
if (pidfile == -1) {
err(1, "open %s", opts->pidfile);
Expand Down Expand Up @@ -340,7 +340,7 @@ int enter(struct entry_settings *opts)
}
}

if (opts->setup_program) {
if (opts->setup_program != NULL) {
pid_t pid = fork();
if (pid == -1) {
err(1, "setup: fork");
Expand All @@ -358,7 +358,7 @@ int enter(struct entry_settings *opts)
extern char **environ;

char *const *argv = default_argv;
if (opts->setup_argv) {
if (opts->setup_argv != NULL) {
argv = opts->setup_argv;
}

Expand Down Expand Up @@ -416,23 +416,23 @@ int enter(struct entry_settings *opts)
}

/* Set the host and domain names only when in an UTS namespace. */
if ((opts->hostname || opts->domainname) && !uts_unshare) {
if ((opts->hostname != NULL || opts->domainname != NULL ) && !uts_unshare) {
errx(1, "attempted to set host or domain names on the host UTS namespace.");
}

const char *hostname = opts->hostname;
if (!hostname && uts_unshare) {
if (hostname == NULL && uts_unshare) {
hostname = "localhost";
}
if (hostname && sethostname(hostname, strlen(hostname)) == -1) {
if (hostname != NULL && sethostname(hostname, strlen(hostname)) == -1) {
err(1, "sethostname");
}

const char *domainname = opts->domainname;
if (!domainname && uts_unshare) {
if (domainname == NULL && uts_unshare) {
domainname = "localdomain";
}
if (domainname && setdomainname(domainname, strlen(domainname)) == -1) {
if (domainname != NULL && setdomainname(domainname, strlen(domainname)) == -1) {
err(1, "setdomainname");
}

Expand Down Expand Up @@ -588,7 +588,7 @@ int enter(struct entry_settings *opts)
argc = append_argv(argv, argc, opts->argv[0]);
argc = append_argv(argv, argc, (char *) opts->pathname);
char *const *arg = opts->argv + 1;
for (; *arg; ++arg) {
for (; *arg != NULL; ++arg) {
argc = append_argv(argv, argc, *arg);
}
argv[argc] = NULL;
Expand Down
14 changes: 7 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static void process_share(const char **out, const char *optarg)
append_nsname = false;
}

for (; share; share = strtok(NULL, ",")) {
for (; share != NULL; share = strtok(NULL, ",")) {
process_nslist_entry(out, share, path, append_nsname);
}
}
Expand All @@ -136,7 +136,7 @@ static void process_persist(const char **out, const char *optarg)
/* Specifying a standalone path means that all namespaces should be persisted
into nsfs files relative to that directory path. */
char all_namespaces[] = ALL_NAMESPACES;
if (!path) {
if (path == NULL) {
path = nsnames;
nsnames = all_namespaces;
}
Expand All @@ -148,7 +148,7 @@ static void process_persist(const char **out, const char *optarg)
const char *share = strtok(nsnames, ",");
bool multiple = share + strlen(share) != nsnames + nsnames_len;

for (; share; share = strtok(NULL, ",")) {
for (; share != NULL; share = strtok(NULL, ",")) {
process_nslist_entry(out, share, path, multiple);
}
}
Expand All @@ -162,7 +162,7 @@ static void process_unshare(const char **out, char *nsnames)
nsnames = all_namespaces;
}

for (const char *ns = strtok(nsnames, ","); ns; ns = strtok(NULL, ",")) {
for (const char *ns = strtok(nsnames, ","); ns != NULL; ns = strtok(NULL, ",")) {
process_nslist_entry(out, ns, NULL, false);
}
}
Expand Down Expand Up @@ -370,7 +370,7 @@ int main(int argc, char *argv[], char *envp[])
break;

case OPTION_GROUPS:
for (char *grp = strtok(optarg, ","); grp; grp = strtok(NULL, ",")) {
for (char *grp = strtok(optarg, ","); grp != NULL; grp = strtok(NULL, ",")) {
if (opts.ngroups >= NGROUPS_MAX) {
errx(1, "can only be part of a maximum of %d groups", NGROUPS_MAX);
}
Expand Down Expand Up @@ -746,7 +746,7 @@ int main(int argc, char *argv[], char *envp[])
++argv0;
}

new_argv[0] = argv0 ? argv0 : argv[optind];
new_argv[0] = (argv0 != NULL) ? argv0 : argv[optind];
for (int i = 1; i < argc - optind; ++i) {
new_argv[i] = argv[optind + i];
}
Expand All @@ -756,7 +756,7 @@ int main(int argc, char *argv[], char *envp[])
opts.argv = new_argv;
opts.envp = newenv;

if (opts.workdir && opts.workdir[0] != '\0' && opts.workdir[0] != '/') {
if (opts.workdir != NULL && opts.workdir[0] != '\0' && opts.workdir[0] != '/') {
errx(1, "workdir must be an absolute path.");
}

Expand Down
14 changes: 7 additions & 7 deletions mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,38 +81,38 @@ static void update_mount_flags_and_options(unsigned long *mountflags, char *opts
{ "suid", MS_NOSUID },
};

if (!opts) {
if (opts == NULL) {
return;
}

char sentinel;
char *newopts = opts;
for (char *opt = opts, *delim = &sentinel; delim && *opt; opt = delim + 1) {
for (char *opt = opts, *delim = &sentinel; delim != NULL && *opt != '\0'; opt = delim + 1) {

*delim = ',';
delim = strchr(opt, ',');
if (delim) {
*delim = 0;
if (delim != NULL) {
*delim = '\0';
}

struct mntflag *found;

found = bsearch(opt, flags, lengthof(flags), sizeof (*flags), cmpflags);
if (found) {
if (found != NULL) {
*mountflags |= found->flag;
continue;
}

found = bsearch(opt, neg_flags, lengthof(neg_flags), sizeof (*neg_flags), cmpflags);
if (found) {
if (found != NULL) {
*mountflags &= ~found->flag;
continue;
}

if (newopts > opts) {
*(newopts++) = ',';
}
for (char *s = opt; *s; ++s, ++newopts) {
for (char *s = opt; *s != '\0'; ++s, ++newopts) {
*newopts = *s;
}
}
Expand Down
8 changes: 4 additions & 4 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ void cleanpath(char *path) {
char *out = path;
char *start = path;

while (*path) {
while (*path != '\0') {
if (*path == '/') {
// empty component
++path;
} else if (*path == '.' && (!*(path+1) || *(path+1) == '/')) {
} else if (*path == '.' && (*(path+1) == '\0' || *(path+1) == '/')) {
// . component
++path;
} else if (*path == '.' && *(path+1) == '.' && (!*(path+2) || *(path+2) == '/')) {
} else if (*path == '.' && *(path+1) == '.' && (*(path+2) == '\0' || *(path+2) == '/')) {
// .. component
path += 2;

Expand All @@ -44,7 +44,7 @@ void cleanpath(char *path) {
*out = '/';
++out;
}
for (; *path && *path != '/'; ++path, ++out) {
for (; *path != '\0' && *path != '/'; ++path, ++out) {
*out = *path;
}
}
Expand Down
9 changes: 4 additions & 5 deletions setarch.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ struct exec_domain {
void setarch(const char *arch)
{
static struct exec_domain domains[] = {
/* Placeholder for host execution domain */
{ "", 0 },
{ "", 0 }, /* Placeholder for host execution domain */
{ "linux32", PER_LINUX32 },
{ "linux64", PER_LINUX },
{ "x86_64", PER_LINUX },
{ "i386", PER_LINUX32 },
{ "i486", PER_LINUX32 },
{ "i586", PER_LINUX32 },
{ "i686", PER_LINUX32 },
{ "", 0 }
{ "", 0 } /* Entry to mark end of the list */
};

struct exec_domain *host_domain = &domains[0];
Expand All @@ -49,13 +48,13 @@ void setarch(const char *arch)
#endif

struct exec_domain *domain = domains;
for (; *domain->name; ++domain) {
for (; *domain->name != '\0'; ++domain) {
if (strncmp(domain->name, arch, MACHINE_SIZE) == 0) {
break;
}
}

if (!*domain->name) {
if (*domain->name == '\0') {
errx(1, "setarch: unknown arch %s.", arch);
}

Expand Down
2 changes: 1 addition & 1 deletion unpersist.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ int main(int argc, char *argv[])
static struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "no-unlink", no_argument, NULL, OPTION_NO_UNLINK },
{ 0, 0, 0, 0 }
{ NULL, 0, NULL, 0 }
};

static struct {
Expand Down
12 changes: 6 additions & 6 deletions userns.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void id_map_parse(id_map map, char *opt)
size_t i = 0;
char *saveptr;
char *rangestr = strtok_r(opt, ",", &saveptr);
for (; rangestr; rangestr = strtok_r(NULL, ",", &saveptr)) {
for (; rangestr != NULL; rangestr = strtok_r(NULL, ",", &saveptr)) {
uint32_t inner = id_parse(strtok(rangestr, ":"), "inner id range start");
uint32_t outer = id_parse(strtok(NULL, ":"), "outer id range start");
uint32_t length = id_parse(strtok(NULL, ""), "id range length");
Expand Down Expand Up @@ -106,7 +106,7 @@ void id_map_load_subids(id_map map, const char *subid_path, const struct id *id)
size_t range = id_map_append(map, 0, 0, id->id, 1);

FILE *subids = fopen(subid_path, "r");
if (!subids) {
if (subids == NULL) {
return;
}

Expand All @@ -121,7 +121,7 @@ void id_map_load_subids(id_map map, const char *subid_path, const struct id *id)
size assumptions tend to bite back, and pages are extremely cheap. */
char line[4096];

while (fgets(line, sizeof (line), subids)) {
while (fgets(line, sizeof (line), subids) != NULL) {
char entryname[ID_STR_MAX + 1];
entryname[ID_STR_MAX] = 0;

Expand Down Expand Up @@ -220,7 +220,7 @@ void id_map_load_procids(id_map map, const char *procid_path)
memset(map, 0, sizeof (id_map));

char line[4096];
while (fgets(line, sizeof (line), subids)) {
while (fgets(line, sizeof (line), subids) != NULL) {
uint32_t inner, outer, len;

int items = sscanf(line, "%" PRIu32 "%" PRIu32 "%" PRIu32 "\n", &inner, &outer, &len);
Expand Down Expand Up @@ -458,7 +458,7 @@ struct id id_load_user(uid_t uid)
struct id id;
struct passwd *passwd = getpwuid(uid);
id.id = (uint32_t) uid;
id.name = passwd ? passwd->pw_name : NULL;
id.name = (passwd != NULL) ? passwd->pw_name : NULL;
return id;
}

Expand All @@ -467,6 +467,6 @@ struct id id_load_group(gid_t gid)
struct id id;
struct group *group = getgrgid(gid);
id.id = (uint32_t) gid;
id.name = group ? group->gr_name : NULL;
id.name = (group != NULL) ? group->gr_name : NULL;
return id;
}

0 comments on commit 7b368c1

Please sign in to comment.