From 5ba06897be4fb001113f6e8ea2398538dbbb88f2 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sun, 7 Feb 2016 09:36:04 -1000 Subject: [PATCH] Don't cast |OPENSSL_malloc|/|OPENSSL_realloc| result. C has implicit conversion of |void *| to other pointer types so these casts are unnecessary. Clean them up to make the code easier to read and to make it easier to find dangerous casts. Change-Id: I26988a672e8ed4d69c75cfbb284413999b475464 Reviewed-on: https://boringssl-review.googlesource.com/7102 Reviewed-by: David Benjamin --- crypto/bio/buffer.c | 6 +++--- crypto/bn/bn.c | 2 +- crypto/bn/convert.c | 7 +++---- crypto/bn/exponentiation.c | 2 +- crypto/dh/dh.c | 2 +- crypto/dsa/dsa.c | 2 +- crypto/ec/ec_key.c | 2 +- crypto/evp/p_dsa_asn1.c | 2 +- crypto/evp/p_ec_asn1.c | 2 +- crypto/evp/p_rsa_asn1.c | 2 +- crypto/modes/gcm.c | 2 +- crypto/rsa/rsa.c | 2 +- ssl/pqueue/pqueue.c | 4 ++-- ssl/ssl_aead_ctx.c | 2 +- ssl/ssl_cert.c | 4 ++-- ssl/ssl_cipher.c | 2 +- ssl/ssl_lib.c | 4 ++-- ssl/ssl_session.c | 2 +- ssl/t1_enc.c | 2 +- ssl/t1_lib.c | 9 +++------ 20 files changed, 29 insertions(+), 33 deletions(-) diff --git a/crypto/bio/buffer.c b/crypto/bio/buffer.c index 9d0cb3c00..15574510c 100644 --- a/crypto/bio/buffer.c +++ b/crypto/bio/buffer.c @@ -100,7 +100,7 @@ static int buffer_new(BIO *bio) { if (ctx->ibuf == NULL) { goto err1; } - ctx->obuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE); + ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE); if (ctx->obuf == NULL) { goto err2; } @@ -340,13 +340,13 @@ static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr) { p1 = ctx->ibuf; p2 = ctx->obuf; if (ibs > DEFAULT_BUFFER_SIZE && ibs != ctx->ibuf_size) { - p1 = (char *)OPENSSL_malloc(ibs); + p1 = OPENSSL_malloc(ibs); if (p1 == NULL) { goto malloc_error; } } if (obs > DEFAULT_BUFFER_SIZE && obs != ctx->obuf_size) { - p2 = (char *)OPENSSL_malloc(obs); + p2 = OPENSSL_malloc(obs); if (p2 == NULL) { if (p1 != ctx->ibuf) { OPENSSL_free(p1); diff --git a/crypto/bn/bn.c b/crypto/bn/bn.c index 543c14820..2263701f8 100644 --- a/crypto/bn/bn.c +++ b/crypto/bn/bn.c @@ -295,7 +295,7 @@ BIGNUM *bn_wexpand(BIGNUM *bn, size_t words) { return NULL; } - a = (BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG) * words); + a = OPENSSL_malloc(sizeof(BN_ULONG) * words); if (a == NULL) { OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); return NULL; diff --git a/crypto/bn/convert.c b/crypto/bn/convert.c index 1f7af64bf..542f523fe 100644 --- a/crypto/bn/convert.c +++ b/crypto/bn/convert.c @@ -208,7 +208,7 @@ char *BN_bn2hex(const BIGNUM *bn) { char *buf; char *p; - buf = (char *)OPENSSL_malloc(bn->top * BN_BYTES * 2 + 2); + buf = OPENSSL_malloc(bn->top * BN_BYTES * 2 + 2); if (buf == NULL) { OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); return NULL; @@ -385,9 +385,8 @@ char *BN_bn2dec(const BIGNUM *a) { */ i = BN_num_bits(a) * 3; num = i / 10 + i / 1000 + 1 + 1; - bn_data = - (BN_ULONG *)OPENSSL_malloc((num / BN_DEC_NUM + 1) * sizeof(BN_ULONG)); - buf = (char *)OPENSSL_malloc(num + 3); + bn_data = OPENSSL_malloc((num / BN_DEC_NUM + 1) * sizeof(BN_ULONG)); + buf = OPENSSL_malloc(num + 3); if ((buf == NULL) || (bn_data == NULL)) { OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); goto err; diff --git a/crypto/bn/exponentiation.c b/crypto/bn/exponentiation.c index 72a8db4b6..f7100ac3c 100644 --- a/crypto/bn/exponentiation.c +++ b/crypto/bn/exponentiation.c @@ -954,7 +954,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, } else #endif { - if ((powerbufFree = (unsigned char *)OPENSSL_malloc( + if ((powerbufFree = OPENSSL_malloc( powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL) { goto err; } diff --git a/crypto/dh/dh.c b/crypto/dh/dh.c index bf6196ceb..aed8720bc 100644 --- a/crypto/dh/dh.c +++ b/crypto/dh/dh.c @@ -74,7 +74,7 @@ static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT; DH *DH_new(void) { - DH *dh = (DH *)OPENSSL_malloc(sizeof(DH)); + DH *dh = OPENSSL_malloc(sizeof(DH)); if (dh == NULL) { OPENSSL_PUT_ERROR(DH, ERR_R_MALLOC_FAILURE); return NULL; diff --git a/crypto/dsa/dsa.c b/crypto/dsa/dsa.c index ebe55e84c..1e446920b 100644 --- a/crypto/dsa/dsa.c +++ b/crypto/dsa/dsa.c @@ -86,7 +86,7 @@ static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT; DSA *DSA_new(void) { - DSA *dsa = (DSA *)OPENSSL_malloc(sizeof(DSA)); + DSA *dsa = OPENSSL_malloc(sizeof(DSA)); if (dsa == NULL) { OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE); return NULL; diff --git a/crypto/ec/ec_key.c b/crypto/ec/ec_key.c index d3bf4c617..5b015f50b 100644 --- a/crypto/ec/ec_key.c +++ b/crypto/ec/ec_key.c @@ -85,7 +85,7 @@ static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT; EC_KEY *EC_KEY_new(void) { return EC_KEY_new_method(NULL); } EC_KEY *EC_KEY_new_method(const ENGINE *engine) { - EC_KEY *ret = (EC_KEY *)OPENSSL_malloc(sizeof(EC_KEY)); + EC_KEY *ret = OPENSSL_malloc(sizeof(EC_KEY)); if (ret == NULL) { OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE); return NULL; diff --git a/crypto/evp/p_dsa_asn1.c b/crypto/evp/p_dsa_asn1.c index ea75e58c5..a876eef48 100644 --- a/crypto/evp/p_dsa_asn1.c +++ b/crypto/evp/p_dsa_asn1.c @@ -436,7 +436,7 @@ static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) { update_buflen(priv_key, &buf_len); update_buflen(pub_key, &buf_len); - m = (uint8_t *)OPENSSL_malloc(buf_len + 10); + m = OPENSSL_malloc(buf_len + 10); if (m == NULL) { OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE); goto err; diff --git a/crypto/evp/p_ec_asn1.c b/crypto/evp/p_ec_asn1.c index e12508dba..e093c1830 100644 --- a/crypto/evp/p_ec_asn1.c +++ b/crypto/evp/p_ec_asn1.c @@ -279,7 +279,7 @@ static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) { OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB); return 0; } - ep = (uint8_t *)OPENSSL_malloc(eplen); + ep = OPENSSL_malloc(eplen); if (!ep) { EC_KEY_set_enc_flags(ec_key, old_flags); OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE); diff --git a/crypto/evp/p_rsa_asn1.c b/crypto/evp/p_rsa_asn1.c index 8556b5ad7..70f0b763f 100644 --- a/crypto/evp/p_rsa_asn1.c +++ b/crypto/evp/p_rsa_asn1.c @@ -216,7 +216,7 @@ static int do_rsa_print(BIO *out, const RSA *rsa, int off, } } - m = (uint8_t *)OPENSSL_malloc(buf_len + 10); + m = OPENSSL_malloc(buf_len + 10); if (m == NULL) { OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE); goto err; diff --git a/crypto/modes/gcm.c b/crypto/modes/gcm.c index 65451a6cc..2519b19fb 100644 --- a/crypto/modes/gcm.c +++ b/crypto/modes/gcm.c @@ -408,7 +408,7 @@ void gcm_ghash_neon(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp, GCM128_CONTEXT *CRYPTO_gcm128_new(const void *key, block128_f block) { GCM128_CONTEXT *ret; - ret = (GCM128_CONTEXT *)OPENSSL_malloc(sizeof(GCM128_CONTEXT)); + ret = OPENSSL_malloc(sizeof(GCM128_CONTEXT)); if (ret != NULL) { CRYPTO_gcm128_init(ret, key, block); } diff --git a/crypto/rsa/rsa.c b/crypto/rsa/rsa.c index 6c28ad76f..9ffea1f2a 100644 --- a/crypto/rsa/rsa.c +++ b/crypto/rsa/rsa.c @@ -76,7 +76,7 @@ static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT; RSA *RSA_new(void) { return RSA_new_method(NULL); } RSA *RSA_new_method(const ENGINE *engine) { - RSA *rsa = (RSA *)OPENSSL_malloc(sizeof(RSA)); + RSA *rsa = OPENSSL_malloc(sizeof(RSA)); if (rsa == NULL) { OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); return NULL; diff --git a/ssl/pqueue/pqueue.c b/ssl/pqueue/pqueue.c index 14bd9b630..e68976118 100644 --- a/ssl/pqueue/pqueue.c +++ b/ssl/pqueue/pqueue.c @@ -69,7 +69,7 @@ typedef struct _pqueue { pitem *pitem_new(uint8_t prio64be[8], void *data) { - pitem *item = (pitem *)OPENSSL_malloc(sizeof(pitem)); + pitem *item = OPENSSL_malloc(sizeof(pitem)); if (item == NULL) { return NULL; } @@ -91,7 +91,7 @@ void pitem_free(pitem *item) { } pqueue pqueue_new(void) { - pqueue_s *pq = (pqueue_s *)OPENSSL_malloc(sizeof(pqueue_s)); + pqueue_s *pq = OPENSSL_malloc(sizeof(pqueue_s)); if (pq == NULL) { return NULL; } diff --git a/ssl/ssl_aead_ctx.c b/ssl/ssl_aead_ctx.c index 8829679bb..ea44a6cbf 100644 --- a/ssl/ssl_aead_ctx.c +++ b/ssl/ssl_aead_ctx.c @@ -56,7 +56,7 @@ SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction, enc_key_len += fixed_iv_len; } - SSL_AEAD_CTX *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX)); + SSL_AEAD_CTX *aead_ctx = OPENSSL_malloc(sizeof(SSL_AEAD_CTX)); if (aead_ctx == NULL) { OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); return NULL; diff --git a/ssl/ssl_cert.c b/ssl/ssl_cert.c index 4952cfdee..983e1a7bd 100644 --- a/ssl/ssl_cert.c +++ b/ssl/ssl_cert.c @@ -139,7 +139,7 @@ int SSL_get_ex_data_X509_STORE_CTX_idx(void) { } CERT *ssl_cert_new(void) { - CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); + CERT *ret = OPENSSL_malloc(sizeof(CERT)); if (ret == NULL) { OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); return NULL; @@ -150,7 +150,7 @@ CERT *ssl_cert_new(void) { } CERT *ssl_cert_dup(CERT *cert) { - CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); + CERT *ret = OPENSSL_malloc(sizeof(CERT)); if (ret == NULL) { OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); return NULL; diff --git a/ssl/ssl_cipher.c b/ssl/ssl_cipher.c index ba7384883..4ff9f5c22 100644 --- a/ssl/ssl_cipher.c +++ b/ssl/ssl_cipher.c @@ -1392,7 +1392,7 @@ ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method, /* Now we have to collect the available ciphers from the compiled in ciphers. * We cannot get more than the number compiled in, so it is used for * allocation. */ - co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen); + co_list = OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen); if (co_list == NULL) { OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); return NULL; diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c index 56edf2fb8..542dc17e4 100644 --- a/ssl/ssl_lib.c +++ b/ssl/ssl_lib.c @@ -221,7 +221,7 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) { goto err; } - ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); + ret = OPENSSL_malloc(sizeof(SSL_CTX)); if (ret == NULL) { goto err; } @@ -353,7 +353,7 @@ SSL *SSL_new(SSL_CTX *ctx) { return NULL; } - SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL)); + SSL *ssl = OPENSSL_malloc(sizeof(SSL)); if (ssl == NULL) { goto err; } diff --git a/ssl/ssl_session.c b/ssl/ssl_session.c index 24de4ec50..804a04f60 100644 --- a/ssl/ssl_session.c +++ b/ssl/ssl_session.c @@ -161,7 +161,7 @@ static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session); static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock); SSL_SESSION *SSL_SESSION_new(void) { - SSL_SESSION *session = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION)); + SSL_SESSION *session = OPENSSL_malloc(sizeof(SSL_SESSION)); if (session == NULL) { OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); return 0; diff --git a/ssl/t1_enc.c b/ssl/t1_enc.c index a64e9d824..216514dc4 100644 --- a/ssl/t1_enc.c +++ b/ssl/t1_enc.c @@ -373,7 +373,7 @@ int tls1_setup_key_block(SSL *ssl) { ssl3_cleanup_key_block(ssl); - uint8_t *keyblock = (uint8_t *)OPENSSL_malloc(key_block_len); + uint8_t *keyblock = OPENSSL_malloc(key_block_len); if (keyblock == NULL) { OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); return 0; diff --git a/ssl/t1_lib.c b/ssl/t1_lib.c index c010d7f44..434286bfd 100644 --- a/ssl/t1_lib.c +++ b/ssl/t1_lib.c @@ -169,8 +169,7 @@ static int tls1_check_duplicate_extensions(const CBS *cbs) { return 1; } - extension_types = - (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions); + extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions); if (extension_types == NULL) { OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); goto done; @@ -377,7 +376,7 @@ int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len, uint16_t *curve_ids; size_t i; - curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t)); + curve_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t)); if (curve_ids == NULL) { return 0; } @@ -1905,9 +1904,7 @@ static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert, return 0; } - ssl->s3->tmp.peer_ellipticcurvelist = - (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list)); - + ssl->s3->tmp.peer_ellipticcurvelist = OPENSSL_malloc(CBS_len(&elliptic_curve_list)); if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) { *out_alert = SSL_AD_INTERNAL_ERROR; return 0;