Always perform == or != operation on cmp function result

Although not required to address bad code generation in
some versions of gcc 9 and 10, a coding style that requires
explicit comparison of the result to zero before use is
both clearer and would have avoided the generation of bad
code.

This change converts all use of cmp function usage from

```
    if (strcmp(a, b) || !strcmp(c, d)) ...
```

to

```
    if (strcmp(a, b) != 0 || strcmp(c, d)) == 0
```

for all C library cmp functions and related:

 - strcmp(), strncmp()
 - strcasecmp(), strncasecmp()
 - stricmp(), strnicmp()
 - memcmp()

Change-Id: Ic60c15e1e3a07e4faaf10648eefe3adae2543188
This commit is contained in:
Nicolas Williams
2021-11-14 23:52:50 -06:00
committed by Jeffrey Altman
parent 02200d55ea
commit 5f63215d0d
46 changed files with 119 additions and 116 deletions

View File

@@ -146,7 +146,7 @@ int teardown_test(void)
(len = strlen(dirname)) > sizeof(TESTDIR)/sizeof(char) &&
!strcmp(dirname + len + 1 - sizeof(TESTDIR)/sizeof(char), TESTDIR)) {
strcmp(dirname + len + 1 - sizeof(TESTDIR)/sizeof(char), TESTDIR) == 0) {
/* fallthrough */
@@ -215,12 +215,12 @@ int check_list(const char * filespec, const char ** list, int n, int expect_dot_
n_found ++;
if (expect_dot_and_dotdot &&
(!strcmp(e->d_name, ".") ||
!strcmp(e->d_name, "..")))
(strcmp(e->d_name, ".") == 0 ||
strcmp(e->d_name, "..") == 0))
continue;
for (i=0; i < n; i++) {
if (!strcmp(list[i], e->d_name))
if (strcmp(list[i], e->d_name) == 0)
break;
}

View File

@@ -759,7 +759,7 @@ rk_getifaddrs(struct ifaddrs **ifap)
}
if (ifamap.address_len != ifamap.local_len ||
(ifamap.address != NULL &&
memcmp(ifamap.address, ifamap.local, ifamap.address_len))) {
memcmp(ifamap.address, ifamap.local, ifamap.address_len) != 0)) {
/* p2p; address is peer and local is ours */
ifamap.broadcast = ifamap.address;
ifamap.broadcast_len = ifamap.address_len;

View File

@@ -74,7 +74,7 @@ setprogname(const char *argv0)
strlwr(fn);
ext = strrchr(fn, '.');
if (ext != NULL && !strcmp(ext, ".exe"))
if (ext != NULL && strcmp(ext, ".exe") == 0)
*ext = '\0';
__progname = fn;

View File

@@ -557,7 +557,7 @@ rk_snprintf (char *str, size_t sz, const char *format, ...)
va_start(args, format);
ret2 = vsprintf (tmp, format, args);
va_end(args);
if (ret != ret2 || strcmp(str, tmp))
if (ret != ret2 || strcmp(str, tmp) != 0)
abort ();
free (tmp);
}
@@ -589,7 +589,7 @@ rk_asprintf (char **ret, const char *format, ...)
va_start(args, format);
ret2 = vsprintf (tmp, format, args);
va_end(args);
if (val != ret2 || strcmp(*ret, tmp))
if (val != ret2 || strcmp(*ret, tmp) != 0)
abort ();
free (tmp);
}
@@ -618,7 +618,7 @@ rk_asnprintf (char **ret, size_t max_sz, const char *format, ...)
abort ();
ret2 = vsprintf (tmp, format, args);
if (val != ret2 || strcmp(*ret, tmp))
if (val != ret2 || strcmp(*ret, tmp) != 0)
abort ();
free (tmp);
}

View File

@@ -61,7 +61,7 @@ int main(int argc, char **argv)
pid_t parent = getpid();
pid_t child;
if (argc == 2 && strcmp(argv[1], "--reexec"))
if (argc == 2 && strcmp(argv[1], "--reexec") != 0)
errx(1, "Usage: test-detach [--reexec] [--daemon-child FD]");
if (argc == 3 || argc == 4) {
parent = getppid();

View File

@@ -195,7 +195,7 @@ test_simple_echo_socket(void)
getprogname(), srv, rv);
}
if (!strcmp(buf, "exit")) {
if (strcmp(buf, "exit") == 0) {
fprintf(stderr, "[%s] Exiting...\n", prog);
shutdown(s, SD_SEND);
rk_closesocket(s);
@@ -234,7 +234,7 @@ test_simple_echo(void)
while (gets(buf)) {
fprintf(stderr, "[%s] Received [%s]\n", prog, buf);
if (!strcmp(buf, "exit"))
if (strcmp(buf, "exit") == 0)
return 0;
/* simple echo */