From bad6b116a2d3c005330e618c726f172fd0fefc2a Mon Sep 17 00:00:00 2001 From: FdaSilvaYY Date: Thu, 10 Nov 2016 00:54:03 +0100 Subject: [PATCH] Raise an error on memory alloc failure. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both strdup or malloc failure should raise à err. Reviewed-by: Andy Polyakov Reviewed-by: Rich Salz (Merged from https://github.com/openssl/openssl/pull/1905) --- crypto/dh/dh_meth.c | 37 ++++++++++++++++++------------------- crypto/dsa/dsa_meth.c | 37 ++++++++++++++++++------------------- crypto/rsa/rsa_meth.c | 37 ++++++++++++++++++------------------- 3 files changed, 54 insertions(+), 57 deletions(-) diff --git a/crypto/dh/dh_meth.c b/crypto/dh/dh_meth.c index 45753b6164..ce6114c133 100644 --- a/crypto/dh/dh_meth.c +++ b/crypto/dh/dh_meth.c @@ -13,19 +13,20 @@ DH_METHOD *DH_meth_new(const char *name, int flags) { - DH_METHOD *dhm = OPENSSL_zalloc(sizeof(DH_METHOD)); + DH_METHOD *dhm = OPENSSL_zalloc(sizeof(*dhm)); if (dhm != NULL) { - dhm->name = OPENSSL_strdup(name); - if (dhm->name == NULL) { - OPENSSL_free(dhm); - DHerr(DH_F_DH_METH_NEW, ERR_R_MALLOC_FAILURE); - return NULL; - } dhm->flags = flags; + + dhm->name = OPENSSL_strdup(name); + if (dhm->name != NULL) + return dhm; + + OPENSSL_free(dhm); } - return dhm; + DHerr(DH_F_DH_METH_NEW, ERR_R_MALLOC_FAILURE); + return NULL; } void DH_meth_free(DH_METHOD *dhm) @@ -38,21 +39,20 @@ void DH_meth_free(DH_METHOD *dhm) DH_METHOD *DH_meth_dup(const DH_METHOD *dhm) { - DH_METHOD *ret; - - ret = OPENSSL_malloc(sizeof(DH_METHOD)); + DH_METHOD *ret = OPENSSL_malloc(sizeof(*ret)); if (ret != NULL) { memcpy(ret, dhm, sizeof(*dhm)); + ret->name = OPENSSL_strdup(dhm->name); - if (ret->name == NULL) { - OPENSSL_free(ret); - DHerr(DH_F_DH_METH_DUP, ERR_R_MALLOC_FAILURE); - return NULL; - } + if (ret->name != NULL) + return ret; + + OPENSSL_free(ret); } - return ret; + DHerr(DH_F_DH_METH_DUP, ERR_R_MALLOC_FAILURE); + return NULL; } const char *DH_meth_get0_name(const DH_METHOD *dhm) @@ -62,9 +62,8 @@ const char *DH_meth_get0_name(const DH_METHOD *dhm) int DH_meth_set1_name(DH_METHOD *dhm, const char *name) { - char *tmpname; + char *tmpname = OPENSSL_strdup(name); - tmpname = OPENSSL_strdup(name); if (tmpname == NULL) { DHerr(DH_F_DH_METH_SET1_NAME, ERR_R_MALLOC_FAILURE); return 0; diff --git a/crypto/dsa/dsa_meth.c b/crypto/dsa/dsa_meth.c index a8cee06fc3..f0188f2007 100644 --- a/crypto/dsa/dsa_meth.c +++ b/crypto/dsa/dsa_meth.c @@ -21,19 +21,20 @@ DSA_METHOD *DSA_meth_new(const char *name, int flags) { - DSA_METHOD *dsam = OPENSSL_zalloc(sizeof(DSA_METHOD)); + DSA_METHOD *dsam = OPENSSL_zalloc(sizeof(*dsam)); if (dsam != NULL) { - dsam->name = OPENSSL_strdup(name); - if (dsam->name == NULL) { - OPENSSL_free(dsam); - DSAerr(DSA_F_DSA_METH_NEW, ERR_R_MALLOC_FAILURE); - return NULL; - } dsam->flags = flags; + + dsam->name = OPENSSL_strdup(name); + if (dsam->name != NULL) + return dsam; + + OPENSSL_free(dsam); } - return dsam; + DSAerr(DSA_F_DSA_METH_NEW, ERR_R_MALLOC_FAILURE); + return NULL; } void DSA_meth_free(DSA_METHOD *dsam) @@ -46,21 +47,20 @@ void DSA_meth_free(DSA_METHOD *dsam) DSA_METHOD *DSA_meth_dup(const DSA_METHOD *dsam) { - DSA_METHOD *ret; - - ret = OPENSSL_malloc(sizeof(DSA_METHOD)); + DSA_METHOD *ret = OPENSSL_malloc(sizeof(*ret)); if (ret != NULL) { memcpy(ret, dsam, sizeof(*dsam)); + ret->name = OPENSSL_strdup(dsam->name); - if (ret->name == NULL) { - OPENSSL_free(ret); - DSAerr(DSA_F_DSA_METH_DUP, ERR_R_MALLOC_FAILURE); - return NULL; - } + if (ret->name != NULL) + return ret; + + OPENSSL_free(ret); } - return ret; + DSAerr(DSA_F_DSA_METH_DUP, ERR_R_MALLOC_FAILURE); + return NULL; } const char *DSA_meth_get0_name(const DSA_METHOD *dsam) @@ -70,9 +70,8 @@ const char *DSA_meth_get0_name(const DSA_METHOD *dsam) int DSA_meth_set1_name(DSA_METHOD *dsam, const char *name) { - char *tmpname; + char *tmpname = OPENSSL_strdup(name); - tmpname = OPENSSL_strdup(name); if (tmpname == NULL) { DSAerr(DSA_F_DSA_METH_SET1_NAME, ERR_R_MALLOC_FAILURE); return 0; diff --git a/crypto/rsa/rsa_meth.c b/crypto/rsa/rsa_meth.c index ef0dc9751f..9480abd700 100644 --- a/crypto/rsa/rsa_meth.c +++ b/crypto/rsa/rsa_meth.c @@ -13,19 +13,20 @@ RSA_METHOD *RSA_meth_new(const char *name, int flags) { - RSA_METHOD *meth = OPENSSL_zalloc(sizeof(RSA_METHOD)); + RSA_METHOD *meth = OPENSSL_zalloc(sizeof(*meth)); if (meth != NULL) { - meth->name = OPENSSL_strdup(name); - if (meth->name == NULL) { - OPENSSL_free(meth); - RSAerr(RSA_F_RSA_METH_NEW, ERR_R_MALLOC_FAILURE); - return NULL; - } meth->flags = flags; + + meth->name = OPENSSL_strdup(name); + if (meth->name != NULL) + return meth; + + OPENSSL_free(meth); } - return meth; + RSAerr(RSA_F_RSA_METH_NEW, ERR_R_MALLOC_FAILURE); + return NULL; } void RSA_meth_free(RSA_METHOD *meth) @@ -38,21 +39,20 @@ void RSA_meth_free(RSA_METHOD *meth) RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth) { - RSA_METHOD *ret; - - ret = OPENSSL_malloc(sizeof(RSA_METHOD)); + RSA_METHOD *ret = OPENSSL_malloc(sizeof(*ret)); if (ret != NULL) { memcpy(ret, meth, sizeof(*meth)); + ret->name = OPENSSL_strdup(meth->name); - if (ret->name == NULL) { - OPENSSL_free(ret); - RSAerr(RSA_F_RSA_METH_DUP, ERR_R_MALLOC_FAILURE); - return NULL; - } + if (ret->name != NULL) + return ret; + + OPENSSL_free(ret); } - return ret; + RSAerr(RSA_F_RSA_METH_DUP, ERR_R_MALLOC_FAILURE); + return NULL; } const char *RSA_meth_get0_name(const RSA_METHOD *meth) @@ -62,9 +62,8 @@ const char *RSA_meth_get0_name(const RSA_METHOD *meth) int RSA_meth_set1_name(RSA_METHOD *meth, const char *name) { - char *tmpname; + char *tmpname = OPENSSL_strdup(name); - tmpname = OPENSSL_strdup(name); if (tmpname == NULL) { RSAerr(RSA_F_RSA_METH_SET1_NAME, ERR_R_MALLOC_FAILURE); return 0;