From 43ecb9c35caed8623cfd83e7d893b8b67725feb7 Mon Sep 17 00:00:00 2001 From: Rich Salz Date: Mon, 8 Feb 2016 10:11:56 -0500 Subject: [PATCH] GH641: Don't care openssl_zmalloc Don't cast malloc-family return values. Also found some places where (a) blank line was missing; and (b) the *wrong* return value was checked. Reviewed-by: Richard Levitte --- crypto/bio/b_addr.c | 7 ++++--- crypto/evp/cmeth_lib.c | 6 ++++-- crypto/evp/encode.c | 2 +- crypto/evp/evp_lib.c | 6 ++++-- crypto/hmac/hmac.c | 8 +++++--- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/crypto/bio/b_addr.c b/crypto/bio/b_addr.c index a2a0dd2647..39597b8651 100644 --- a/crypto/bio/b_addr.c +++ b/crypto/bio/b_addr.c @@ -75,7 +75,8 @@ BIO_ADDR *BIO_ADDR_new(void) { - BIO_ADDR *ret = (BIO_ADDR *)OPENSSL_zalloc(sizeof(BIO_ADDR)); + BIO_ADDR *ret = OPENSSL_zalloc(sizeof(*ret)); + ret->sa.sa_family = AF_UNSPEC; return ret; } @@ -557,10 +558,10 @@ static int addrinfo_wrap(int family, int socktype, { OPENSSL_assert(bai != NULL); - *bai = (BIO_ADDRINFO *)OPENSSL_zalloc(sizeof(**bai)); - + *bai = OPENSSL_zalloc(sizeof(**bai)); if (*bai == NULL) return 0; + (*bai)->bai_family = family; (*bai)->bai_socktype = socktype; if (socktype == SOCK_STREAM) diff --git a/crypto/evp/cmeth_lib.c b/crypto/evp/cmeth_lib.c index 1e7cac8157..33944e1894 100644 --- a/crypto/evp/cmeth_lib.c +++ b/crypto/evp/cmeth_lib.c @@ -64,7 +64,8 @@ EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len) { - EVP_CIPHER *cipher = (EVP_CIPHER *)OPENSSL_zalloc(sizeof(EVP_CIPHER)); + EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER)); + if (cipher != NULL) { cipher->nid = cipher_type; cipher->block_size = block_size; @@ -77,7 +78,8 @@ EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher) { EVP_CIPHER *to = EVP_CIPHER_meth_new(cipher->nid, cipher->block_size, cipher->key_len); - if (cipher != NULL) + + if (to != NULL) memcpy(to, cipher, sizeof(*to)); return to; } diff --git a/crypto/evp/encode.c b/crypto/evp/encode.c index de52ae4c2f..ed85f8933b 100644 --- a/crypto/evp/encode.c +++ b/crypto/evp/encode.c @@ -142,7 +142,7 @@ static unsigned char conv_ascii2bin(unsigned char a) EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void) { - return (EVP_ENCODE_CTX *)OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX)); + return OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX)); } void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx) diff --git a/crypto/evp/evp_lib.c b/crypto/evp/evp_lib.c index ff2a1d253a..a1636284c0 100644 --- a/crypto/evp/evp_lib.c +++ b/crypto/evp/evp_lib.c @@ -352,7 +352,8 @@ unsigned long EVP_MD_flags(const EVP_MD *md) EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type) { - EVP_MD *md = (EVP_MD *)OPENSSL_zalloc(sizeof(EVP_MD)); + EVP_MD *md = OPENSSL_zalloc(sizeof(*md)); + if (md != NULL) { md->type = md_type; md->pkey_type = pkey_type; @@ -362,7 +363,8 @@ EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type) EVP_MD *EVP_MD_meth_dup(const EVP_MD *md) { EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type); - if (md != NULL) + + if (to != NULL) memcpy(to, md, sizeof(*to)); return to; } diff --git a/crypto/hmac/hmac.c b/crypto/hmac/hmac.c index f372955c60..9504aada94 100644 --- a/crypto/hmac/hmac.c +++ b/crypto/hmac/hmac.c @@ -171,12 +171,14 @@ size_t HMAC_size(HMAC_CTX *ctx) HMAC_CTX *HMAC_CTX_new(void) { - HMAC_CTX *ctx = (HMAC_CTX *)OPENSSL_zalloc(sizeof(HMAC_CTX)); - if (ctx) + HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX)); + + if (ctx != NULL) { if (!HMAC_CTX_reset(ctx)) { HMAC_CTX_free(ctx); - ctx = NULL; + return NULL; } + } return ctx; } -- 2.34.1