diff options
author | Rich Salz <rsalz@akamai.com> | 2015-05-01 23:10:31 -0400 |
---|---|---|
committer | Rich Salz <rsalz@openssl.org> | 2015-05-04 15:00:13 -0400 |
commit | b4faea50c35d92a67d1369355b49cc3efba78406 (patch) | |
tree | cfebea69d625f936c9fd7281f1fa3eaa2fa38834 /crypto | |
parent | 8920a7cd04f43b1a090d0b0a8c9e16b94c6898d4 (diff) |
Use safer sizeof variant in malloc
For a local variable:
TYPE *p;
Allocations like this are "risky":
p = OPENSSL_malloc(sizeof(TYPE));
if the type of p changes, and the malloc call isn't updated, you
could get memory corruption. Instead do this:
p = OPENSSL_malloc(sizeof(*p));
Also fixed a few memset() calls that I noticed while doing this.
Reviewed-by: Richard Levitte <levitte@openssl.org>
Diffstat (limited to 'crypto')
111 files changed, 203 insertions, 204 deletions
diff --git a/crypto/LPdir_unix.c b/crypto/LPdir_unix.c index bead6abd71..3a14da19b1 100644 --- a/crypto/LPdir_unix.c +++ b/crypto/LPdir_unix.c @@ -78,12 +78,12 @@ const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory) errno = 0; if (*ctx == NULL) { - *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX)); + *ctx = malloc(sizeof(**ctx)); if (*ctx == NULL) { errno = ENOMEM; return 0; } - memset(*ctx, '\0', sizeof(LP_DIR_CTX)); + memset(*ctx, '\0', sizeof(**ctx)); (*ctx)->dir = opendir(directory); if ((*ctx)->dir == NULL) { diff --git a/crypto/LPdir_vms.c b/crypto/LPdir_vms.c index 88c7ddd85c..1e8f9e7d61 100644 --- a/crypto/LPdir_vms.c +++ b/crypto/LPdir_vms.c @@ -104,12 +104,12 @@ const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory) return 0; } - *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX)); + *ctx = malloc(sizeof(**ctx)); if (*ctx == NULL) { errno = ENOMEM; return 0; } - memset(*ctx, '\0', sizeof(LP_DIR_CTX)); + memset(*ctx, '\0', sizeof(**ctx)); strcpy((*ctx)->filespec, directory); strcat((*ctx)->filespec, "*.*;"); diff --git a/crypto/LPdir_win.c b/crypto/LPdir_win.c index 07e63fb424..78a796de80 100644 --- a/crypto/LPdir_win.c +++ b/crypto/LPdir_win.c @@ -69,12 +69,12 @@ const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory) return 0; } - *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX)); + *ctx = malloc(sizeof(**ctx)); if (*ctx == NULL) { errno = ENOMEM; return 0; } - memset(*ctx, '\0', sizeof(LP_DIR_CTX)); + memset(*ctx, '\0', sizeof(**ctx)); if (directory[dirlen - 1] != '*') { extdirbuf = (char *)malloc(dirlen + 3); diff --git a/crypto/asn1/a_object.c b/crypto/asn1/a_object.c index 809e9a4112..7cedce82e7 100644 --- a/crypto/asn1/a_object.c +++ b/crypto/asn1/a_object.c @@ -345,7 +345,7 @@ ASN1_OBJECT *ASN1_OBJECT_new(void) { ASN1_OBJECT *ret; - ret = OPENSSL_malloc(sizeof(ASN1_OBJECT)); + ret = OPENSSL_malloc(sizeof(*ret)); if (ret == NULL) { ASN1err(ASN1_F_ASN1_OBJECT_NEW, ERR_R_MALLOC_FAILURE); return (NULL); diff --git a/crypto/asn1/a_strnid.c b/crypto/asn1/a_strnid.c index 5126298673..071613b89b 100644 --- a/crypto/asn1/a_strnid.c +++ b/crypto/asn1/a_strnid.c @@ -240,7 +240,7 @@ static ASN1_STRING_TABLE *stable_get(int nid) tmp = ASN1_STRING_TABLE_get(nid); if (tmp && tmp->flags & STABLE_FLAGS_MALLOC) return tmp; - rv = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE)); + rv = OPENSSL_malloc(sizeof(*rv)); if (!rv) return NULL; if (!sk_ASN1_STRING_TABLE_push(stable, rv)) { diff --git a/crypto/asn1/ameth_lib.c b/crypto/asn1/ameth_lib.c index 49f4e5abbf..c7acb468a6 100644 --- a/crypto/asn1/ameth_lib.c +++ b/crypto/asn1/ameth_lib.c @@ -284,7 +284,7 @@ EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags, const char *pem_str, const char *info) { EVP_PKEY_ASN1_METHOD *ameth; - ameth = OPENSSL_malloc(sizeof(EVP_PKEY_ASN1_METHOD)); + ameth = OPENSSL_malloc(sizeof(*ameth)); if (!ameth) return NULL; diff --git a/crypto/asn1/asn1_lib.c b/crypto/asn1/asn1_lib.c index e1e509b3ec..bda6ab09b4 100644 --- a/crypto/asn1/asn1_lib.c +++ b/crypto/asn1/asn1_lib.c @@ -349,7 +349,7 @@ ASN1_STRING *ASN1_STRING_type_new(int type) { ASN1_STRING *ret; - ret = OPENSSL_malloc(sizeof(ASN1_STRING)); + ret = OPENSSL_malloc(sizeof(*ret)); if (ret == NULL) { ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE); return (NULL); diff --git a/crypto/asn1/asn_mime.c b/crypto/asn1/asn_mime.c index e8411a82ad..da5b417048 100644 --- a/crypto/asn1/asn_mime.c +++ b/crypto/asn1/asn_mime.c @@ -844,7 +844,7 @@ static MIME_HEADER *mime_hdr_new(char *name, char *value) } } } - mhdr = OPENSSL_malloc(sizeof(MIME_HEADER)); + mhdr = OPENSSL_malloc(sizeof(*mhdr)); if (!mhdr) goto err; mhdr->name = tmpname; @@ -883,7 +883,7 @@ static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value) goto err; } /* Parameter values are case sensitive so leave as is */ - mparam = OPENSSL_malloc(sizeof(MIME_PARAM)); + mparam = OPENSSL_malloc(sizeof(*mparam)); if (!mparam) goto err; mparam->param_name = tmpname; diff --git a/crypto/asn1/bio_asn1.c b/crypto/asn1/bio_asn1.c index ba39069374..910d06f04d 100644 --- a/crypto/asn1/bio_asn1.c +++ b/crypto/asn1/bio_asn1.c @@ -146,7 +146,7 @@ BIO_METHOD *BIO_f_asn1(void) static int asn1_bio_new(BIO *b) { BIO_ASN1_BUF_CTX *ctx; - ctx = OPENSSL_malloc(sizeof(BIO_ASN1_BUF_CTX)); + ctx = OPENSSL_malloc(sizeof(*ctx)); if (!ctx) return 0; if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) { diff --git a/crypto/asn1/bio_ndef.c b/crypto/asn1/bio_ndef.c index de517f22e4..ff2fdf529b 100644 --- a/crypto/asn1/bio_ndef.c +++ b/crypto/asn1/bio_ndef.c @@ -106,7 +106,7 @@ BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it) ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED); return NULL; } - ndef_aux = OPENSSL_malloc(sizeof(NDEF_SUPPORT)); + ndef_aux = OPENSSL_malloc(sizeof(*ndef_aux)); asn_bio = BIO_new(BIO_f_asn1()); /* ASN1 bio needs to be next to output BIO |