memset, memcpy, sizeof consistency fixes

Just as with the OPENSSL_malloc calls, consistently use sizeof(*ptr)
for memset and memcpy.  Remove needless casts for those functions.
For memset, replace alternative forms of zero with 0.

Reviewed-by: Richard Levitte <levitte@openssl.org>
diff --git a/ssl/bio_ssl.c b/ssl/bio_ssl.c
index 18e7074..aa6d623 100644
--- a/ssl/bio_ssl.c
+++ b/ssl/bio_ssl.c
@@ -107,7 +107,7 @@
         BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
         return (0);
     }
-    memset(bs, 0, sizeof(BIO_SSL));
+    memset(bs, 0, sizeof(*bs));
     bi->init = 0;
     bi->ptr = (char *)bs;
     bi->flags = 0;
diff --git a/ssl/d1_both.c b/ssl/d1_both.c
index 65a3a18..05b8f9e 100644
--- a/ssl/d1_both.c
+++ b/ssl/d1_both.c
@@ -467,7 +467,7 @@
     }
 
     msg_hdr = &s->d1->r_msg_hdr;
-    memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
+    memset(msg_hdr, 0, sizeof(*msg_hdr));
 
  again:
     i = dtls1_get_message_fragment(s, st1, stn, max, ok);
@@ -497,7 +497,7 @@
         s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
                         p, msg_len, s, s->msg_callback_arg);
 
-    memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
+    memset(msg_hdr, 0, sizeof(*msg_hdr));
 
     /* Don't change sequence numbers while listening */
     if (!s->d1->listen)
@@ -1289,7 +1289,7 @@
 void
 dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
 {
-    memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
+    memset(msg_hdr, 0, sizeof(*msg_hdr));
     msg_hdr->type = *(data++);
     n2l3(data, msg_hdr->msg_len);
 
@@ -1300,7 +1300,7 @@
 
 void dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr)
 {
-    memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
+    memset(ccs_hdr, 0, sizeof(*ccs_hdr));
 
     ccs_hdr->type = *(data++);
 }
diff --git a/ssl/d1_lib.c b/ssl/d1_lib.c
index 3441fc5..c0ed8fb 100644
--- a/ssl/d1_lib.c
+++ b/ssl/d1_lib.c
@@ -215,7 +215,7 @@
 
         dtls1_clear_queues(s);
 
-        memset(s->d1, 0, sizeof(*(s->d1)));
+        memset(s->d1, 0, sizeof(*s->d1));
 
         if (s->server) {
             s->d1->cookie_len = sizeof(s->d1->cookie);
@@ -324,7 +324,7 @@
 #ifndef OPENSSL_NO_SCTP
     /* Disable timer for SCTP */
     if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
-        memset(&(s->d1->next_timeout), 0, sizeof(struct timeval));
+        memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
         return;
     }
 #endif
@@ -359,7 +359,7 @@
     if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
         (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
          s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
-        memset(timeleft, 0, sizeof(struct timeval));
+        memset(timeleft, 0, sizeof(*timeleft));
         return timeleft;
     }
 
@@ -377,7 +377,7 @@
      * because of small devergences with socket timeouts.
      */
     if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
-        memset(timeleft, 0, sizeof(struct timeval));
+        memset(timeleft, 0, sizeof(*timeleft));
     }
 
     return timeleft;
@@ -412,8 +412,8 @@
 void dtls1_stop_timer(SSL *s)
 {
     /* Reset everything */
-    memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st));
-    memset(&(s->d1->next_timeout), 0, sizeof(struct timeval));
+    memset(&s->d1->timeout, 0, sizeof(s->d1->timeout));
+    memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
     s->d1->timeout_duration = 1;
     BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
              &(s->d1->next_timeout));
diff --git a/ssl/d1_msg.c b/ssl/d1_msg.c
index f71b1cc..13bda46 100644
--- a/ssl/d1_msg.c
+++ b/ssl/d1_msg.c
@@ -161,7 +161,7 @@
 
     s->s3->alert_dispatch = 0;
 
-    memset(buf, 0x00, sizeof(buf));
+    memset(buf, 0, sizeof(buf));
     *ptr++ = s->s3->send_alert[0];
     *ptr++ = s->s3->send_alert[1];
 
diff --git a/ssl/kssl.c b/ssl/kssl.c
index ce43529..d781042 100644
--- a/ssl/kssl.c
+++ b/ssl/kssl.c
@@ -1048,7 +1048,7 @@
     krb5_data krb5_app_req;
 
     kssl_err_set(kssl_err, 0, "");
-    memset((char *)&krb5creds, 0, sizeof(krb5creds));
+    memset(&krb5creds, 0, sizeof(krb5creds));
 
     if (!kssl_ctx) {
         kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT, "No kssl_ctx defined.\n");
@@ -1797,7 +1797,7 @@
     krb5_creds krb5creds, *krb5credsp = NULL;
     int rc = 0;
 
-    memset((char *)&krb5creds, 0, sizeof(krb5creds));
+    memset(&krb5creds, 0, sizeof(krb5creds));
 
     if (!kssl_ctx)
         return (0);
@@ -2072,7 +2072,7 @@
     }
 # endif
     enc = kssl_map_enc(enctype);
-    memset(iv, 0, sizeof iv);   /* per RFC 1510 */
+    memset(iv, 0, sizeof(iv));   /* per RFC 1510 */
 
     if (enc == NULL) {
         /*
@@ -2126,7 +2126,7 @@
         goto err;
     }
 
-    memset(&tm_time, 0, sizeof(struct tm));
+    memset(&tm_time, 0, sizeof(tm_tmime));
     if (k_gmtime(auth->ctime, &tm_time) &&
         ((tr = mktime(&tm_time)) != (time_t)(-1))) {
         now = time(&now);
diff --git a/ssl/record/rec_layer_d1.c b/ssl/record/rec_layer_d1.c
index 2635894..45324ec 100644
--- a/ssl/record/rec_layer_d1.c
+++ b/ssl/record/rec_layer_d1.c
@@ -286,8 +286,8 @@
 
     s->rlayer.packet = NULL;
     s->rlayer.packet_length = 0;
-    memset(&s->rlayer.rbuf, 0, sizeof(SSL3_BUFFER));
-    memset(&s->rlayer.rrec, 0, sizeof(SSL3_RECORD));
+    memset(&s->rlayer.rbuf, 0, sizeof(s->rlayer.rbuf));
+    memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec));
 
     if (!ssl3_setup_buffers(s)) {
         SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
@@ -1298,9 +1298,10 @@
     if (rw & SSL3_CC_READ) {
         seq = s->rlayer.read_sequence;
         s->rlayer.d->r_epoch++;
-        memcpy(&(s->rlayer.d->bitmap), &(s->rlayer.d->next_bitmap),
-            sizeof(DTLS1_BITMAP));
-        memset(&(s->rlayer.d->next_bitmap), 0x00, sizeof(DTLS1_BITMAP));
+        memcpy(&s->rlayer.d->bitmap, &s->rlayer.d->next_bitmap,
+               sizeof(s->rlayer.d->bitmap));
+        memset(&s->rlayer.d->next_bitmap, 0,
+               sizeof(s->rlayer.d->next_bitmap));
     } else {
         seq = s->rlayer.write_sequence;
         memcpy(s->rlayer.d->last_write_sequence, seq,
@@ -1308,5 +1309,5 @@
         s->rlayer.d->w_epoch++;
     }
 
-    memset(seq, 0x00, seq_bytes);
+    memset(seq, 0, seq_bytes);
 }
diff --git a/ssl/record/rec_layer_s3.c b/ssl/record/rec_layer_s3.c
index 0ed82f7..eccb517 100644
--- a/ssl/record/rec_layer_s3.c
+++ b/ssl/record/rec_layer_s3.c
@@ -155,7 +155,7 @@
     rlen = SSL3_BUFFER_get_len(&rl->rbuf);
     wp = SSL3_BUFFER_get_buf(&rl->wbuf);
     wlen = SSL3_BUFFER_get_len(&rl->wbuf);
-    memset(rl, 0, sizeof (RECORD_LAYER));
+    memset(rl, 0, sizeof(*rl));
     SSL3_BUFFER_set_buf(&rl->rbuf, rp);
     SSL3_BUFFER_set_len(&rl->rbuf, rlen);
     SSL3_BUFFER_set_buf(&rl->wbuf, wp);
diff --git a/ssl/s3_clnt.c b/ssl/s3_clnt.c
index f936fa3..ea4503f 100644
--- a/ssl/s3_clnt.c
+++ b/ssl/s3_clnt.c
@@ -2518,7 +2518,7 @@
              *      EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
              */
 
-            memset(iv, 0, sizeof iv); /* per RFC 1510 */
+            memset(iv, 0, sizeof(iv)); /* per RFC 1510 */
             EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
             EVP_EncryptUpdate(&ciph_ctx, epms, &outl, pms, pmslen);
             EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
@@ -2788,7 +2788,7 @@
                 /* Encoded point will be copied here */
                 p += 1;
                 /* copy the point */
-                memcpy((unsigned char *)p, encodedPoint, n);
+                memcpy(p, encodedPoint, n);
                 /* increment n to account for length field */
                 n += 1;
             }
diff --git a/ssl/s3_enc.c b/ssl/s3_enc.c
index d968a1c..ea9042b 100644
--- a/ssl/s3_enc.c
+++ b/ssl/s3_enc.c
@@ -519,12 +519,13 @@
     /* Allocate handshake_dgst array */
     ssl3_free_digest_list(s);
     s->s3->handshake_dgst =
-        OPENSSL_malloc(SSL_MAX_DIGEST * sizeof(EVP_MD_CTX *));
+        OPENSSL_malloc(sizeof(*s->s3->handshake_dgst) * SSL_MAX_DIGEST);
     if (s->s3->handshake_dgst == NULL) {
         SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_MALLOC_FAILURE);
         return 0;
     }
-    memset(s->s3->handshake_dgst, 0, SSL_MAX_DIGEST * sizeof(EVP_MD_CTX *));
+    memset(s->s3->handshake_dgst, 0,
+           sizeof(*s->s3->handshake_dgst) * SSL_MAX_DIGEST);
     hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
     if (hdatalen <= 0) {
         SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, SSL_R_BAD_HANDSHAKE_LENGTH);
diff --git a/ssl/s3_srvr.c b/ssl/s3_srvr.c
index e6884f3..ec94882 100644
--- a/ssl/s3_srvr.c
+++ b/ssl/s3_srvr.c
@@ -1935,8 +1935,7 @@
             p += 1;
             *p = encodedlen;
             p += 1;
-            memcpy((unsigned char *)p,
-                   (unsigned char *)encodedPoint, encodedlen);
+            memcpy(p, encodedPoint, encodedlen);
             OPENSSL_free(encodedPoint);
             encodedPoint = NULL;
             p += encodedlen;
@@ -2509,7 +2508,7 @@
         if (enc == NULL)
             goto err;
 
-        memset(iv, 0, sizeof iv); /* per RFC 1510 */
+        memset(iv, 0, sizeof(iv)); /* per RFC 1510 */
 
         if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
             SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
diff --git a/ssl/ssl_cert.c b/ssl/ssl_cert.c
index c7a2aa9..38e7b82 100644
--- a/ssl/ssl_cert.c
+++ b/ssl/ssl_cert.c
@@ -188,7 +188,7 @@
         SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE);
         return (NULL);
     }
-    memset(ret, 0, sizeof(CERT));
+    memset(ret, 0, sizeof(*ret));
 
     ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]);
     ret->references = 1;
@@ -209,14 +209,9 @@
         return (NULL);
     }
 
-    memset(ret, 0, sizeof(CERT));
+    memset(ret, 0, sizeof(*ret));
 
-    ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
-    /*
-     * or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that
-     * more readable
-     */
-
+    ret->key = &ret->pkeys[cert->key - cert->pkeys];
     ret->valid = cert->valid;
     ret->mask_k = cert->mask_k;
     ret->mask_a = cert->mask_a;
diff --git a/ssl/ssl_ciph.c b/ssl/ssl_ciph.c
index 0ddb56b..a81ab85 100644
--- a/ssl/ssl_ciph.c
+++ b/ssl/ssl_ciph.c
@@ -1063,12 +1063,12 @@
         curr = curr->next;
     }
 
-    number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
+    number_uses = OPENSSL_malloc(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, (max_strength_bits + 1) * sizeof(int));
+    memset(number_uses, 0, sizeof(int) * (max_strength_bits + 1));
 
     /*
      * Now find the strength_bits values actually used
diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c
index 56d7e6c..0a2c04e 100644
--- a/ssl/ssl_lib.c
+++ b/ssl/ssl_lib.c
@@ -275,7 +275,7 @@
     s = OPENSSL_malloc(sizeof(*s));
     if (s == NULL)
         goto err;
-    memset(s, 0, sizeof(SSL));
+    memset(s, 0, sizeof(*s));
 
     RECORD_LAYER_init(&s->rlayer, s);
 
@@ -1848,7 +1848,7 @@
     if (ret == NULL)
         goto err;
 
-    memset(ret, 0, sizeof(SSL_CTX));
+    memset(ret, 0, sizeof(*ret));
 
     ret->method = meth;
 
@@ -1866,7 +1866,7 @@
     ret->get_session_cb = 0;
     ret->generate_session_id = 0;
 
-    memset((char *)&ret->stats, 0, sizeof(ret->stats));
+    memset(&ret->stats, 0, sizeof(ret->stats));
 
     ret->references = 1;
     ret->quiet_shutdown = 0;
diff --git a/ssl/ssl_sess.c b/ssl/ssl_sess.c
index b592da4..4e73f04 100644
--- a/ssl/ssl_sess.c
+++ b/ssl/ssl_sess.c
@@ -198,7 +198,7 @@
         SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
         return (0);
     }
-    memset(ss, 0, sizeof(SSL_SESSION));
+    memset(ss, 0, sizeof(*ss));
 
     ss->verify_result = 1;      /* avoid 0 (= X509_V_OK) just in case */
     ss->references = 1;
diff --git a/ssl/t1_ext.c b/ssl/t1_ext.c
index 193cae8..f1092ac 100644
--- a/ssl/t1_ext.c
+++ b/ssl/t1_ext.c
@@ -232,7 +232,7 @@
     }
 
     meth = exts->meths + exts->meths_count;
-    memset(meth, 0, sizeof(custom_ext_method));
+    memset(meth, 0, sizeof(*meth));
     meth->parse_cb = parse_cb;
     meth->add_cb = add_cb;
     meth->free_cb = free_cb;