Rename and tidy up x509v3_name_cmp.

First, rename to x509v3_conf_name_matches and flip the result value. We
don't need to preserve the positive vs negative return of strncmp here.
The rename is because "name" can mean so many things in the context of
X.509. Here, it's specifically the name of a CONF_VALUE.

Finally, fix it to be size_t-clean.

Bug: 516
Change-Id: I1c3039d9c6ce70cde669e07f943ad1e25fb49dc1
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/55705
Commit-Queue: Bob Beck <bbe@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
diff --git a/crypto/x509v3/internal.h b/crypto/x509v3/internal.h
index 00dae92..d077632 100644
--- a/crypto/x509v3/internal.h
+++ b/crypto/x509v3/internal.h
@@ -92,9 +92,9 @@
 // name, |string_to_hex| converted from hex.
 unsigned char *x509v3_hex_to_bytes(const char *str, long *len);
 
-// x509v3_name_cmp returns zero if |name| is equal to |cmp| or begins with |cmp|
-// followed by '.'. Otherwise, it returns a non-zero number.
-int x509v3_name_cmp(const char *name, const char *cmp);
+// x509v3_conf_name_matches returns one if |name| is equal to |cmp| or begins
+// with |cmp| followed by '.', and zero otherwise.
+int x509v3_conf_name_matches(const char *name, const char *cmp);
 
 // x509v3_looks_like_dns_name returns one if |in| looks like a DNS name and zero
 // otherwise.
diff --git a/crypto/x509v3/v3_alt.c b/crypto/x509v3/v3_alt.c
index 1f83d88..6123ac3 100644
--- a/crypto/x509v3/v3_alt.c
+++ b/crypto/x509v3/v3_alt.c
@@ -278,7 +278,7 @@
   }
   for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
     cnf = sk_CONF_VALUE_value(nval, i);
-    if (!x509v3_name_cmp(cnf->name, "issuer") && cnf->value &&
+    if (x509v3_conf_name_matches(cnf->name, "issuer") && cnf->value &&
         !strcmp(cnf->value, "copy")) {
       if (!copy_issuer(ctx, gens)) {
         goto err;
@@ -349,12 +349,12 @@
   }
   for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
     cnf = sk_CONF_VALUE_value(nval, i);
-    if (!x509v3_name_cmp(cnf->name, "email") && cnf->value &&
+    if (x509v3_conf_name_matches(cnf->name, "email") && cnf->value &&
         !strcmp(cnf->value, "copy")) {
       if (!copy_email(ctx, gens, 0)) {
         goto err;
       }
-    } else if (!x509v3_name_cmp(cnf->name, "email") && cnf->value &&
+    } else if (x509v3_conf_name_matches(cnf->name, "email") && cnf->value &&
                !strcmp(cnf->value, "move")) {
       if (!copy_email(ctx, gens, 1)) {
         goto err;
@@ -558,19 +558,19 @@
     return NULL;
   }
 
-  if (!x509v3_name_cmp(name, "email")) {
+  if (x509v3_conf_name_matches(name, "email")) {
     type = GEN_EMAIL;
-  } else if (!x509v3_name_cmp(name, "URI")) {
+  } else if (x509v3_conf_name_matches(name, "URI")) {
     type = GEN_URI;
-  } else if (!x509v3_name_cmp(name, "DNS")) {
+  } else if (x509v3_conf_name_matches(name, "DNS")) {
     type = GEN_DNS;
-  } else if (!x509v3_name_cmp(name, "RID")) {
+  } else if (x509v3_conf_name_matches(name, "RID")) {
     type = GEN_RID;
-  } else if (!x509v3_name_cmp(name, "IP")) {
+  } else if (x509v3_conf_name_matches(name, "IP")) {
     type = GEN_IPADD;
-  } else if (!x509v3_name_cmp(name, "dirName")) {
+  } else if (x509v3_conf_name_matches(name, "dirName")) {
     type = GEN_DIRNAME;
-  } else if (!x509v3_name_cmp(name, "otherName")) {
+  } else if (x509v3_conf_name_matches(name, "otherName")) {
     type = GEN_OTHERNAME;
   } else {
     OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNSUPPORTED_OPTION);
diff --git a/crypto/x509v3/v3_cpols.c b/crypto/x509v3/v3_cpols.c
index 82c68a1..5d781c0 100644
--- a/crypto/x509v3/v3_cpols.c
+++ b/crypto/x509v3/v3_cpols.c
@@ -241,7 +241,7 @@
       }
       pol->policyid = pobj;
 
-    } else if (!x509v3_name_cmp(cnf->name, "CPS")) {
+    } else if (x509v3_conf_name_matches(cnf->name, "CPS")) {
       if (!pol->qualifiers) {
         pol->qualifiers = sk_POLICYQUALINFO_new_null();
       }
@@ -263,7 +263,7 @@
       if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value, strlen(cnf->value))) {
         goto merr;
       }
-    } else if (!x509v3_name_cmp(cnf->name, "userNotice")) {
+    } else if (x509v3_conf_name_matches(cnf->name, "userNotice")) {
       STACK_OF(CONF_VALUE) *unot;
       if (*cnf->value != '@') {
         OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXPECTED_A_SECTION_NAME);
diff --git a/crypto/x509v3/v3_utl.c b/crypto/x509v3/v3_utl.c
index d21de45..466dbf2 100644
--- a/crypto/x509v3/v3_utl.c
+++ b/crypto/x509v3/v3_utl.c
@@ -556,18 +556,14 @@
   return NULL;
 }
 
-int x509v3_name_cmp(const char *name, const char *cmp) {
-  int len, ret;
-  char c;
-  len = strlen(cmp);
-  if ((ret = strncmp(name, cmp, len))) {
-    return ret;
-  }
-  c = name[len];
-  if (!c || (c == '.')) {
+int x509v3_conf_name_matches(const char *name, const char *cmp) {
+  // |name| must begin with |cmp|.
+  size_t len = strlen(cmp);
+  if (strncmp(name, cmp, len) != 0) {
     return 0;
   }
-  return 1;
+  // |name| must either be equal to |cmp| or begin with |cmp|, followed by '.'.
+  return name[len] == '\0' || name[len] == '.';
 }
 
 static int sk_strcmp(const char **a, const char **b) { return strcmp(*a, *b); }