From e5b0fc18472b9780a0563b1a5550f039264e88da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 12 Nov 2014 22:27:42 +0100 Subject: [PATCH] Make malloc-init script a bit happier --- library/asn1write.c | 4 ++-- library/ssl_cache.c | 13 +++++-------- library/ssl_tls.c | 8 ++++---- library/x509_crl.c | 6 +++--- library/x509_crt.c | 2 +- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/library/asn1write.c b/library/asn1write.c index ebc0e9766..d3ece60c7 100644 --- a/library/asn1write.c +++ b/library/asn1write.c @@ -327,6 +327,8 @@ asn1_named_data *asn1_store_named_data( asn1_named_data **head, return( NULL ); } + memcpy( cur->oid.p, oid, oid_len ); + cur->val.len = val_len; cur->val.p = polarssl_malloc( val_len ); if( cur->val.p == NULL ) @@ -336,8 +338,6 @@ asn1_named_data *asn1_store_named_data( asn1_named_data **head, return( NULL ); } - memcpy( cur->oid.p, oid, oid_len ); - cur->next = *head; *head = cur; } diff --git a/library/ssl_cache.c b/library/ssl_cache.c index 836b68511..5868e693b 100644 --- a/library/ssl_cache.c +++ b/library/ssl_cache.c @@ -105,10 +105,8 @@ int ssl_cache_get( void *data, ssl_session *session ) */ if( entry->peer_cert.p != NULL ) { - session->peer_cert = - (x509_crt *) polarssl_malloc( sizeof(x509_crt) ); - - if( session->peer_cert == NULL ) + if( ( session->peer_cert = (x509_crt *) polarssl_malloc( + sizeof(x509_crt) ) ) == NULL ) { ret = 1; goto exit; @@ -226,8 +224,7 @@ int ssl_cache_set( void *data, const ssl_session *session ) /* * max_entries not reached, create new entry */ - cur = (ssl_cache_entry *) - polarssl_malloc( sizeof(ssl_cache_entry) ); + cur = (ssl_cache_entry *) polarssl_malloc( sizeof(ssl_cache_entry) ); if( cur == NULL ) { ret = 1; @@ -264,8 +261,8 @@ int ssl_cache_set( void *data, const ssl_session *session ) */ if( session->peer_cert != NULL ) { - cur->peer_cert.p = (unsigned char *) - polarssl_malloc( session->peer_cert->raw.len ); + cur->peer_cert.p = (unsigned char *) polarssl_malloc( + session->peer_cert->raw.len ); if( cur->peer_cert.p == NULL ) { ret = 1; diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 33d4678c1..89688867a 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -3341,14 +3341,14 @@ static int ssl_handshake_init( ssl_context *ssl ) */ if( ssl->transform_negotiate == NULL ) { - ssl->transform_negotiate = - (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) ); + ssl->transform_negotiate = (ssl_transform *) polarssl_malloc( + sizeof(ssl_transform) ); } if( ssl->session_negotiate == NULL ) { - ssl->session_negotiate = - (ssl_session *) polarssl_malloc( sizeof(ssl_session) ); + ssl->session_negotiate = (ssl_session *) polarssl_malloc( + sizeof(ssl_session) ); } if( ssl->handshake == NULL ) diff --git a/library/x509_crl.c b/library/x509_crl.c index 7dd53c2f6..b9a5c1373 100644 --- a/library/x509_crl.c +++ b/library/x509_crl.c @@ -243,8 +243,8 @@ static int x509_get_entries( unsigned char **p, if( cur_entry->next == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); + memset( cur_entry->next, 0, sizeof( x509_crl_entry ) ); cur_entry = cur_entry->next; - memset( cur_entry, 0, sizeof( x509_crl_entry ) ); } } @@ -294,8 +294,8 @@ int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); } + x509_crl_init( crl->next ); crl = crl->next; - x509_crl_init( crl ); } #if defined(POLARSSL_PEM_PARSE_C) @@ -532,8 +532,8 @@ int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); } + x509_crl_init( crl->next ); crl = crl->next; - x509_crl_init( crl ); return( x509_crl_parse( crl, buf, buflen ) ); } diff --git a/library/x509_crt.c b/library/x509_crt.c index 6193ecd12..2d72f6acd 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -819,8 +819,8 @@ int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf, return( POLARSSL_ERR_X509_MALLOC_FAILED ); prev = crt; + x509_crt_init( crt->next ); crt = crt->next; - x509_crt_init( crt ); } if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )