From: deraadt Date: Mon, 21 Apr 2014 11:12:49 +0000 (+0000) Subject: Bring malloc/calloc/realloc sequences to modern standard X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=3c6fe066dc73ba56622777c440a3221989a53a7c;p=openbsd Bring malloc/calloc/realloc sequences to modern standard ok guenther --- diff --git a/lib/libcrypto/bio/bf_buff.c b/lib/libcrypto/bio/bf_buff.c index 9dfd90c7b44..99be1a629e5 100644 --- a/lib/libcrypto/bio/bf_buff.c +++ b/lib/libcrypto/bio/bf_buff.c @@ -95,15 +95,15 @@ buffer_new(BIO *bi) { BIO_F_BUFFER_CTX *ctx; - ctx = (BIO_F_BUFFER_CTX *)malloc(sizeof(BIO_F_BUFFER_CTX)); + ctx = malloc(sizeof(BIO_F_BUFFER_CTX)); if (ctx == NULL) return (0); - ctx->ibuf = (char *)malloc(DEFAULT_BUFFER_SIZE); + ctx->ibuf = malloc(DEFAULT_BUFFER_SIZE); if (ctx->ibuf == NULL) { free(ctx); return (0); } - ctx->obuf = (char *)malloc(DEFAULT_BUFFER_SIZE); + ctx->obuf = malloc(DEFAULT_BUFFER_SIZE); if (ctx->obuf == NULL) { free(ctx->ibuf); free(ctx); @@ -370,12 +370,12 @@ 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 *)malloc((int)num); + p1 = malloc((int)num); if (p1 == NULL) goto malloc_error; } if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) { - p2 = (char *)malloc((int)num); + p2 = malloc((int)num); if (p2 == NULL) { if (p1 != ctx->ibuf) free(p1); diff --git a/lib/libcrypto/bio/bf_lbuf.c b/lib/libcrypto/bio/bf_lbuf.c index 7ed26ccec76..f0bd0d709ea 100644 --- a/lib/libcrypto/bio/bf_lbuf.c +++ b/lib/libcrypto/bio/bf_lbuf.c @@ -106,10 +106,10 @@ linebuffer_new(BIO *bi) { BIO_LINEBUFFER_CTX *ctx; - ctx = (BIO_LINEBUFFER_CTX *)malloc(sizeof(BIO_LINEBUFFER_CTX)); + ctx = malloc(sizeof(BIO_LINEBUFFER_CTX)); if (ctx == NULL) return (0); - ctx->obuf = (char *)malloc(DEFAULT_LINEBUFFER_SIZE); + ctx->obuf = malloc(DEFAULT_LINEBUFFER_SIZE); if (ctx->obuf == NULL) { free(ctx); return (0); @@ -281,7 +281,7 @@ linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr) obs = (int)num; p = ctx->obuf; if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) { - p = (char *)malloc((int)num); + p = malloc((int)num); if (p == NULL) goto malloc_error; } diff --git a/lib/libcrypto/bio/bf_nbio.c b/lib/libcrypto/bio/bf_nbio.c index 48c9781700a..048e6892ccc 100644 --- a/lib/libcrypto/bio/bf_nbio.c +++ b/lib/libcrypto/bio/bf_nbio.c @@ -104,7 +104,7 @@ nbiof_new(BIO *bi) { NBIO_TEST *nt; - if (!(nt = (NBIO_TEST *)malloc(sizeof(NBIO_TEST)))) + if (!(nt = malloc(sizeof(NBIO_TEST)))) return (0); nt->lrn = -1; nt->lwn = -1; diff --git a/lib/libcrypto/bio/bio_lib.c b/lib/libcrypto/bio/bio_lib.c index 47ab0c8a455..3deca5b7443 100644 --- a/lib/libcrypto/bio/bio_lib.c +++ b/lib/libcrypto/bio/bio_lib.c @@ -68,7 +68,7 @@ BIO_new(BIO_METHOD *method) { BIO *ret = NULL; - ret = (BIO *)malloc(sizeof(BIO)); + ret = malloc(sizeof(BIO)); if (ret == NULL) { BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE); return (NULL); diff --git a/lib/libcrypto/bio/bss_acpt.c b/lib/libcrypto/bio/bss_acpt.c index 4ab5856ed6c..916af3cb20e 100644 --- a/lib/libcrypto/bio/bss_acpt.c +++ b/lib/libcrypto/bio/bss_acpt.c @@ -137,10 +137,8 @@ BIO_ACCEPT_new(void) { BIO_ACCEPT *ret; - if ((ret = (BIO_ACCEPT *)malloc(sizeof(BIO_ACCEPT))) == NULL) + if ((ret = calloc(1, sizeof(BIO_ACCEPT))) == NULL) return (NULL); - - memset(ret, 0, sizeof(BIO_ACCEPT)); ret->accept_sock = -1; ret->bind_mode = BIO_BIND_NORMAL; return (ret); diff --git a/lib/libcrypto/bio/bss_conn.c b/lib/libcrypto/bio/bss_conn.c index 45815fd6968..b64d7d7761c 100644 --- a/lib/libcrypto/bio/bss_conn.c +++ b/lib/libcrypto/bio/bss_conn.c @@ -295,7 +295,7 @@ BIO_CONNECT_new(void) { BIO_CONNECT *ret; - if ((ret = (BIO_CONNECT *)malloc(sizeof(BIO_CONNECT))) == NULL) + if ((ret = malloc(sizeof(BIO_CONNECT))) == NULL) return (NULL); ret->state = BIO_CONN_S_BEFORE; ret->param_hostname = NULL; diff --git a/lib/libcrypto/bio/bss_dgram.c b/lib/libcrypto/bio/bss_dgram.c index 79a6c2aa672..72cb19d135f 100644 --- a/lib/libcrypto/bio/bss_dgram.c +++ b/lib/libcrypto/bio/bss_dgram.c @@ -211,10 +211,9 @@ dgram_new(BIO *bi) bi->init = 0; bi->num = 0; - data = malloc(sizeof(bio_dgram_data)); + data = calloc(1, sizeof(bio_dgram_data)); if (data == NULL) return 0; - memset(data, 0x00, sizeof(bio_dgram_data)); bi->ptr = data; bi->flags = 0; @@ -773,8 +772,7 @@ BIO_new_dgram_sctp(int fd, int close_flag) * SCTP-AUTH has to be activated for the listening socket * already, otherwise the connected socket won't use it. */ sockopt_len = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t)); - authchunks = malloc(sockopt_len); - memset(authchunks, 0, sizeof(sockopt_len)); + authchunks = calloc(1, sockopt_len); ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks, &sockopt_len); OPENSSL_assert(ret >= 0); @@ -834,10 +832,9 @@ dgram_sctp_new(BIO *bi) bi->init = 0; bi->num = 0; - data = malloc(sizeof(bio_dgram_sctp_data)); + data = calloc(1, sizeof(bio_dgram_sctp_data)); if (data == NULL) return 0; - memset(data, 0x00, sizeof(bio_dgram_sctp_data)); #ifdef SCTP_PR_SCTP_NONE data->prinfo.pr_policy = SCTP_PR_SCTP_NONE; #endif @@ -1055,8 +1052,7 @@ dgram_sctp_read(BIO *b, char *out, int outl) struct sctp_authchunks *authchunks; optlen = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t)); - authchunks = malloc(optlen); - memset(authchunks, 0, sizeof(optlen)); + authchunks = calloc(1, optlen); ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS, authchunks, &optlen); OPENSSL_assert(ii >= 0); @@ -1122,8 +1118,7 @@ dgram_sctp_write(BIO *b, const char *in, int inl) if (data->save_shutdown && !BIO_dgram_sctp_wait_for_dry(b)) { data->saved_message.bio = b; data->saved_message.length = inl; - data->saved_message.data = malloc(inl); - memcpy(data->saved_message.data, in, inl); + data->saved_message.data = calloc(1, inl); return inl; } @@ -1250,8 +1245,7 @@ dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr) /* Add new key */ sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t); - authkey = malloc(sockopt_len); - memset(authkey, 0x00, sockopt_len); + authkey = calloc(1, sockopt_len); authkey->sca_keynumber = authkeyid.scact_keynumber + 1; #ifndef __FreeBSD__ /* This field is missing in FreeBSD 8.2 and earlier, diff --git a/lib/libcrypto/bio/bss_log.c b/lib/libcrypto/bio/bss_log.c index 342176f82ec..6aa2d8b0a49 100644 --- a/lib/libcrypto/bio/bss_log.c +++ b/lib/libcrypto/bio/bss_log.c @@ -157,7 +157,7 @@ slg_write(BIO *b, const char *in, int inl) { 0, "", LOG_ERR } /* The default */ }; - if ((buf = (char *)malloc(inl + 1)) == NULL) { + if ((buf = malloc(inl + 1)) == NULL) { return (0); } strlcpy(buf, in, inl + 1); diff --git a/lib/libssl/src/crypto/bio/bf_buff.c b/lib/libssl/src/crypto/bio/bf_buff.c index 9dfd90c7b44..99be1a629e5 100644 --- a/lib/libssl/src/crypto/bio/bf_buff.c +++ b/lib/libssl/src/crypto/bio/bf_buff.c @@ -95,15 +95,15 @@ buffer_new(BIO *bi) { BIO_F_BUFFER_CTX *ctx; - ctx = (BIO_F_BUFFER_CTX *)malloc(sizeof(BIO_F_BUFFER_CTX)); + ctx = malloc(sizeof(BIO_F_BUFFER_CTX)); if (ctx == NULL) return (0); - ctx->ibuf = (char *)malloc(DEFAULT_BUFFER_SIZE); + ctx->ibuf = malloc(DEFAULT_BUFFER_SIZE); if (ctx->ibuf == NULL) { free(ctx); return (0); } - ctx->obuf = (char *)malloc(DEFAULT_BUFFER_SIZE); + ctx->obuf = malloc(DEFAULT_BUFFER_SIZE); if (ctx->obuf == NULL) { free(ctx->ibuf); free(ctx); @@ -370,12 +370,12 @@ 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 *)malloc((int)num); + p1 = malloc((int)num); if (p1 == NULL) goto malloc_error; } if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) { - p2 = (char *)malloc((int)num); + p2 = malloc((int)num); if (p2 == NULL) { if (p1 != ctx->ibuf) free(p1); diff --git a/lib/libssl/src/crypto/bio/bf_lbuf.c b/lib/libssl/src/crypto/bio/bf_lbuf.c index 7ed26ccec76..f0bd0d709ea 100644 --- a/lib/libssl/src/crypto/bio/bf_lbuf.c +++ b/lib/libssl/src/crypto/bio/bf_lbuf.c @@ -106,10 +106,10 @@ linebuffer_new(BIO *bi) { BIO_LINEBUFFER_CTX *ctx; - ctx = (BIO_LINEBUFFER_CTX *)malloc(sizeof(BIO_LINEBUFFER_CTX)); + ctx = malloc(sizeof(BIO_LINEBUFFER_CTX)); if (ctx == NULL) return (0); - ctx->obuf = (char *)malloc(DEFAULT_LINEBUFFER_SIZE); + ctx->obuf = malloc(DEFAULT_LINEBUFFER_SIZE); if (ctx->obuf == NULL) { free(ctx); return (0); @@ -281,7 +281,7 @@ linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr) obs = (int)num; p = ctx->obuf; if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) { - p = (char *)malloc((int)num); + p = malloc((int)num); if (p == NULL) goto malloc_error; } diff --git a/lib/libssl/src/crypto/bio/bf_nbio.c b/lib/libssl/src/crypto/bio/bf_nbio.c index 48c9781700a..048e6892ccc 100644 --- a/lib/libssl/src/crypto/bio/bf_nbio.c +++ b/lib/libssl/src/crypto/bio/bf_nbio.c @@ -104,7 +104,7 @@ nbiof_new(BIO *bi) { NBIO_TEST *nt; - if (!(nt = (NBIO_TEST *)malloc(sizeof(NBIO_TEST)))) + if (!(nt = malloc(sizeof(NBIO_TEST)))) return (0); nt->lrn = -1; nt->lwn = -1; diff --git a/lib/libssl/src/crypto/bio/bio_lib.c b/lib/libssl/src/crypto/bio/bio_lib.c index 47ab0c8a455..3deca5b7443 100644 --- a/lib/libssl/src/crypto/bio/bio_lib.c +++ b/lib/libssl/src/crypto/bio/bio_lib.c @@ -68,7 +68,7 @@ BIO_new(BIO_METHOD *method) { BIO *ret = NULL; - ret = (BIO *)malloc(sizeof(BIO)); + ret = malloc(sizeof(BIO)); if (ret == NULL) { BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE); return (NULL); diff --git a/lib/libssl/src/crypto/bio/bss_acpt.c b/lib/libssl/src/crypto/bio/bss_acpt.c index 4ab5856ed6c..916af3cb20e 100644 --- a/lib/libssl/src/crypto/bio/bss_acpt.c +++ b/lib/libssl/src/crypto/bio/bss_acpt.c @@ -137,10 +137,8 @@ BIO_ACCEPT_new(void) { BIO_ACCEPT *ret; - if ((ret = (BIO_ACCEPT *)malloc(sizeof(BIO_ACCEPT))) == NULL) + if ((ret = calloc(1, sizeof(BIO_ACCEPT))) == NULL) return (NULL); - - memset(ret, 0, sizeof(BIO_ACCEPT)); ret->accept_sock = -1; ret->bind_mode = BIO_BIND_NORMAL; return (ret); diff --git a/lib/libssl/src/crypto/bio/bss_conn.c b/lib/libssl/src/crypto/bio/bss_conn.c index 45815fd6968..b64d7d7761c 100644 --- a/lib/libssl/src/crypto/bio/bss_conn.c +++ b/lib/libssl/src/crypto/bio/bss_conn.c @@ -295,7 +295,7 @@ BIO_CONNECT_new(void) { BIO_CONNECT *ret; - if ((ret = (BIO_CONNECT *)malloc(sizeof(BIO_CONNECT))) == NULL) + if ((ret = malloc(sizeof(BIO_CONNECT))) == NULL) return (NULL); ret->state = BIO_CONN_S_BEFORE; ret->param_hostname = NULL; diff --git a/lib/libssl/src/crypto/bio/bss_dgram.c b/lib/libssl/src/crypto/bio/bss_dgram.c index 79a6c2aa672..72cb19d135f 100644 --- a/lib/libssl/src/crypto/bio/bss_dgram.c +++ b/lib/libssl/src/crypto/bio/bss_dgram.c @@ -211,10 +211,9 @@ dgram_new(BIO *bi) bi->init = 0; bi->num = 0; - data = malloc(sizeof(bio_dgram_data)); + data = calloc(1, sizeof(bio_dgram_data)); if (data == NULL) return 0; - memset(data, 0x00, sizeof(bio_dgram_data)); bi->ptr = data; bi->flags = 0; @@ -773,8 +772,7 @@ BIO_new_dgram_sctp(int fd, int close_flag) * SCTP-AUTH has to be activated for the listening socket * already, otherwise the connected socket won't use it. */ sockopt_len = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t)); - authchunks = malloc(sockopt_len); - memset(authchunks, 0, sizeof(sockopt_len)); + authchunks = calloc(1, sockopt_len); ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks, &sockopt_len); OPENSSL_assert(ret >= 0); @@ -834,10 +832,9 @@ dgram_sctp_new(BIO *bi) bi->init = 0; bi->num = 0; - data = malloc(sizeof(bio_dgram_sctp_data)); + data = calloc(1, sizeof(bio_dgram_sctp_data)); if (data == NULL) return 0; - memset(data, 0x00, sizeof(bio_dgram_sctp_data)); #ifdef SCTP_PR_SCTP_NONE data->prinfo.pr_policy = SCTP_PR_SCTP_NONE; #endif @@ -1055,8 +1052,7 @@ dgram_sctp_read(BIO *b, char *out, int outl) struct sctp_authchunks *authchunks; optlen = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t)); - authchunks = malloc(optlen); - memset(authchunks, 0, sizeof(optlen)); + authchunks = calloc(1, optlen); ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS, authchunks, &optlen); OPENSSL_assert(ii >= 0); @@ -1122,8 +1118,7 @@ dgram_sctp_write(BIO *b, const char *in, int inl) if (data->save_shutdown && !BIO_dgram_sctp_wait_for_dry(b)) { data->saved_message.bio = b; data->saved_message.length = inl; - data->saved_message.data = malloc(inl); - memcpy(data->saved_message.data, in, inl); + data->saved_message.data = calloc(1, inl); return inl; } @@ -1250,8 +1245,7 @@ dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr) /* Add new key */ sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t); - authkey = malloc(sockopt_len); - memset(authkey, 0x00, sockopt_len); + authkey = calloc(1, sockopt_len); authkey->sca_keynumber = authkeyid.scact_keynumber + 1; #ifndef __FreeBSD__ /* This field is missing in FreeBSD 8.2 and earlier, diff --git a/lib/libssl/src/crypto/bio/bss_log.c b/lib/libssl/src/crypto/bio/bss_log.c index 342176f82ec..6aa2d8b0a49 100644 --- a/lib/libssl/src/crypto/bio/bss_log.c +++ b/lib/libssl/src/crypto/bio/bss_log.c @@ -157,7 +157,7 @@ slg_write(BIO *b, const char *in, int inl) { 0, "", LOG_ERR } /* The default */ }; - if ((buf = (char *)malloc(inl + 1)) == NULL) { + if ((buf = malloc(inl + 1)) == NULL) { return (0); } strlcpy(buf, in, inl + 1);