From f25cab4ea49f3888b3ea942c94524586adb27dee Mon Sep 17 00:00:00 2001
From: Jonas Maebe <jonas.maebe@elis.ugent.be>
Date: Tue, 3 Dec 2013 17:11:48 +0100
Subject: [PATCH 4/7] mime_hdr_addparam: free tmpname, tmpval and mparam on
 error path, and check whether sk_MIME_PARAM_push succeeds

---
 crypto/asn1/asn_mime.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/crypto/asn1/asn_mime.c b/crypto/asn1/asn_mime.c
index ce55d3a..2d0d334 100644
--- a/crypto/asn1/asn_mime.c
+++ b/crypto/asn1/asn_mime.c
@@ -843,12 +843,12 @@ static MIME_HEADER *mime_hdr_new(char *name, char *value)
 		
 static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
 {
-	char *tmpname, *tmpval, *p;
+	char *tmpname, *tmpval=NULL, *p;
 	int c;
-	MIME_PARAM *mparam;
+	MIME_PARAM *mparam=NULL;
 	if(name) {
 		tmpname = BUF_strdup(name);
-		if(!tmpname) return 0;
+		if(!tmpname) goto err;
 		for(p = tmpname ; *p; p++) {
 			c = (unsigned char)*p;
 			if(isupper(c)) {
@@ -859,15 +859,24 @@ static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
 	} else tmpname = NULL;
 	if(value) {
 		tmpval = BUF_strdup(value);
-		if(!tmpval) return 0;
+		if(!tmpval) goto err;
 	} else tmpval = NULL;
 	/* Parameter values are case sensitive so leave as is */
 	mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM));
-	if(!mparam) return 0;
+	if(!mparam) goto err;
 	mparam->param_name = tmpname;
 	mparam->param_value = tmpval;
-	sk_MIME_PARAM_push(mhdr->params, mparam);
+	if (!sk_MIME_PARAM_push(mhdr->params, mparam))
+		goto err;
 	return 1;
+	err:
+	if (tmpname != NULL)
+		BUF_MEM_free(tmpname);
+	if (tmpval != NULL)
+		BUF_MEM_free(tmpval);
+	if (mparam != NULL)
+		OPENSSL_free(mparam);
+	return 0;
 }
 
 static int mime_hdr_cmp(const MIME_HEADER * const *a,
-- 
1.8.3.1

