Fix memory leaks upon failure.
authormiod <miod@openbsd.org>
Thu, 15 May 2014 21:07:10 +0000 (21:07 +0000)
committermiod <miod@openbsd.org>
Thu, 15 May 2014 21:07:10 +0000 (21:07 +0000)
ok beck@

lib/libcrypto/asn1/asn_mime.c
lib/libcrypto/asn1/asn_pack.c
lib/libssl/src/crypto/asn1/asn_mime.c
lib/libssl/src/crypto/asn1/asn_pack.c

index b30c366..6ea47da 100644 (file)
@@ -825,11 +825,12 @@ static MIME_HEADER *
 mime_hdr_new(char *name, char *value)
 {
        MIME_HEADER *mhdr;
-       char *tmpname, *tmpval, *p;
+       char *tmpname = NULL, *tmpval = NULL, *p;
        int c;
+
        if (name) {
                if (!(tmpname = BUF_strdup(name)))
-                       return NULL;
+                       goto err;
                for (p = tmpname; *p; p++) {
                        c = (unsigned char)*p;
                        if (isupper(c)) {
@@ -837,11 +838,10 @@ mime_hdr_new(char *name, char *value)
                                *p = c;
                        }
                }
-       } else
-               tmpname = NULL;
+       }
        if (value) {
                if (!(tmpval = BUF_strdup(value)))
-                       return NULL;
+                       goto err;
                for (p = tmpval; *p; p++) {
                        c = (unsigned char)*p;
                        if (isupper(c)) {
@@ -849,32 +849,34 @@ mime_hdr_new(char *name, char *value)
                                *p = c;
                        }
                }
-       } else tmpval = NULL;
-               mhdr = malloc(sizeof(MIME_HEADER));
-       if (!mhdr) {
-               OPENSSL_free(tmpname);
-               return NULL;
        }
+       mhdr = malloc(sizeof(MIME_HEADER));
+       if (!mhdr)
+               goto err;
        mhdr->name = tmpname;
        mhdr->value = tmpval;
        if (!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) {
                free(mhdr);
-               return NULL;
+               goto err;
        }
        return mhdr;
+err:
+       free(tmpname);
+       free(tmpval);
+       return NULL;
 }
 
 static int
 mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
 {
-       char *tmpname, *tmpval, *p;
+       char *tmpname = NULL, *tmpval = NULL, *p;
        int c;
        MIME_PARAM *mparam;
 
        if (name) {
                tmpname = BUF_strdup(name);
                if (!tmpname)
-                       return 0;
+                       goto err;
                for (p = tmpname; *p; p++) {
                        c = (unsigned char)*p;
                        if (isupper(c)) {
@@ -882,22 +884,24 @@ mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
                                *p = c;
                        }
                }
-       } else
-               tmpname = NULL;
+       }
        if (value) {
                tmpval = BUF_strdup(value);
                if (!tmpval)
-                       return 0;
-       } else
-               tmpval = NULL;
+                       goto err;
+       }
        /* Parameter values are case sensitive so leave as is */
        mparam = malloc(sizeof(MIME_PARAM));
        if (!mparam)
-               return 0;
+               goto err;
        mparam->param_name = tmpname;
        mparam->param_value = tmpval;
        sk_MIME_PARAM_push(mhdr->params, mparam);
        return 1;
+err:
+       free(tmpname);
+       free(tmpval);
+       return 0;
 }
 
 static int
index 8eb39e6..7f46e11 100644 (file)
@@ -137,22 +137,26 @@ ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct)
                        ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
                        return NULL;
                }
-               if (oct)
-                       *oct = octmp;
        } else
                octmp = *oct;
                
        if (!(octmp->length = i2d(obj, NULL))) {
                ASN1err(ASN1_F_ASN1_PACK_STRING,ASN1_R_ENCODE_ERROR);
-               return NULL;
+               goto err;
        }
        if (!(p = malloc (octmp->length))) {
                ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
-               return NULL;
+               goto err;
        }
        octmp->data = p;
        i2d (obj, &p);
+       if (oct)
+               *oct = octmp;
        return octmp;
+err:
+       if (!oct || octmp != *oct)
+               ASN1_STRING_free(octmp);
+       return NULL;
 }
 
 #endif
@@ -169,8 +173,6 @@ ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
                        ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
                        return NULL;
                }
-               if (oct)
-                       *oct = octmp;
        } else
                octmp = *oct;
 
@@ -181,13 +183,19 @@ ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
 
        if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
                ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR);
-               return NULL;
+               goto err;
        }
        if (!octmp->data) {
                ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
-               return NULL;
+               goto err;
        }
+       if (oct)
+               *oct = octmp;
        return octmp;
+err:
+       if (!oct || octmp != *oct)
+               ASN1_STRING_free(octmp);
+       return NULL;
 }
 
 /* Extract an ASN1 object from an ASN1_STRING */
index b30c366..6ea47da 100644 (file)
@@ -825,11 +825,12 @@ static MIME_HEADER *
 mime_hdr_new(char *name, char *value)
 {
        MIME_HEADER *mhdr;
-       char *tmpname, *tmpval, *p;
+       char *tmpname = NULL, *tmpval = NULL, *p;
        int c;
+
        if (name) {
                if (!(tmpname = BUF_strdup(name)))
-                       return NULL;
+                       goto err;
                for (p = tmpname; *p; p++) {
                        c = (unsigned char)*p;
                        if (isupper(c)) {
@@ -837,11 +838,10 @@ mime_hdr_new(char *name, char *value)
                                *p = c;
                        }
                }
-       } else
-               tmpname = NULL;
+       }
        if (value) {
                if (!(tmpval = BUF_strdup(value)))
-                       return NULL;
+                       goto err;
                for (p = tmpval; *p; p++) {
                        c = (unsigned char)*p;
                        if (isupper(c)) {
@@ -849,32 +849,34 @@ mime_hdr_new(char *name, char *value)
                                *p = c;
                        }
                }
-       } else tmpval = NULL;
-               mhdr = malloc(sizeof(MIME_HEADER));
-       if (!mhdr) {
-               OPENSSL_free(tmpname);
-               return NULL;
        }
+       mhdr = malloc(sizeof(MIME_HEADER));
+       if (!mhdr)
+               goto err;
        mhdr->name = tmpname;
        mhdr->value = tmpval;
        if (!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) {
                free(mhdr);
-               return NULL;
+               goto err;
        }
        return mhdr;
+err:
+       free(tmpname);
+       free(tmpval);
+       return NULL;
 }
 
 static int
 mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
 {
-       char *tmpname, *tmpval, *p;
+       char *tmpname = NULL, *tmpval = NULL, *p;
        int c;
        MIME_PARAM *mparam;
 
        if (name) {
                tmpname = BUF_strdup(name);
                if (!tmpname)
-                       return 0;
+                       goto err;
                for (p = tmpname; *p; p++) {
                        c = (unsigned char)*p;
                        if (isupper(c)) {
@@ -882,22 +884,24 @@ mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
                                *p = c;
                        }
                }
-       } else
-               tmpname = NULL;
+       }
        if (value) {
                tmpval = BUF_strdup(value);
                if (!tmpval)
-                       return 0;
-       } else
-               tmpval = NULL;
+                       goto err;
+       }
        /* Parameter values are case sensitive so leave as is */
        mparam = malloc(sizeof(MIME_PARAM));
        if (!mparam)
-               return 0;
+               goto err;
        mparam->param_name = tmpname;
        mparam->param_value = tmpval;
        sk_MIME_PARAM_push(mhdr->params, mparam);
        return 1;
+err:
+       free(tmpname);
+       free(tmpval);
+       return 0;
 }
 
 static int
index 8eb39e6..7f46e11 100644 (file)
@@ -137,22 +137,26 @@ ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct)
                        ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
                        return NULL;
                }
-               if (oct)
-                       *oct = octmp;
        } else
                octmp = *oct;
                
        if (!(octmp->length = i2d(obj, NULL))) {
                ASN1err(ASN1_F_ASN1_PACK_STRING,ASN1_R_ENCODE_ERROR);
-               return NULL;
+               goto err;
        }
        if (!(p = malloc (octmp->length))) {
                ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
-               return NULL;
+               goto err;
        }
        octmp->data = p;
        i2d (obj, &p);
+       if (oct)
+               *oct = octmp;
        return octmp;
+err:
+       if (!oct || octmp != *oct)
+               ASN1_STRING_free(octmp);
+       return NULL;
 }
 
 #endif
@@ -169,8 +173,6 @@ ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
                        ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
                        return NULL;
                }
-               if (oct)
-                       *oct = octmp;
        } else
                octmp = *oct;
 
@@ -181,13 +183,19 @@ ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
 
        if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
                ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR);
-               return NULL;
+               goto err;
        }
        if (!octmp->data) {
                ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
-               return NULL;
+               goto err;
        }
+       if (oct)
+               *oct = octmp;
        return octmp;
+err:
+       if (!oct || octmp != *oct)
+               ASN1_STRING_free(octmp);
+       return NULL;
 }
 
 /* Extract an ASN1 object from an ASN1_STRING */