From 137e5555bd3d1dc4486619bc524502c55682a6f4 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Thu, 28 Apr 2016 13:53:52 +0100 Subject: [PATCH] Don't leak memory on int X509_PURPOSE_add() error path The int X509_PURPOSE_add() function was leaking an X509_PURPOSE object on error. Reviewed-by: Richard Levitte --- crypto/x509v3/v3_purp.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crypto/x509v3/v3_purp.c b/crypto/x509v3/v3_purp.c index b757d8ed1a..b0d40ed84b 100644 --- a/crypto/x509v3/v3_purp.c +++ b/crypto/x509v3/v3_purp.c @@ -180,7 +180,7 @@ int X509_PURPOSE_add(int id, int trust, int flags, ptmp->sname = OPENSSL_strdup(sname); if (!ptmp->name || !ptmp->sname) { X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE); - return 0; + goto err; } /* Keep the dynamic flag of existing entry */ ptmp->flags &= X509_PURPOSE_DYNAMIC; @@ -197,14 +197,21 @@ int X509_PURPOSE_add(int id, int trust, int flags, if (xptable == NULL && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) { X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE); - return 0; + goto err; } if (!sk_X509_PURPOSE_push(xptable, ptmp)) { X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE); - return 0; + goto err; } } return 1; + err: + if (idx == -1) { + OPENSSL_free(ptmp->name); + OPENSSL_free(ptmp->sname); + OPENSSL_free(ptmp); + } + return 0; } static void xptable_free(X509_PURPOSE *p) -- 2.25.1