summaryrefslogtreecommitdiff
path: root/lib/libcrypto/asn1
diff options
context:
space:
mode:
authorMiod Vallat <miod@cvs.openbsd.org>2014-05-15 21:07:11 +0000
committerMiod Vallat <miod@cvs.openbsd.org>2014-05-15 21:07:11 +0000
commit0f5f083616289ace95f0d378cb2abe9039a096dd (patch)
treef025c9e0d7f9e87ba611efbf846a613c01ab2b0f /lib/libcrypto/asn1
parent06c9bf96ea51d14b7758a71b4cf5b5fb5cde2ac5 (diff)
Fix memory leaks upon failure.
ok beck@
Diffstat (limited to 'lib/libcrypto/asn1')
-rw-r--r--lib/libcrypto/asn1/asn_mime.c42
-rw-r--r--lib/libcrypto/asn1/asn_pack.c24
2 files changed, 39 insertions, 27 deletions
diff --git a/lib/libcrypto/asn1/asn_mime.c b/lib/libcrypto/asn1/asn_mime.c
index b30c366561b..6ea47dacf87 100644
--- a/lib/libcrypto/asn1/asn_mime.c
+++ b/lib/libcrypto/asn1/asn_mime.c
@@ -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
diff --git a/lib/libcrypto/asn1/asn_pack.c b/lib/libcrypto/asn1/asn_pack.c
index 8eb39e6a9b6..7f46e1100d4 100644
--- a/lib/libcrypto/asn1/asn_pack.c
+++ b/lib/libcrypto/asn1/asn_pack.c
@@ -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 */