Memory leak and NULL dereference fixes.
[openssl.git] / crypto / asn1 / a_utctm.c
index 2da5f255c603b31dbff49222ec6e3b7419254210..468123cc6f888918b13a7eede584d1c476e18179 100644 (file)
@@ -240,24 +240,29 @@ ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
        struct tm *ts;
        struct tm data;
        size_t len = 20;
+       int free_s = 0;
 
        if (s == NULL)
+               {
+               free_s = 1;
                s=M_ASN1_UTCTIME_new();
+               }
        if (s == NULL)
-               return(NULL);
+               goto err;
+
 
        ts=OPENSSL_gmtime(&t, &data);
        if (ts == NULL)
-               return(NULL);
+               goto err;
 
        if (offset_day || offset_sec)
                { 
                if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
-                       return NULL;
+                       goto err;
                }
 
        if((ts->tm_year < 50) || (ts->tm_year >= 150))
-               return NULL;
+               goto err;
 
        p=(char *)s->data;
        if ((p == NULL) || ((size_t)s->length < len))
@@ -266,7 +271,7 @@ ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
                if (p == NULL)
                        {
                        ASN1err(ASN1_F_ASN1_UTCTIME_ADJ,ERR_R_MALLOC_FAILURE);
-                       return(NULL);
+                       goto err;
                        }
                if (s->data != NULL)
                        OPENSSL_free(s->data);
@@ -281,6 +286,10 @@ ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
        ebcdic2ascii(s->data, s->data, s->length);
 #endif
        return(s);
+       err:
+       if (free_s && s)
+               M_ASN1_UTCTIME_free(s);
+       return NULL;
        }