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

@@ -272,7 +272,7 @@ read_token(gss_buffer_t in, int negotiate)
tmp = inbuf;
if (negotiate) {
if (strncasecmp("Negotiate ", inbuf, 10)) {
if (strncasecmp("Negotiate ", inbuf, 10) != 0) {
fprintf(stderr, "Token doesn't begin with "
"\"Negotiate \"\n");
ret = -1;

View File

@@ -365,7 +365,7 @@ _gssapi_verify_mic_arcfour(OM_uint32 * minor_status,
return GSS_S_FAILURE;
}
cmp = ct_memcmp(cksum_data, p + 8, 8);
cmp = (ct_memcmp(cksum_data, p + 8, 8) != 0);
if (cmp) {
*minor_status = 0;
return GSS_S_BAD_MIC;
@@ -385,7 +385,7 @@ _gssapi_verify_mic_arcfour(OM_uint32 * minor_status,
_gsskrb5_decode_be_om_uint32(SND_SEQ, &seq_number);
if (context_handle->more_flags & LOCAL)
cmp = memcmp(&SND_SEQ[4], "\xff\xff\xff\xff", 4);
cmp = (memcmp(&SND_SEQ[4], "\xff\xff\xff\xff", 4) != 0);
else
cmp = (memcmp(&SND_SEQ[4], "\x00\x00\x00\x00", 4) != 0);
@@ -656,7 +656,7 @@ OM_uint32 _gssapi_unwrap_arcfour(OM_uint32 *minor_status,
_gsskrb5_decode_be_om_uint32(SND_SEQ, &seq_number);
if (context_handle->more_flags & LOCAL)
cmp = memcmp(&SND_SEQ[4], "\xff\xff\xff\xff", 4);
cmp = (memcmp(&SND_SEQ[4], "\xff\xff\xff\xff", 4) != 0);
else
cmp = (memcmp(&SND_SEQ[4], "\x00\x00\x00\x00", 4) != 0);
@@ -1274,7 +1274,7 @@ _gssapi_unwrap_iov_arcfour(OM_uint32 *minor_status,
_gsskrb5_decode_be_om_uint32(snd_seq, &seq_number);
if (ctx->more_flags & LOCAL) {
cmp = memcmp(&snd_seq[4], "\xff\xff\xff\xff", 4);
cmp = (memcmp(&snd_seq[4], "\xff\xff\xff\xff", 4) != 0);
} else {
cmp = (memcmp(&snd_seq[4], "\x00\x00\x00\x00", 4) != 0);
}
@@ -1351,8 +1351,8 @@ _gssapi_unwrap_iov_arcfour(OM_uint32 *minor_status,
return GSS_S_FAILURE;
}
cmp = memcmp(cksum_data, p0 + 16, 8); /* SGN_CKSUM */
if (cmp != 0) {
cmp = (memcmp(cksum_data, p0 + 16, 8) != 0); /* SGN_CKSUM */
if (cmp) {
*minor_status = 0;
return GSS_S_BAD_MIC;
}

View File

@@ -53,7 +53,7 @@ gss_compare_name(OM_uint32 *minor_status,
*name_equal = 0;
} else if (name1->gn_value.length != name2->gn_value.length ||
memcmp(name1->gn_value.value, name2->gn_value.value,
name1->gn_value.length)) {
name1->gn_value.length) != 0) {
*name_equal = 0;
}
} else {

View File

@@ -362,7 +362,7 @@ loop(gss_OID mechoid,
printf("localname: %.*s\n", (int)lname.length,
(char *)lname.value);
if (lname.length != strlen(localname_string) ||
strncmp(localname_string, lname.value, lname.length))
strncmp(localname_string, lname.value, lname.length) != 0)
errx(1, "localname: expected \"%s\", got \"%.*s\" (1)",
localname_string, (int)lname.length, (char *)lname.value);
gss_release_buffer(&min_stat, &lname);
@@ -372,7 +372,7 @@ loop(gss_OID mechoid,
errx(1, "localname: %s",
gssapi_err(maj_stat, min_stat, actual_mech_server));
if (lname.length != strlen(localname_string) ||
strncmp(localname_string, lname.value, lname.length))
strncmp(localname_string, lname.value, lname.length) != 0)
errx(1, "localname: expected \"%s\", got \"%.*s\" (2)",
localname_string, (int)lname.length, (char *)lname.value);
gss_release_buffer(&min_stat, &lname);

View File

@@ -55,7 +55,7 @@ main(int argc, char **argv)
ret = strncmp(data.value, "1 2 840 113554 1 2 2", data.length);
gss_release_buffer(&maj_stat, &data);
if (ret)
if (ret != 0)
return 1;
maj_stat = gss_oid_to_str(&minor_status, GSS_C_NT_EXPORT_NAME, &data);
@@ -64,7 +64,7 @@ main(int argc, char **argv)
ret = strncmp(data.value, "1 3 6 1 5 6 4", data.length);
gss_release_buffer(&maj_stat, &data);
if (ret)
if (ret != 0)
return 1;
return 0;