asn1/a_time.c: make handling of 'fractional point' formally correct.
authorAndy Polyakov <appro@openssl.org>
Mon, 31 Jul 2017 10:34:01 +0000 (12:34 +0200)
committerAndy Polyakov <appro@openssl.org>
Tue, 1 Aug 2017 20:39:27 +0000 (22:39 +0200)
Even though tm->length >= 15 && v[14] == '.' works in practice,
[because "YYYYMMDDHHMMSS." would be rejected as invalid by
asn1_time_to_tm,] formal correctness with respect to buffer
overstep in few lines vicinity improves readability.

[Also fold one if condition and improve expression readability.]

Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4058)

crypto/asn1/a_time.c

index b29aabeb512896b53fcfc9714a56cf501d87f1ae..b85f91760a31e498ffd39a4f41747156984c0775 100644 (file)
@@ -450,8 +450,7 @@ static const char _asn1_mon[12][4] = {
 int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
 {
     char *v;
 int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
 {
     char *v;
-    char *f = NULL;
-    int f_len = 0, gmt = 0, l;
+    int gmt = 0, l;
     struct tm stm;
 
     if (!asn1_time_to_tm(&stm, tm)) {
     struct tm stm;
 
     if (!asn1_time_to_tm(&stm, tm)) {
@@ -463,28 +462,32 @@ int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
     v = (char *)tm->data;
     if (v[l - 1] == 'Z')
         gmt = 1;
     v = (char *)tm->data;
     if (v[l - 1] == 'Z')
         gmt = 1;
-    /*
-     * Try to parse fractional seconds. '14' is the place of
-     * 'fraction point' in a GeneralizedTime string.
-     */
-    if (tm->type == V_ASN1_GENERALIZEDTIME
-        && tm->length >= 15 && v[14] == '.') {
-        f = &v[14];
-        f_len = 1;
-        while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
-            ++f_len;
-    }
 
 
-    if (tm->type == V_ASN1_GENERALIZEDTIME)
+    if (tm->type == V_ASN1_GENERALIZEDTIME) {
+        char *f = NULL;
+        int f_len = 0;
+
+        /*
+         * Try to parse fractional seconds. '14' is the place of
+         * 'fraction point' in a GeneralizedTime string.
+         */
+        if (tm->length > 15 && v[14] == '.') {
+            f = &v[14];
+            f_len = 1;
+            while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
+                ++f_len;
+        }
+
         return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
                           stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900,
         return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
                           stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900,
-                          (gmt) ? " GMT" : "") > 0;
-    else
+                          (gmt ? " GMT" : "")) > 0;
+    } else {
         return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
                           stm.tm_min, stm.tm_sec, stm.tm_year + 1900,
         return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
                           stm.tm_min, stm.tm_sec, stm.tm_year + 1900,
-                          (gmt) ? " GMT" : "") > 0;
+                          (gmt ? " GMT" : "")) > 0;
+    }
  err:
     BIO_write(bp, "Bad time value", 14);
     return 0;
  err:
     BIO_write(bp, "Bad time value", 14);
     return 0;