x509asn1: clean up GTime2str
authorDaniel Stenberg <daniel@haxx.se>
Tue, 30 Jul 2024 08:05:17 +0000 (10:05 +0200)
committerAquila Macedo Costa <aquilamacedo@riseup.net>
Tue, 17 Sep 2024 19:29:24 +0000 (16:29 -0300)
Co-authored-by: Stefan Eissing
Reported-by: Dov Murik
Closes #14307

Backported to Debian by Carlos Henrique Lima Melara <charles@debian.org>.

Changes:
- In this version, GTime2str doesn't return CURLcode, so change that to NULL.

Gbp-Pq: Name CVE-2024-7264-0.patch

lib/vtls/x509asn1.c

index 39e4fb33bc37dec22c7c5024d6441321bfa8bb9c..27f85120c7a3b74520c0cdb119f2ec3502844be8 100644 (file)
@@ -543,7 +543,7 @@ static const char *GTime2str(const char *beg, const char *end)
   /* Convert an ASN.1 Generalized time to a printable string.
      Return the dynamically allocated string, or NULL if an error occurs. */
 
-  for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
+  for(fracp = beg; fracp < end && ISDIGIT(*fracp); fracp++)
     ;
 
   /* Get seconds digits. */
@@ -562,17 +562,22 @@ static const char *GTime2str(const char *beg, const char *end)
     return NULL;
   }
 
-  /* Scan for timezone, measure fractional seconds. */
+  /* timezone follows optional fractional seconds. */
   tzp = fracp;
-  fracl = 0;
+  fracl = 0; /* no fractional seconds detected so far */
   if(fracp < end && (*fracp == '.' || *fracp == ',')) {
-    fracp++;
-    do
+    /* Have fractional seconds, e.g. "[.,]\d+". How many? */
+    tzp = fracp++; /* should be a digit char or BAD ARGUMENT */
+    while(tzp < end && ISDIGIT(*tzp))
       tzp++;
-    while(tzp < end && *tzp >= '0' && *tzp <= '9');
-    /* Strip leading zeroes in fractional seconds. */
-    for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
-      ;
+    if(tzp == fracp) /* never looped, no digit after [.,] */
+      return NULL;
+    fracl = tzp - fracp - 1; /* number of fractional sec digits */
+    DEBUGASSERT(fracl > 0);
+    /* Strip trailing zeroes in fractional seconds.
+     * May reduce fracl to 0 if only '0's are present. */
+    while(fracl && fracp[fracl - 1] == '0')
+      fracl--;
   }
 
   /* Process timezone. */