Browse Source

strcase: use curl_str(n)equal for case insensitive matches

No point in having two entry points for the same functions.

Also merged the *safe* function treatment into these so that they can
also be used when one or both pointers are NULL.

Closes #9837
Daniel Stenberg 1 year ago
parent
commit
3f039dfd6f
7 changed files with 36 additions and 47 deletions
  1. 4 4
      lib/hsts.c
  2. 2 2
      lib/http2.c
  3. 21 28
      lib/strcase.c
  4. 2 6
      lib/strcase.h
  5. 1 1
      lib/url.c
  6. 1 1
      lib/vtls/openssl.c
  7. 5 5
      lib/vtls/vtls.c

+ 4 - 4
lib/hsts.c

@@ -157,7 +157,7 @@ CURLcode Curl_hsts_parse(struct hsts *h, const char *hostname,
   do {
     while(*p && ISBLANK(*p))
       p++;
-    if(Curl_strncasecompare("max-age=", p, 8)) {
+    if(strncasecompare("max-age=", p, 8)) {
       bool quoted = FALSE;
       CURLofft offt;
       char *endp;
@@ -186,7 +186,7 @@ CURLcode Curl_hsts_parse(struct hsts *h, const char *hostname,
       }
       gotma = TRUE;
     }
-    else if(Curl_strncasecompare("includesubdomains", p, 17)) {
+    else if(strncasecompare("includesubdomains", p, 17)) {
       if(gotinc)
         return CURLE_BAD_FUNCTION_ARGUMENT;
       subdomains = TRUE;
@@ -277,11 +277,11 @@ struct stsentry *Curl_hsts(struct hsts *h, const char *hostname,
         if(ntail < hlen) {
           size_t offs = hlen - ntail;
           if((hostname[offs-1] == '.') &&
-             Curl_strncasecompare(&hostname[offs], sts->host, ntail))
+             strncasecompare(&hostname[offs], sts->host, ntail))
             return sts;
         }
       }
-      if(Curl_strcasecompare(hostname, sts->host))
+      if(strcasecompare(hostname, sts->host))
         return sts;
     }
   }

+ 2 - 2
lib/http2.c

@@ -1024,9 +1024,9 @@ static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
       if(!check)
         /* no memory */
         return NGHTTP2_ERR_CALLBACK_FAILURE;
-      if(!Curl_strcasecompare(check, (const char *)value) &&
+      if(!strcasecompare(check, (const char *)value) &&
          ((conn->remote_port != conn->given->defport) ||
-          !Curl_strcasecompare(conn->host.name, (const char *)value))) {
+          !strcasecompare(conn->host.name, (const char *)value))) {
         /* This is push is not for the same authority that was asked for in
          * the URL. RFC 7540 section 8.2 says: "A client MUST treat a
          * PUSH_PROMISE for which the server is not authoritative as a stream

+ 21 - 28
lib/strcase.c

@@ -83,16 +83,13 @@ char Curl_raw_tolower(char in)
 }
 
 /*
- * Curl_strcasecompare() is for doing "raw" case insensitive strings. This is
- * meant to be locale independent and only compare strings we know are safe
- * for this.  See
- * https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for some
- * further explanation to why this function is necessary.
- *
- * @unittest: 1301
+ * curl_strequal() is for doing "raw" case insensitive strings. This is meant
+ * to be locale independent and only compare strings we know are safe for
+ * this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
+ * further explanations as to why this function is necessary.
  */
 
-int Curl_strcasecompare(const char *first, const char *second)
+static int casecompare(const char *first, const char *second)
 {
   while(*first && *second) {
     if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
@@ -108,25 +105,22 @@ int Curl_strcasecompare(const char *first, const char *second)
   return !*first == !*second;
 }
 
-int Curl_safe_strcasecompare(const char *first, const char *second)
+/* --- public function --- */
+int curl_strequal(const char *first, const char *second)
 {
   if(first && second)
     /* both pointers point to something then compare them */
-    return Curl_strcasecompare(first, second);
+    return casecompare(first, second);
 
   /* if both pointers are NULL then treat them as equal */
   return (NULL == first && NULL == second);
 }
 
-/*
- * @unittest: 1301
- */
-int Curl_strncasecompare(const char *first, const char *second, size_t max)
+static int ncasecompare(const char *first, const char *second, size_t max)
 {
   while(*first && *second && max) {
-    if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
-      break;
-    }
+    if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
+      return 0;
     max--;
     first++;
     second++;
@@ -137,6 +131,16 @@ int Curl_strncasecompare(const char *first, const char *second, size_t max)
   return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
 }
 
+/* --- public function --- */
+int curl_strnequal(const char *first, const char *second, size_t max)
+{
+  if(first && second)
+    /* both pointers point to something then compare them */
+    return ncasecompare(first, second, max);
+
+  /* if both pointers are NULL then treat them as equal if max is non-zero */
+  return (NULL == first && NULL == second && max);
+}
 /* Copy an upper case version of the string from src to dest.  The
  * strings may overlap.  No more than n characters of the string are copied
  * (including any NUL) and the destination string will NOT be
@@ -198,14 +202,3 @@ int Curl_timestrcmp(const char *a, const char *b)
     return a || b;
   return match;
 }
-
-/* --- public functions --- */
-
-int curl_strequal(const char *first, const char *second)
-{
-  return Curl_strcasecompare(first, second);
-}
-int curl_strnequal(const char *first, const char *second, size_t max)
-{
-  return Curl_strncasecompare(first, second, max);
-}

+ 2 - 6
lib/strcase.h

@@ -35,12 +35,8 @@
  * Result is 1 if text matches and 0 if not.
  */
 
-#define strcasecompare(a,b) Curl_strcasecompare(a,b)
-#define strncasecompare(a,b,c) Curl_strncasecompare(a,b,c)
-
-int Curl_strcasecompare(const char *first, const char *second);
-int Curl_safe_strcasecompare(const char *first, const char *second);
-int Curl_strncasecompare(const char *first, const char *second, size_t max);
+#define strcasecompare(a,b) curl_strequal(a,b)
+#define strncasecompare(a,b,c) curl_strnequal(a,b,c)
 
 char Curl_raw_toupper(char in);
 char Curl_raw_tolower(char in);

+ 1 - 1
lib/url.c

@@ -933,7 +933,7 @@ proxy_info_matches(const struct proxy_info *data,
 {
   if((data->proxytype == needle->proxytype) &&
      (data->port == needle->port) &&
-     Curl_safe_strcasecompare(data->host.name, needle->host.name))
+     strcasecompare(data->host.name, needle->host.name))
     return TRUE;
 
   return FALSE;

+ 1 - 1
lib/vtls/openssl.c

@@ -4364,7 +4364,7 @@ static size_t ossl_version(char *buffer, size_t size)
   int count;
   const char *ver = OpenSSL_version(OPENSSL_VERSION);
   const char expected[] = OSSL_PACKAGE " "; /* ie "LibreSSL " */
-  if(Curl_strncasecompare(ver, expected, sizeof(expected) - 1)) {
+  if(strncasecompare(ver, expected, sizeof(expected) - 1)) {
     ver += sizeof(expected) - 1;
   }
   count = msnprintf(buffer, size, "%s/%s", OSSL_PACKAGE, ver);

+ 5 - 5
lib/vtls/vtls.c

@@ -150,11 +150,11 @@ Curl_ssl_config_matches(struct ssl_primary_config *data,
      !Curl_timestrcmp(data->password, needle->password) &&
      (data->authtype == needle->authtype) &&
 #endif
-     Curl_safe_strcasecompare(data->cipher_list, needle->cipher_list) &&
-     Curl_safe_strcasecompare(data->cipher_list13, needle->cipher_list13) &&
-     Curl_safe_strcasecompare(data->curves, needle->curves) &&
-     Curl_safe_strcasecompare(data->CRLfile, needle->CRLfile) &&
-     Curl_safe_strcasecompare(data->pinned_key, needle->pinned_key))
+     strcasecompare(data->cipher_list, needle->cipher_list) &&
+     strcasecompare(data->cipher_list13, needle->cipher_list13) &&
+     strcasecompare(data->curves, needle->curves) &&
+     strcasecompare(data->CRLfile, needle->CRLfile) &&
+     strcasecompare(data->pinned_key, needle->pinned_key))
     return TRUE;
 
   return FALSE;