]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Add and use OPENSSL_zalloc
authorRich Salz <rsalz@akamai.com>
Tue, 25 Aug 2015 17:25:58 +0000 (13:25 -0400)
committerRich Salz <rsalz@openssl.org>
Thu, 3 Sep 2015 02:05:37 +0000 (22:05 -0400)
There are many places (nearly 50) where we malloc and then memset.
Add an OPENSSL_zalloc routine to encapsulate that.
(Missed one conversion; thanks Richard)
Also fixes GH328

Reviewed-by: Richard Levitte <levitte@openssl.org>
45 files changed:
crypto/asn1/ameth_lib.c
crypto/asn1/tasn_new.c
crypto/asn1/x_pkey.c
crypto/bio/bss_acpt.c
crypto/bio/bss_conn.c
crypto/bio/bss_dgram.c
crypto/bn/bn_blind.c
crypto/comp/c_zlib.c
crypto/comp/comp_lib.c
crypto/dso/dso_lib.c
crypto/dso/dso_win32.c
crypto/ec/ecp_nistp224.c
crypto/ec/ecp_nistp521.c
crypto/engine/eng_cryptodev.c
crypto/engine/eng_dyn.c
crypto/engine/eng_lib.c
crypto/evp/evp_enc.c
crypto/evp/pmeth_lib.c
crypto/mem.c
crypto/pqueue/pqueue.c
crypto/rsa/rsa_lib.c
crypto/rsa/rsa_pk1.c
crypto/sec_mem.c
crypto/store/str_lib.c
crypto/store/str_mem.c
crypto/store/str_meth.c
crypto/ts/ts_rsp_sign.c
crypto/ts/ts_verify_ctx.c
crypto/ui/ui_lib.c
crypto/x509/x509_vfy.c
crypto/x509/x509_vpm.c
crypto/x509v3/pcy_tree.c
engines/ccgost/gost_pmeth.c
include/openssl/crypto.h
ssl/bio_ssl.c
ssl/d1_both.c
ssl/d1_lib.c
ssl/s3_lib.c
ssl/ssl_cert.c
ssl/ssl_ciph.c
ssl/ssl_lib.c
ssl/ssl_sess.c
test/ecdsatest.c
test/ssltest.c
util/libeay.num

index 8060c18b19d97968f2615a3da3511cb7d9098ea7..155de83aaaef7022ffd9837727f24108c279e1fb 100644 (file)
@@ -283,12 +283,11 @@ const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(EVP_PKEY *pkey)
 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(*ameth));
+    EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
+
     if (!ameth)
         return NULL;
 
-    memset(ameth, 0, sizeof(*ameth));
     ameth->pkey_id = id;
     ameth->pkey_base_id = id;
     ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
index f54bd9bcf14d8e27d0268972dfcb65c6bdc0ace8..e7ceda3c5495aecb3665cd0f5ffc6c73eba18e6b 100644 (file)
@@ -135,10 +135,9 @@ int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
                 return 1;
             }
         }
-        *pval = OPENSSL_malloc(it->size);
+        *pval = OPENSSL_zalloc(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))
             goto auxerr;
@@ -158,10 +157,9 @@ int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
                 return 1;
             }
         }
-        *pval = OPENSSL_malloc(it->size);
+        *pval = OPENSSL_zalloc(it->size);
         if (!*pval)
             goto memerr;
-        memset(*pval, 0, it->size);
         asn1_do_lock(pval, 0, it);
         asn1_enc_init(pval, it);
         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
index afd3aab2d23dde74c531108f10986fcd38991494..07104195960506dbcd3f07bf575e7ed2df820744 100644 (file)
@@ -66,10 +66,9 @@ X509_PKEY *X509_PKEY_new(void)
 {
     X509_PKEY *ret = NULL;
 
-    ret = OPENSSL_malloc(sizeof(*ret));
+    ret = OPENSSL_zalloc(sizeof(*ret));
     if (!ret)
         goto err;
-    memset(ret, 0, sizeof(*ret));
 
     ret->version = 0;
     ret->enc_algor = X509_ALGOR_new();
index 16a660800d6b888b2d01facc5b1f5b358da1369c..eba6e25714cbf8796a1f959ff707d4810c9cf3c7 100644 (file)
@@ -137,10 +137,8 @@ static BIO_ACCEPT *BIO_ACCEPT_new(void)
 {
     BIO_ACCEPT *ret;
 
-    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
+    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
         return (NULL);
-
-    memset(ret, 0, sizeof(*ret));
     ret->accept_sock = INVALID_SOCKET;
     ret->bind_mode = BIO_BIND_NORMAL;
     return (ret);
index f23adb217af70d39899372d747efb42ec5f24e69..0733a296754a82eae6bd6405fba213bcba331c99 100644 (file)
@@ -286,19 +286,12 @@ BIO_CONNECT *BIO_CONNECT_new(void)
 {
     BIO_CONNECT *ret;
 
-    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
+    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
         return (NULL);
     ret->state = BIO_CONN_S_BEFORE;
     ret->param_hostname = NULL;
     ret->param_port = NULL;
     ret->info_callback = NULL;
-    ret->nbio = 0;
-    ret->ip[0] = 0;
-    ret->ip[1] = 0;
-    ret->ip[2] = 0;
-    ret->ip[3] = 0;
-    ret->port = 0;
-    memset(&ret->them, 0, sizeof(ret->them));
     return (ret);
 }
 
index dabfea35c040b1bf1b72e0ed65d1a527c805e1ff..bbb9aca1c4d7efb68aba88731c4adc871a4678db 100644 (file)
@@ -221,16 +221,13 @@ BIO *BIO_new_dgram(int fd, int close_flag)
 
 static int dgram_new(BIO *bi)
 {
-    bio_dgram_data *data = NULL;
+    bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
 
-    bi->init = 0;
-    bi->num = 0;
-    data = OPENSSL_malloc(sizeof(*data));
     if (data == NULL)
         return 0;
-    memset(data, 0, sizeof(*data));
+    bi->init = 0;
+    bi->num = 0;
     bi->ptr = data;
-
     bi->flags = 0;
     return (1);
 }
@@ -997,16 +994,13 @@ BIO *BIO_new_dgram_sctp(int fd, int close_flag)
      * connected socket won't use it.
      */
     sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
-    authchunks = OPENSSL_malloc(sockopt_len);
+    authchunks = OPENSSL_zalloc(sockopt_len);
     if (!authchunks) {
         BIO_vfree(bio);
         return (NULL);
     }
-    memset(authchunks, 0, sockopt_len);
-    ret =
-        getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
+    ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
                    &sockopt_len);
-
     if (ret < 0) {
         OPENSSL_free(authchunks);
         BIO_vfree(bio);
@@ -1086,10 +1080,9 @@ static int dgram_sctp_new(BIO *bi)
 
     bi->init = 0;
     bi->num = 0;
-    data = OPENSSL_malloc(sizeof(*data));
+    data = OPENSSL_zalloc(sizeof(*data));
     if (data == NULL)
         return 0;
-    memset(data, 0, sizeof(*data));
 #  ifdef SCTP_PR_SCTP_NONE
     data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
 #  endif
index 4ae6b096687c78cd713ad62aa86aed30372c2a16..7ca13bb31e6ce1635d1fe61787617139a76a328f 100644 (file)
@@ -137,11 +137,10 @@ BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
 
     bn_check_top(mod);
 
-    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
+    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
         BNerr(BN_F_BN_BLINDING_NEW, ERR_R_MALLOC_FAILURE);
         return (NULL);
     }
-    memset(ret, 0, sizeof(*ret));
     if (A != NULL) {
         if ((ret->A = BN_dup(A)) == NULL)
             goto err;
index f0fc0aff9e59fad30fc4677014236f8b80704965..474751192a2df9a096d7595ffe00de07867dfa7f 100644 (file)
@@ -91,9 +91,7 @@ static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
 {
     void *p;
 
-    p = OPENSSL_malloc(no * size);
-    if (p)
-        memset(p, 0, no * size);
+    p = OPENSSL_zalloc(no * size);
     return p;
 }
 
index aa82376cdde8f84d03ba502ff1f030f4aec98a22..83fea93cb80d45f2508b03dcde1fc6d97f52955a 100644 (file)
@@ -63,9 +63,8 @@ COMP_CTX *COMP_CTX_new(COMP_METHOD *meth)
 {
     COMP_CTX *ret;
 
-    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
+    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
         return (NULL);
-    memset(ret, 0, sizeof(*ret));
     ret->meth = meth;
     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
         OPENSSL_free(ret);
index 12544b3a1610ec0ad609ef3c7d346fa0723a8e89..17d1732dfc31a9d8f289bf24bfe1938c1daf81af 100644 (file)
@@ -104,12 +104,11 @@ DSO *DSO_new_method(DSO_METHOD *meth)
          */
         default_DSO_meth = DSO_METHOD_openssl();
     }
-    ret = OPENSSL_malloc(sizeof(*ret));
+    ret = OPENSSL_zalloc(sizeof(*ret));
     if (ret == NULL) {
         DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
         return (NULL);
     }
-    memset(ret, 0, sizeof(*ret));
     ret->meth_data = sk_void_new_null();
     if (ret->meth_data == NULL) {
         /* sk_new doesn't generate any errors so we do */
index 2da318f70783dd8705c58c72aa0ef11a9c8b7e02..c6fec6625abf4e20bcd5d36deda9086906f29a76 100644 (file)
@@ -304,13 +304,12 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
         return (NULL);
     }
 
-    result = OPENSSL_malloc(sizeof(*result));
+    result = OPENSSL_zalloc(sizeof(*result));
     if (result == NULL) {
         DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
         return (NULL);
     }
 
-    memset(result, 0, sizeof(*result));
     position = IN_DEVICE;
 
     if ((filename[0] == '\\' && filename[1] == '\\')
index febfcab6f9927dfa1cd94b540050cc9bca67ee98..8b1deaae7bc8c11b02e4cb513507819bb6eb446e 100644 (file)
@@ -1199,13 +1199,12 @@ static void batch_mul(felem x_out, felem y_out, felem z_out,
 
 static NISTP224_PRE_COMP *nistp224_pre_comp_new()
 {
-    NISTP224_PRE_COMP *ret = NULL;
-    ret = OPENSSL_malloc(sizeof(*ret));
+    NISTP224_PRE_COMP *ret = OPENSSL_zalloc(sizeof(*ret));
+
     if (!ret) {
         ECerr(EC_F_NISTP224_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
         return ret;
     }
-    memset(ret->g_pre_comp, 0, sizeof(ret->g_pre_comp));
     ret->references = 1;
     return ret;
 }
@@ -1457,8 +1456,8 @@ int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
              */
             mixed = 1;
         }
-        secrets = OPENSSL_malloc(sizeof(*secrets) * num_points);
-        pre_comp = OPENSSL_malloc(sizeof(*pre_comp) * num_points);
+        secrets = OPENSSL_zalloc(sizeof(*secrets) * num_points);
+        pre_comp = OPENSSL_zalloc(sizeof(*pre_comp) * num_points);
         if (mixed)
             tmp_felems =
                 OPENSSL_malloc(sizeof(felem) * (num_points * 17 + 1));
@@ -1472,8 +1471,6 @@ int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
          * we treat NULL scalars as 0, and NULL points as points at infinity,
          * i.e., they contribute nothing to the linear combination
          */
-        memset(secrets, 0, sizeof(*secrets) * num_points);
-        memset(pre_comp, 0, sizeof(*pre_comp) * num_points);
         for (i = 0; i < num_points; ++i) {
             if (i == num)
                 /* the generator */
index a5d7360bb913c45617a2e9d4e584a4c962d85c03..febf5e94b7445ffeae52f704ec84761fcb19385f 100644 (file)
@@ -1644,13 +1644,12 @@ const EC_METHOD *EC_GFp_nistp521_method(void)
 
 static NISTP521_PRE_COMP *nistp521_pre_comp_new()
 {
-    NISTP521_PRE_COMP *ret = OPENSSL_malloc(sizeof(*ret));
+    NISTP521_PRE_COMP *ret = OPENSSL_zalloc(sizeof(*ret));
 
     if (!ret) {
         ECerr(EC_F_NISTP521_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
         return ret;
     }
-    memset(ret->g_pre_comp, 0, sizeof(ret->g_pre_comp));
     ret->references = 1;
     return ret;
 }
@@ -1902,8 +1901,8 @@ int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
              */
             mixed = 1;
         }
-        secrets = OPENSSL_malloc(sizeof(*secrets) * num_points);
-        pre_comp = OPENSSL_malloc(sizeof(*pre_comp) * num_points);
+        secrets = OPENSSL_zalloc(sizeof(*secrets) * num_points);
+        pre_comp = OPENSSL_zalloc(sizeof(*pre_comp) * num_points);
         if (mixed)
             tmp_felems =
                 OPENSSL_malloc(sizeof(*tmp_felems) * (num_points * 17 + 1));
@@ -1917,8 +1916,6 @@ int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
          * we treat NULL scalars as 0, and NULL points as points at infinity,
          * i.e., they contribute nothing to the linear combination
          */
-        memset(secrets, 0, sizeof(*secrets) * num_points);
-        memset(pre_comp, 0, sizeof(*pre_comp) * num_points);
         for (i = 0; i < num_points; ++i) {
             if (i == num)
                 /*
index 49a398964b024ddd97e160dbb921ddd2f8d8753c..ca567dc67d931f6fa86e31afbe45e7b30dd500dc 100644 (file)
@@ -1020,10 +1020,9 @@ static int bn2crparam(const BIGNUM *a, struct crparam *crp)
     bits = BN_num_bits(a);
     bytes = BN_num_bytes(a);
 
-    b = OPENSSL_malloc(bytes);
+    b = OPENSSL_zalloc(bytes);
     if (b == NULL)
         return (1);
-    memset(b, 0, bytes);
 
     crp->crp_p = (caddr_t) b;
     crp->crp_nbits = bits;
index ae7d1d0fe80833e9619f769be99b267cee19d52d..777f44018b0b84a3a05fbbc79f2170b2af3f2325 100644 (file)
@@ -202,13 +202,12 @@ static void dynamic_data_ctx_free_func(void *parent, void *ptr,
  */
 static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
 {
-    dynamic_data_ctx *c = OPENSSL_malloc(sizeof(*c));
+    dynamic_data_ctx *c = OPENSSL_zalloc(sizeof(*c));
 
     if (!c) {
         ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
         return 0;
     }
-    memset(c, 0, sizeof(*c));
     c->dynamic_dso = NULL;
     c->v_check = NULL;
     c->bind_engine = NULL;
index c477c7efc5e01c85587eb2e83746d21edbb131c3..a113ebc57c99bb0869b0df0fb86f8a0e27ac8ece 100644 (file)
@@ -66,12 +66,11 @@ ENGINE *ENGINE_new(void)
 {
     ENGINE *ret;
 
-    ret = OPENSSL_malloc(sizeof(*ret));
+    ret = OPENSSL_zalloc(sizeof(*ret));
     if (ret == NULL) {
         ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
-    memset(ret, 0, sizeof(*ret));
     ret->struct_ref = 1;
     engine_ref_debug(ret, 0, 1)
         CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data);
index 405cbb0c1b520a08a3dffdc31024c044c25ba930..7f55c4196b603143f9c8b7978174e06d65f4c28b 100644 (file)
@@ -158,12 +158,11 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
 
         ctx->cipher = cipher;
         if (ctx->cipher->ctx_size) {
-            ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size);
+            ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
             if (!ctx->cipher_data) {
                 EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
                 return 0;
             }
-            memset(ctx->cipher_data, 0, ctx->cipher->ctx_size);
         } else {
             ctx->cipher_data = NULL;
         }
index 210c7fa988439588bc715ef49660a4876c408a08..f317471c6b08afe68b3acc1d3d8dc4c622d8f067 100644 (file)
@@ -198,15 +198,12 @@ EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
 {
     EVP_PKEY_METHOD *pmeth;
 
-    pmeth = OPENSSL_malloc(sizeof(*pmeth));
+    pmeth = OPENSSL_zalloc(sizeof(*pmeth));
     if (!pmeth)
         return NULL;
 
-    memset(pmeth, 0, sizeof(*pmeth));
-
     pmeth->pkey_id = id;
     pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
-
     pmeth->init = 0;
     pmeth->copy = 0;
     pmeth->cleanup = 0;
index 8b9c8c38051b9b95530c1e4a17bb9ed68efd0379..33a76d2d08fe8a816f25dec927082c451fc10f3a 100644 (file)
@@ -312,14 +312,21 @@ void *CRYPTO_malloc(int num, const char *file, int line)
     return ret;
 }
 
+void *CRYPTO_zalloc(int num, const char *file, int line)
+{
+    void *ret = CRYPTO_malloc(num, file, line);
+
+    if (ret != NULL)
+        memset(ret, 0, num);
+    return ret;
+}
+
 char *CRYPTO_strdup(const char *str, const char *file, int line)
 {
     char *ret = CRYPTO_malloc(strlen(str) + 1, file, line);
 
-    if (ret == NULL)
-        return NULL;
-
-    strcpy(ret, str);
+    if (ret != NULL)
+        strcpy(ret, str);
     return ret;
 }
 
index d10088e1f9abf11ab72b78c8a27296ffc5e91d49..b6e19c7f52d047e7645d6b718bdcad124586c136 100644 (file)
@@ -87,11 +87,8 @@ void pitem_free(pitem *item)
 
 pqueue_s *pqueue_new()
 {
-    pqueue_s *pq = OPENSSL_malloc(sizeof(*pq));
-    if (pq == NULL)
-        return NULL;
+    pqueue_s *pq = OPENSSL_zalloc(sizeof(*pq));
 
-    memset(pq, 0, sizeof(*pq));
     return pq;
 }
 
index b28021ba883a5842a19e62e91a11b2ccb93d8631..f62fd731471bab7e1235a18f8ddb0f10eeca9a16 100644 (file)
@@ -287,7 +287,7 @@ int RSA_memory_lock(RSA *r)
     j = 1;
     for (i = 0; i < 6; i++)
         j += bn_get_top(*t[i]);
-    if ((p = OPENSSL_malloc((off + j) * sizeof(BN_ULONG))) == NULL) {
+    if ((p = OPENSSL_malloc((off + j) * sizeof(*p))) == NULL) {
         RSAerr(RSA_F_RSA_MEMORY_LOCK, ERR_R_MALLOC_FAILURE);
         return (0);
     }
index 9a8145b8cea176af8b5b7ac461e64679fe0304ee..8f8587a3ea693d4eac05a864f33c451ece181fdd 100644 (file)
@@ -203,12 +203,11 @@ int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
     if (num < 11)
         goto err;
 
-    em = OPENSSL_malloc(num);
+    em = OPENSSL_zalloc(num);
     if (em == NULL) {
         RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, ERR_R_MALLOC_FAILURE);
         return -1;
     }
-    memset(em, 0, num);
     /*
      * Always do this zero-padding copy (even when num == flen) to avoid
      * leaking that information. The copy still leaks some side-channel
index a630cbc82e092daa4801323651093fdba3fce9ed..fbed8b49fca655160f255cae7967b902453c167a 100644 (file)
@@ -314,23 +314,20 @@ static int sh_init(size_t size, int minsize)
     for (i = sh.bittable_size; i; i >>= 1)
         sh.freelist_size++;
 
-    sh.freelist = OPENSSL_malloc(sh.freelist_size * sizeof (char *));
+    sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof (char *));
     OPENSSL_assert(sh.freelist != NULL);
     if (sh.freelist == NULL)
         goto err;
-    memset(sh.freelist, 0, sh.freelist_size * sizeof (char *));
 
-    sh.bittable = OPENSSL_malloc(sh.bittable_size >> 3);
+    sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
     OPENSSL_assert(sh.bittable != NULL);
     if (sh.bittable == NULL)
         goto err;
-    memset(sh.bittable, 0, sh.bittable_size >> 3);
 
-    sh.bitmalloc = OPENSSL_malloc(sh.bittable_size >> 3);
+    sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
     OPENSSL_assert(sh.bitmalloc != NULL);
     if (sh.bitmalloc == NULL)
         goto err;
-    memset(sh.bitmalloc, 0, sh.bittable_size >> 3);
 
     /* Allocate space for heap, and two extra pages as guards */
 #ifdef _SC_PAGE_SIZE
index 3201da953667432315239ce95d5b2b2b2493f246..55ca19da8a03e02ea493958be9ffc0f09d4ebac9 100644 (file)
@@ -1154,9 +1154,7 @@ int STORE_delete_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
 
 STORE_OBJECT *STORE_OBJECT_new(void)
 {
-    STORE_OBJECT *object = OPENSSL_malloc(sizeof(*object));
-    if (object)
-        memset(object, 0, sizeof(*object));
+    STORE_OBJECT *object = OPENSSL_zalloc(sizeof(*object));
     return object;
 }
 
index b14e28908cc3187bb1ef44565bd69ea92d587af3..1736f7903f4ee3c12c217ef0cbc9dc4b3422e48a 100644 (file)
@@ -244,7 +244,7 @@ static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
                             OPENSSL_ITEM attributes[],
                             OPENSSL_ITEM parameters[])
 {
-    struct mem_ctx_st *context = OPENSSL_malloc(sizeof(*context));
+    struct mem_ctx_st *context = OPENSSL_zalloc(sizeof(*context));
     void *attribute_context = NULL;
     STORE_ATTR_INFO *attrs = NULL;
 
@@ -252,7 +252,6 @@ static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
         STOREerr(STORE_F_MEM_LIST_START, ERR_R_MALLOC_FAILURE);
         return 0;
     }
-    memset(context, 0, sizeof(*context));
 
     attribute_context = STORE_parse_attrs_start(attributes);
     if (!attribute_context) {
index 74878197f9d24e07d15e26fa80a4a43e3c9f1526..c030198841ccfd765a3fc99c9520fbf98b446fe0 100644 (file)
 
 STORE_METHOD *STORE_create_method(char *name)
 {
-    STORE_METHOD *store_method = OPENSSL_malloc(sizeof(*store_method));
+    STORE_METHOD *store_method = OPENSSL_zalloc(sizeof(*store_method));
 
-    if (store_method) {
-        memset(store_method, 0, sizeof(*store_method));
+    if (store_method)
         store_method->name = BUF_strdup(name);
-    }
     return store_method;
 }
 
index f0fc503aff02bf6c5b79336812d379742b681567..9cacec87138c01ad915a98e33b7bc884631378a9 100644 (file)
@@ -169,11 +169,10 @@ TS_RESP_CTX *TS_RESP_CTX_new()
 {
     TS_RESP_CTX *ctx;
 
-    if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) {
+    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
         TSerr(TS_F_TS_RESP_CTX_NEW, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
-    memset(ctx, 0, sizeof(*ctx));
 
     /* Setting default callbacks. */
     ctx->serial_cb = def_serial_cb;
index 7465e048c5bd8defe37649816f22ea55bf1e6cf6..e23ae268f4ca0fc46474b490a1242e8aa826a787 100644 (file)
 
 TS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
 {
-    TS_VERIFY_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
+    TS_VERIFY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
 
-    if (ctx)
-        memset(ctx, 0, sizeof(*ctx));
-    else
+    if (!ctx)
         TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
     return ctx;
 }
index 9ba844eacb0e23c908a3d7071a58be8308a92dde..654d74fee8c940fe06cedfb2ccb4b9029ed19c41 100644 (file)
@@ -582,12 +582,10 @@ const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
 
 UI_METHOD *UI_create_method(char *name)
 {
-    UI_METHOD *ui_method = OPENSSL_malloc(sizeof(*ui_method));
+    UI_METHOD *ui_method = OPENSSL_zalloc(sizeof(*ui_method));
 
-    if (ui_method) {
-        memset(ui_method, 0, sizeof(*ui_method));
+    if (ui_method)
         ui_method->name = BUF_strdup(name);
-    }
     return ui_method;
 }
 
index 1376e4486bceead7ff935ea487876d9701e04473..bc48b8a334d973eedfebfd09df3e21f50f8fe155 100644 (file)
@@ -2259,13 +2259,12 @@ int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
 
 X509_STORE_CTX *X509_STORE_CTX_new(void)
 {
-    X509_STORE_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
+    X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
 
     if (!ctx) {
         X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
-    memset(ctx, 0, sizeof(*ctx));
     return ctx;
 }
 
index eedc2179a6b7a0b3f7e8559b9d5f50563c81eb89..cf8784d5fbbdfaf584949a895a988898cdc212fc 100644 (file)
@@ -162,24 +162,14 @@ X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void)
     X509_VERIFY_PARAM *param;
     X509_VERIFY_PARAM_ID *paramid;
 
-    param = OPENSSL_malloc(sizeof(*param));
+    param = OPENSSL_zalloc(sizeof(*param));
     if (!param)
         return NULL;
-    memset(param, 0, sizeof(*param));
-
-    paramid = OPENSSL_malloc(sizeof(*paramid));
+    param->id = paramid = OPENSSL_zalloc(sizeof(*paramid));
     if (!paramid) {
         OPENSSL_free(param);
         return NULL;
     }
-    memset(paramid, 0, sizeof(*paramid));
-    /* Exotic platforms may have non-zero bit representation of NULL */
-    paramid->hosts = NULL;
-    paramid->peername = NULL;
-    paramid->email = NULL;
-    paramid->ip = NULL;
-
-    param->id = paramid;
     x509_verify_param_zero(param);
     return param;
 }
index 4b0ea15b6dd37ddd4433971b94005cdbdb184684..2a41903a72e8f42ef1f45f75d62a81537b5db908 100644 (file)
@@ -217,25 +217,18 @@ static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
     }
 
     /* If we get this far initialize the tree */
-
     tree = OPENSSL_malloc(sizeof(*tree));
-
     if (!tree)
         return 0;
-
-    tree->flags = 0;
-    tree->levels = OPENSSL_malloc(sizeof(*tree->levels) * n);
-    tree->nlevel = 0;
-    tree->extra_data = NULL;
-    tree->auth_policies = NULL;
-    tree->user_policies = NULL;
-
+    tree->levels = OPENSSL_zalloc(sizeof(*tree->levels) * n);
     if (!tree->levels) {
         OPENSSL_free(tree);
         return 0;
     }
-
-    memset(tree->levels, 0, sizeof(*tree->levels) * n);
+    tree->flags = 0;
+    tree->extra_data = NULL;
+    tree->auth_policies = NULL;
+    tree->user_policies = NULL;
     tree->nlevel = n;
     level = tree->levels;
 
index 0574d6eee4624db9ecc1ec0371fdaa9449fd5711..e70e2979db77975b345f025d769528cacacb6f49 100644 (file)
@@ -24,10 +24,9 @@ static int pkey_gost_init(EVP_PKEY_CTX *ctx)
     struct gost_pmeth_data *data;
     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
 
-    data = OPENSSL_malloc(sizeof(*data));
+    data = OPENSSL_zalloc(sizeof(*data));
     if (!data)
         return 0;
-    memset(data, 0, sizeof(*data));
     if (pkey && EVP_PKEY_get0(pkey)) {
         switch (EVP_PKEY_base_id(pkey)) {
         case NID_id_GostR3410_2001:
@@ -309,11 +308,10 @@ static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
 {
-    struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
+    struct gost_mac_pmeth_data *data = OPENSSL_zalloc(sizeof(*data));
 
     if (!data)
         return 0;
-    memset(data, 0, sizeof(*data));
     EVP_PKEY_CTX_set_data(ctx, data);
     return 1;
 }
index f1ff0e02b94fcb497f7b18b10709ec21b9b8c39b..56afc517b605e07df2190b94c0d23e438df3ed41 100644 (file)
@@ -337,6 +337,7 @@ int CRYPTO_is_mem_check_on(void);
 # define is_MemCheck_on() CRYPTO_is_mem_check_on()
 
 # define OPENSSL_malloc(num)     CRYPTO_malloc((int)num,__FILE__,__LINE__)
+# define OPENSSL_zalloc(num)     CRYPTO_zalloc((int)num,__FILE__,__LINE__)
 # define OPENSSL_strdup(str)     CRYPTO_strdup((str),__FILE__,__LINE__)
 # define OPENSSL_realloc(addr,num) \
         CRYPTO_realloc((char *)addr,(int)num,__FILE__,__LINE__)
@@ -469,6 +470,7 @@ void CRYPTO_get_mem_debug_functions(void (**m)
                                     void (**so) (long), long (**go) (void));
 
 void *CRYPTO_malloc(int num, const char *file, int line);
+void *CRYPTO_zalloc(int num, const char *file, int line);
 char *CRYPTO_strdup(const char *str, const char *file, int line);
 void CRYPTO_free(void *ptr);
 void CRYPTO_clear_free(void *ptr, size_t num);
index aa6d623191c48ce25d7c89cb8e87dcd938c28b26..639b10556a30a257a961c927091dbbf12b8d2829 100644 (file)
@@ -101,13 +101,12 @@ BIO_METHOD *BIO_f_ssl(void)
 
 static int ssl_new(BIO *bi)
 {
-    BIO_SSL *bs = OPENSSL_malloc(sizeof(*bs));
+    BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs));
 
     if (bs == NULL) {
         BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
         return (0);
     }
-    memset(bs, 0, sizeof(*bs));
     bi->init = 0;
     bi->ptr = (char *)bs;
     bi->flags = 0;
index d2f5defe14c61d379782e6b26295f56dbb5d830a..52b7304af2a61de982dbb2bd5f0cc2a90bcfb7dc 100644 (file)
@@ -187,13 +187,12 @@ static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len,
 
     /* Initialize reassembly bitmask if necessary */
     if (reassembly) {
-        bitmask = OPENSSL_malloc(RSMBLY_BITMASK_SIZE(frag_len));
+        bitmask = OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len));
         if (bitmask == NULL) {
             OPENSSL_free(buf);
             OPENSSL_free(frag);
             return NULL;
         }
-        memset(bitmask, 0, RSMBLY_BITMASK_SIZE(frag_len));
     }
 
     frag->reassembly = bitmask;
index fc1887ab35692c5574cac20486466bf67a15efcd..d3b582a98cc810ec39cbcb723e7616ace75b8892 100644 (file)
@@ -135,11 +135,10 @@ int dtls1_new(SSL *s)
     
     if (!ssl3_new(s))
         return (0);
-    if ((d1 = OPENSSL_malloc(sizeof(*d1))) == NULL) {
+    if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) {
         ssl3_free(s);
         return (0);
     }
-    memset(d1, 0, sizeof(*d1));
 
     d1->buffered_messages = pqueue_new();
     d1->sent_messages = pqueue_new();
index 47d28e73ade27feb4476b255d28bab2e818abed9..bb090efc48d989e46857d64228fea4b37852dcf3 100644 (file)
@@ -3836,9 +3836,8 @@ int ssl3_new(SSL *s)
 {
     SSL3_STATE *s3;
 
-    if ((s3 = OPENSSL_malloc(sizeof(*s3))) == NULL)
+    if ((s3 = OPENSSL_zalloc(sizeof(*s3))) == NULL)
         goto err;
-    memset(s3, 0, sizeof(*s3));
     s->s3 = s3;
     
 #ifndef OPENSSL_NO_SRP
index 11839612f25a58d0379982dadd5c0b133f045659..c3e2c2ed0ae61d78547032c80bc6af2058a5ac9f 100644 (file)
@@ -167,13 +167,12 @@ int SSL_get_ex_data_X509_STORE_CTX_idx(void)
 
 CERT *ssl_cert_new(void)
 {
-    CERT *ret = OPENSSL_malloc(sizeof(*ret));
+    CERT *ret = OPENSSL_zalloc(sizeof(*ret));
 
     if (ret == NULL) {
         SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE);
         return (NULL);
     }
-    memset(ret, 0, sizeof(*ret));
 
     ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]);
     ret->references = 1;
@@ -185,7 +184,7 @@ CERT *ssl_cert_new(void)
 
 CERT *ssl_cert_dup(CERT *cert)
 {
-    CERT *ret = OPENSSL_malloc(sizeof(*ret));
+    CERT *ret = OPENSSL_zalloc(sizeof(*ret));
     int i;
 
     if (ret == NULL) {
@@ -193,8 +192,6 @@ CERT *ssl_cert_dup(CERT *cert)
         return (NULL);
     }
 
-    memset(ret, 0, sizeof(*ret));
-
     ret->key = &ret->pkeys[cert->key - cert->pkeys];
 
 #ifndef OPENSSL_NO_RSA
index c048fc2a8c18d5b687faa2fd6af7e7f34b32a0be..2dd2379819a11cfbf8ae9d383306a84c2133242c 100644 (file)
@@ -1038,12 +1038,11 @@ static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
         curr = curr->next;
     }
 
-    number_uses = OPENSSL_malloc(sizeof(int) * (max_strength_bits + 1));
+    number_uses = OPENSSL_zalloc(sizeof(int) * (max_strength_bits + 1));
     if (!number_uses) {
         SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT, ERR_R_MALLOC_FAILURE);
         return (0);
     }
-    memset(number_uses, 0, sizeof(int) * (max_strength_bits + 1));
 
     /*
      * Now find the strength_bits values actually used
index fd1561e52d82acd0fc62c9d1e9e1953f9495142b..b1d4771e9a07e49cecf1e7afd47129cf35b3521d 100644 (file)
@@ -277,10 +277,9 @@ SSL *SSL_new(SSL_CTX *ctx)
         return (NULL);
     }
 
-    s = OPENSSL_malloc(sizeof(*s));
+    s = OPENSSL_zalloc(sizeof(*s));
     if (s == NULL)
         goto err;
-    memset(s, 0, sizeof(*s));
 
     RECORD_LAYER_init(&s->rlayer, s);
 
@@ -1684,14 +1683,11 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
         SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
         goto err;
     }
-    ret = OPENSSL_malloc(sizeof(*ret));
+    ret = OPENSSL_zalloc(sizeof(*ret));
     if (ret == NULL)
         goto err;
 
-    memset(ret, 0, sizeof(*ret));
-
     ret->method = meth;
-
     ret->cert_store = NULL;
     ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
     ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
@@ -1706,8 +1702,6 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
     ret->get_session_cb = 0;
     ret->generate_session_id = 0;
 
-    memset(&ret->stats, 0, sizeof(ret->stats));
-
     ret->references = 1;
     ret->quiet_shutdown = 0;
     ret->info_callback = NULL;
index 69e6d7fea52e3b57cd06cc6b4204d48d8541d81a..3e980bf5cbe77956cd2f54cda6bc47765ad1e093 100644 (file)
@@ -193,12 +193,11 @@ SSL_SESSION *SSL_SESSION_new(void)
 {
     SSL_SESSION *ss;
 
-    ss = OPENSSL_malloc(sizeof(*ss));
+    ss = OPENSSL_zalloc(sizeof(*ss));
     if (ss == NULL) {
         SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
         return (0);
     }
-    memset(ss, 0, sizeof(*ss));
 
     ss->verify_result = 1;      /* avoid 0 (= X509_V_OK) just in case */
     ss->references = 1;
index 377e2c53673b657d60913ab2818c52b7a2575c3c..4b6d0444bf73b3262cb1ce1973a816e21824d8ee 100644 (file)
@@ -440,10 +440,8 @@ int test_builtin(BIO *out)
             goto builtin_err;
         }
         buf_len = 2 * bn_len;
-        if ((raw_buf = OPENSSL_malloc(buf_len)) == NULL)
+        if ((raw_buf = OPENSSL_zalloc(buf_len)) == NULL)
             goto builtin_err;
-        /* Pad the bignums with leading zeroes. */
-        memset(raw_buf, 0, buf_len);
         BN_bn2bin(ecdsa_sig->r, raw_buf + bn_len - r_len);
         BN_bn2bin(ecdsa_sig->s, raw_buf + buf_len - s_len);
 
index 914419109d136d3df528d60c4927e0f921666ae8..adf13680204b7f37e75df998f363f485cfabebc4 100644 (file)
@@ -2116,14 +2116,11 @@ int doit(SSL *s_ssl, SSL *c_ssl, long count)
 
     bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
 
-    if ((cbuf = OPENSSL_malloc(bufsiz)) == NULL)
+    if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
         goto err;
-    if ((sbuf = OPENSSL_malloc(bufsiz)) == NULL)
+    if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
         goto err;
 
-    memset(cbuf, 0, bufsiz);
-    memset(sbuf, 0, bufsiz);
-
     c_to_s = BIO_new(BIO_s_mem());
     s_to_c = BIO_new(BIO_s_mem());
     if ((s_to_c == NULL) || (c_to_s == NULL)) {
index 0cf3cb7c22e0ead4c08b7f6de3a4465c84ba8c72..6b27c9e9ed4a71014ab508ca1c9e9353198995be 100755 (executable)
@@ -4592,6 +4592,7 @@ X509_up_ref                             4950      EXIST::FUNCTION:
 X509_REQ_get_version                    4951   EXIST::FUNCTION:
 X509_REQ_get_subject_name               4952   EXIST::FUNCTION:
 X509_CRL_up_ref                         4953   EXIST::FUNCTION:
+CRYPTO_zalloc                           4954   EXIST::FUNCTION:
 X509_get_extension_flags                4954   EXIST::FUNCTION:
 X509_get_extended_key_usage             4955   EXIST::FUNCTION:
 X509_get_key_usage                      4956   EXIST::FUNCTION: