summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2014-04-21 11:37:42 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2014-04-21 11:37:42 +0000
commit22cdd9186213dfbf2ecfd2cfa6846e34c3ecc7a8 (patch)
tree52b117ad3cdd466ff7fdba70b2213d77893463cd
parent3318b9d45c118455136019a95e2082f6f26be5b7 (diff)
improve realloc/calloc/malloc patterns; ok guenther
-rw-r--r--lib/libssl/src/crypto/asn1/a_bitstr.c8
-rw-r--r--lib/libssl/src/crypto/asn1/a_bytes.c4
-rw-r--r--lib/libssl/src/crypto/asn1/a_enum.c3
-rw-r--r--lib/libssl/src/crypto/asn1/a_i2d_fp.c2
-rw-r--r--lib/libssl/src/crypto/asn1/a_int.c7
-rw-r--r--lib/libssl/src/crypto/asn1/a_object.c4
-rw-r--r--lib/libssl/src/crypto/asn1/ameth_lib.c4
-rw-r--r--lib/libssl/src/crypto/asn1/asn1_lib.c2
-rw-r--r--lib/libssl/src/crypto/asn1/asn_mime.c4
-rw-r--r--lib/libssl/src/crypto/asn1/f_enum.c7
-rw-r--r--lib/libssl/src/crypto/asn1/f_int.c7
-rw-r--r--lib/libssl/src/crypto/asn1/f_string.c7
-rw-r--r--lib/libssl/src/crypto/asn1/n_pkey.c2
-rw-r--r--lib/libssl/src/crypto/asn1/t_x509.c2
-rw-r--r--lib/libssl/src/crypto/asn1/tasn_new.c6
-rw-r--r--lib/libssl/src/crypto/asn1/x_info.c2
16 files changed, 23 insertions, 48 deletions
diff --git a/lib/libssl/src/crypto/asn1/a_bitstr.c b/lib/libssl/src/crypto/asn1/a_bitstr.c
index c578ce6279f..f3cce8b5366 100644
--- a/lib/libssl/src/crypto/asn1/a_bitstr.c
+++ b/lib/libssl/src/crypto/asn1/a_bitstr.c
@@ -153,7 +153,7 @@ c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **pp, long len)
if (len-- > 1) /* using one because of the bits left byte */
{
- s = (unsigned char *)malloc((int)len);
+ s = malloc((int)len);
if (s == NULL) {
i = ERR_R_MALLOC_FAILURE;
goto err;
@@ -203,11 +203,7 @@ ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
if ((a->length < (w + 1)) || (a->data == NULL)) {
if (!value)
return(1); /* Don't need to set */
- if (a->data == NULL)
- c = (unsigned char *)malloc(w + 1);
- else
- c = (unsigned char *)OPENSSL_realloc_clean(a->data,
- a->length, w + 1);
+ c = OPENSSL_realloc_clean(a->data, a->length, w + 1);
if (c == NULL) {
ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
return 0;
diff --git a/lib/libssl/src/crypto/asn1/a_bytes.c b/lib/libssl/src/crypto/asn1/a_bytes.c
index 30647c97b5e..34ed7b7db2b 100644
--- a/lib/libssl/src/crypto/asn1/a_bytes.c
+++ b/lib/libssl/src/crypto/asn1/a_bytes.c
@@ -99,7 +99,7 @@ d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp,
ret = (*a);
if (len != 0) {
- s = (unsigned char *)malloc((int)len + 1);
+ s = malloc((int)len + 1);
if (s == NULL) {
i = ERR_R_MALLOC_FAILURE;
goto err;
@@ -205,7 +205,7 @@ d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp,
if ((ret->length < len) || (ret->data == NULL)) {
if (ret->data != NULL)
free(ret->data);
- s = (unsigned char *)malloc((int)len + 1);
+ s = malloc(len + 1);
if (s == NULL) {
i = ERR_R_MALLOC_FAILURE;
goto err;
diff --git a/lib/libssl/src/crypto/asn1/a_enum.c b/lib/libssl/src/crypto/asn1/a_enum.c
index 5e6f7589cfe..aa28c7c8d78 100644
--- a/lib/libssl/src/crypto/asn1/a_enum.c
+++ b/lib/libssl/src/crypto/asn1/a_enum.c
@@ -78,8 +78,7 @@ ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
if (a->length < (int)(sizeof(long) + 1)) {
if (a->data != NULL)
free(a->data);
- if ((a->data = (unsigned char *)malloc(sizeof(long) + 1)) != NULL)
- memset((char *)a->data, 0, sizeof(long) + 1);
+ a->data = calloc(1, sizeof(long) + 1);
}
if (a->data == NULL) {
ASN1err(ASN1_F_ASN1_ENUMERATED_SET, ERR_R_MALLOC_FAILURE);
diff --git a/lib/libssl/src/crypto/asn1/a_i2d_fp.c b/lib/libssl/src/crypto/asn1/a_i2d_fp.c
index 082ba1b3a8e..007e612b4a6 100644
--- a/lib/libssl/src/crypto/asn1/a_i2d_fp.c
+++ b/lib/libssl/src/crypto/asn1/a_i2d_fp.c
@@ -89,7 +89,7 @@ ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, unsigned char *x)
int i, j = 0, n, ret = 1;
n = i2d(x, NULL);
- b = (char *)malloc(n);
+ b = malloc(n);
if (b == NULL) {
ASN1err(ASN1_F_ASN1_I2D_BIO, ERR_R_MALLOC_FAILURE);
return (0);
diff --git a/lib/libssl/src/crypto/asn1/a_int.c b/lib/libssl/src/crypto/asn1/a_int.c
index 05776f572c6..0559cce3843 100644
--- a/lib/libssl/src/crypto/asn1/a_int.c
+++ b/lib/libssl/src/crypto/asn1/a_int.c
@@ -205,7 +205,7 @@ c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, long len)
/* We must malloc stuff, even for 0 bytes otherwise it
* signifies a missing NULL parameter. */
- s = (unsigned char *)malloc((int)len + 1);
+ s = malloc((int)len + 1);
if (s == NULL) {
i = ERR_R_MALLOC_FAILURE;
goto err;
@@ -309,7 +309,7 @@ d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, long length)
/* We must malloc stuff, even for 0 bytes otherwise it
* signifies a missing NULL parameter. */
- s = (unsigned char *)malloc((int)len + 1);
+ s = malloc((int)len + 1);
if (s == NULL) {
i = ERR_R_MALLOC_FAILURE;
goto err;
@@ -352,8 +352,7 @@ ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
if (a->length < (int)(sizeof(long) + 1)) {
if (a->data != NULL)
free(a->data);
- if ((a->data = (unsigned char *)malloc(sizeof(long) + 1)) != NULL)
- memset((char *)a->data, 0, sizeof(long) + 1);
+ a->data = calloc(1, sizeof(long) + 1);
}
if (a->data == NULL) {
ASN1err(ASN1_F_ASN1_INTEGER_SET, ERR_R_MALLOC_FAILURE);
diff --git a/lib/libssl/src/crypto/asn1/a_object.c b/lib/libssl/src/crypto/asn1/a_object.c
index 93c755228a2..f86d54f5271 100644
--- a/lib/libssl/src/crypto/asn1/a_object.c
+++ b/lib/libssl/src/crypto/asn1/a_object.c
@@ -312,7 +312,7 @@ c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, long len)
ret->length = 0;
if (data != NULL)
free(data);
- data = (unsigned char *)malloc(len ? (int)len : 1);
+ data = malloc(len ? (int)len : 1);
if (data == NULL) {
i = ERR_R_MALLOC_FAILURE;
goto err;
@@ -345,7 +345,7 @@ ASN1_OBJECT_new(void)
{
ASN1_OBJECT *ret;
- ret = (ASN1_OBJECT *)malloc(sizeof(ASN1_OBJECT));
+ ret = malloc(sizeof(ASN1_OBJECT));
if (ret == NULL) {
ASN1err(ASN1_F_ASN1_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
return (NULL);
diff --git a/lib/libssl/src/crypto/asn1/ameth_lib.c b/lib/libssl/src/crypto/asn1/ameth_lib.c
index 63ff18edae4..8652e938bdc 100644
--- a/lib/libssl/src/crypto/asn1/ameth_lib.c
+++ b/lib/libssl/src/crypto/asn1/ameth_lib.c
@@ -287,12 +287,10 @@ EVP_PKEY_asn1_new(int id, int flags, const char *pem_str, const char *info)
{
EVP_PKEY_ASN1_METHOD *ameth;
- ameth = malloc(sizeof(EVP_PKEY_ASN1_METHOD));
+ ameth = calloc(1, sizeof(EVP_PKEY_ASN1_METHOD));
if (!ameth)
return NULL;
- memset(ameth, 0, sizeof(EVP_PKEY_ASN1_METHOD));
-
ameth->pkey_id = id;
ameth->pkey_base_id = id;
ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
diff --git a/lib/libssl/src/crypto/asn1/asn1_lib.c b/lib/libssl/src/crypto/asn1/asn1_lib.c
index f3b2f0480fb..4d4368aefe4 100644
--- a/lib/libssl/src/crypto/asn1/asn1_lib.c
+++ b/lib/libssl/src/crypto/asn1/asn1_lib.c
@@ -418,7 +418,7 @@ ASN1_STRING_type_new(int type)
{
ASN1_STRING *ret;
- ret = (ASN1_STRING *)malloc(sizeof(ASN1_STRING));
+ ret = malloc(sizeof(ASN1_STRING));
if (ret == NULL) {
ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
return (NULL);
diff --git a/lib/libssl/src/crypto/asn1/asn_mime.c b/lib/libssl/src/crypto/asn1/asn_mime.c
index 890557578d3..248ea114e89 100644
--- a/lib/libssl/src/crypto/asn1/asn_mime.c
+++ b/lib/libssl/src/crypto/asn1/asn_mime.c
@@ -850,7 +850,7 @@ mime_hdr_new(char *name, char *value)
}
}
} else tmpval = NULL;
- mhdr = (MIME_HEADER *)malloc(sizeof(MIME_HEADER));
+ mhdr = malloc(sizeof(MIME_HEADER));
if (!mhdr) {
OPENSSL_free(tmpname);
return NULL;
@@ -891,7 +891,7 @@ mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
} else
tmpval = NULL;
/* Parameter values are case sensitive so leave as is */
- mparam = (MIME_PARAM *) malloc(sizeof(MIME_PARAM));
+ mparam = malloc(sizeof(MIME_PARAM));
if (!mparam)
return 0;
mparam->param_name = tmpname;
diff --git a/lib/libssl/src/crypto/asn1/f_enum.c b/lib/libssl/src/crypto/asn1/f_enum.c
index e8736e5b726..98fa3122661 100644
--- a/lib/libssl/src/crypto/asn1/f_enum.c
+++ b/lib/libssl/src/crypto/asn1/f_enum.c
@@ -154,12 +154,7 @@ a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
}
i /= 2;
if (num + i > slen) {
- if (s == NULL)
- sp = (unsigned char *)malloc(
- (unsigned int)num + i * 2);
- else
- sp = (unsigned char *)realloc(s,
- (unsigned int)num + i * 2);
+ sp = realloc(s, (unsigned int)num + i * 2);
if (sp == NULL) {
ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,
ERR_R_MALLOC_FAILURE);
diff --git a/lib/libssl/src/crypto/asn1/f_int.c b/lib/libssl/src/crypto/asn1/f_int.c
index f355dbacbe5..3f671d1c490 100644
--- a/lib/libssl/src/crypto/asn1/f_int.c
+++ b/lib/libssl/src/crypto/asn1/f_int.c
@@ -158,12 +158,7 @@ a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)
}
i /= 2;
if (num + i > slen) {
- if (s == NULL)
- sp = (unsigned char *)malloc(
- (unsigned int)num + i * 2);
- else
- sp = OPENSSL_realloc_clean(s, slen,
- num + i * 2);
+ sp = OPENSSL_realloc_clean(s, slen, num + i * 2);
if (sp == NULL) {
ASN1err(ASN1_F_A2I_ASN1_INTEGER,
ERR_R_MALLOC_FAILURE);
diff --git a/lib/libssl/src/crypto/asn1/f_string.c b/lib/libssl/src/crypto/asn1/f_string.c
index d42bcdb6ea9..c213c7a88d3 100644
--- a/lib/libssl/src/crypto/asn1/f_string.c
+++ b/lib/libssl/src/crypto/asn1/f_string.c
@@ -150,12 +150,7 @@ a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
}
i /= 2;
if (num + i > slen) {
- if (s == NULL)
- sp = (unsigned char *)malloc(
- (unsigned int)num + i * 2);
- else
- sp = (unsigned char *)realloc(s,
- (unsigned int)num + i * 2);
+ sp = realloc(s, (unsigned int)num + i * 2);
if (sp == NULL) {
ASN1err(ASN1_F_A2I_ASN1_STRING,
ERR_R_MALLOC_FAILURE);
diff --git a/lib/libssl/src/crypto/asn1/n_pkey.c b/lib/libssl/src/crypto/asn1/n_pkey.c
index 0e58baf1b5b..1a724cfed71 100644
--- a/lib/libssl/src/crypto/asn1/n_pkey.c
+++ b/lib/libssl/src/crypto/asn1/n_pkey.c
@@ -163,7 +163,7 @@ i2d_RSA_NET(const RSA *a, unsigned char **pp,
}
/* Since its RC4 encrypted length is actual length */
- if ((zz = (unsigned char *)malloc(rsalen)) == NULL) {
+ if ((zz = malloc(rsalen)) == NULL) {
ASN1err(ASN1_F_I2D_RSA_NET, ERR_R_MALLOC_FAILURE);
goto err;
}
diff --git a/lib/libssl/src/crypto/asn1/t_x509.c b/lib/libssl/src/crypto/asn1/t_x509.c
index de3fa22171c..81333d67cf8 100644
--- a/lib/libssl/src/crypto/asn1/t_x509.c
+++ b/lib/libssl/src/crypto/asn1/t_x509.c
@@ -265,7 +265,7 @@ int X509_ocspid_print (BIO *bp, X509 *x)
if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
goto err;
derlen = i2d_X509_NAME(x->cert_info->subject, NULL);
- if ((der = dertmp = (unsigned char *)malloc (derlen)) == NULL)
+ if ((der = dertmp = malloc(derlen)) == NULL)
goto err;
i2d_X509_NAME(x->cert_info->subject, &dertmp);
diff --git a/lib/libssl/src/crypto/asn1/tasn_new.c b/lib/libssl/src/crypto/asn1/tasn_new.c
index dc9ddc413a6..56c6a19cfb6 100644
--- a/lib/libssl/src/crypto/asn1/tasn_new.c
+++ b/lib/libssl/src/crypto/asn1/tasn_new.c
@@ -156,10 +156,9 @@ asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
}
}
if (!combine) {
- *pval = malloc(it->size);
+ *pval = calloc(1, it->size);
if (!*pval)
goto memerr;
- memset(*pval, 0, it->size);
}
asn1_set_choice_selector(pval, -1, it);
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
@@ -181,10 +180,9 @@ asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
}
}
if (!combine) {
- *pval = malloc(it->size);
+ *pval = calloc(1, it->size);
if (!*pval)
goto memerr;
- memset(*pval, 0, it->size);
asn1_do_lock(pval, 0, it);
asn1_enc_init(pval, it);
}
diff --git a/lib/libssl/src/crypto/asn1/x_info.c b/lib/libssl/src/crypto/asn1/x_info.c
index 4d3e2ebd17f..2d1bf0d22d2 100644
--- a/lib/libssl/src/crypto/asn1/x_info.c
+++ b/lib/libssl/src/crypto/asn1/x_info.c
@@ -67,7 +67,7 @@ X509_INFO_new(void)
{
X509_INFO *ret = NULL;
- ret = (X509_INFO *)malloc(sizeof(X509_INFO));
+ ret = malloc(sizeof(X509_INFO));
if (ret == NULL) {
ASN1err(ASN1_F_X509_INFO_NEW, ERR_R_MALLOC_FAILURE);
return (NULL);