Convert some malloc + memcpys into BUF_memdup.

Slightly tidier.

Change-Id: Ib3cb4dc262c88087bd56b446a6f7a05d1e57ade6
Reviewed-on: https://boringssl-review.googlesource.com/1345
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
David Benjamin 2014-07-26 11:44:25 -04:00 committed by Adam Langley
parent 9a37359008
commit 072c953f40
2 changed files with 11 additions and 17 deletions

View File

@ -373,12 +373,11 @@ SSL *SSL_new(SSL_CTX *ctx)
if (s->ctx->alpn_client_proto_list) if (s->ctx->alpn_client_proto_list)
{ {
s->alpn_client_proto_list = s->alpn_client_proto_list = BUF_memdup(
OPENSSL_malloc(s->ctx->alpn_client_proto_list_len); s->ctx->alpn_client_proto_list,
s->ctx->alpn_client_proto_list_len);
if (s->alpn_client_proto_list == NULL) if (s->alpn_client_proto_list == NULL)
goto err; goto err;
memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,
s->ctx->alpn_client_proto_list_len);
s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len; s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
} }
@ -551,10 +550,9 @@ ssl_cipher_preference_list_dup(
ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers); ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
if (!ret->ciphers) if (!ret->ciphers)
goto err; goto err;
ret->in_group_flags = OPENSSL_malloc(n); ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
if (!ret->in_group_flags) if (!ret->in_group_flags)
goto err; goto err;
memcpy(ret->in_group_flags, cipher_list->in_group_flags, n);
return ret; return ret;
err: err:
@ -1802,10 +1800,9 @@ int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
if (ctx->alpn_client_proto_list) if (ctx->alpn_client_proto_list)
OPENSSL_free(ctx->alpn_client_proto_list); OPENSSL_free(ctx->alpn_client_proto_list);
ctx->alpn_client_proto_list = OPENSSL_malloc(protos_len); ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
if (!ctx->alpn_client_proto_list) if (!ctx->alpn_client_proto_list)
return 1; return 1;
memcpy(ctx->alpn_client_proto_list, protos, protos_len);
ctx->alpn_client_proto_list_len = protos_len; ctx->alpn_client_proto_list_len = protos_len;
return 0; return 0;
@ -1822,10 +1819,9 @@ int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
if (ssl->alpn_client_proto_list) if (ssl->alpn_client_proto_list)
OPENSSL_free(ssl->alpn_client_proto_list); OPENSSL_free(ssl->alpn_client_proto_list);
ssl->alpn_client_proto_list = OPENSSL_malloc(protos_len); ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
if (!ssl->alpn_client_proto_list) if (!ssl->alpn_client_proto_list)
return 1; return 1;
memcpy(ssl->alpn_client_proto_list, protos, protos_len);
ssl->alpn_client_proto_list_len = protos_len; ssl->alpn_client_proto_list_len = protos_len;
return 0; return 0;

View File

@ -1238,13 +1238,12 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned c
else if (s->session && s->tlsext_session_ticket && else if (s->session && s->tlsext_session_ticket &&
s->tlsext_session_ticket->data) s->tlsext_session_ticket->data)
{ {
ticklen = s->tlsext_session_ticket->length; s->session->tlsext_tick = BUF_memdup(
s->session->tlsext_tick = OPENSSL_malloc(ticklen); s->tlsext_session_ticket->data,
s->tlsext_session_ticket->length);
if (!s->session->tlsext_tick) if (!s->session->tlsext_tick)
return NULL; return NULL;
memcpy(s->session->tlsext_tick, ticklen = s->tlsext_session_ticket->length;
s->tlsext_session_ticket->data,
ticklen);
s->session->tlsext_ticklen = ticklen; s->session->tlsext_ticklen = ticklen;
} }
else else
@ -1687,13 +1686,12 @@ static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
if (r == SSL_TLSEXT_ERR_OK) { if (r == SSL_TLSEXT_ERR_OK) {
if (s->s3->alpn_selected) if (s->s3->alpn_selected)
OPENSSL_free(s->s3->alpn_selected); OPENSSL_free(s->s3->alpn_selected);
s->s3->alpn_selected = OPENSSL_malloc(selected_len); s->s3->alpn_selected = BUF_memdup(selected, selected_len);
if (!s->s3->alpn_selected) if (!s->s3->alpn_selected)
{ {
*out_alert = SSL_AD_INTERNAL_ERROR; *out_alert = SSL_AD_INTERNAL_ERROR;
return 0; return 0;
} }
memcpy(s->s3->alpn_selected, selected, selected_len);
s->s3->alpn_selected_len = selected_len; s->s3->alpn_selected_len = selected_len;
} }
return 1; return 1;