mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-29 11:41:15 +03:00
Replace malloc with calloc
- platform layer currently broken (not adapted yet) - memmory_buffer_alloc too
This commit is contained in:
@ -51,7 +51,7 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_malloc malloc
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_free free
|
||||
#endif
|
||||
|
||||
@ -173,7 +173,7 @@ static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session
|
||||
{
|
||||
int ret;
|
||||
|
||||
dst->peer_cert = mbedtls_malloc( sizeof(mbedtls_x509_crt) );
|
||||
dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
|
||||
if( dst->peer_cert == NULL )
|
||||
return( MBEDTLS_ERR_SSL_MALLOC_FAILED );
|
||||
|
||||
@ -192,7 +192,7 @@ static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session
|
||||
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
||||
if( src->ticket != NULL )
|
||||
{
|
||||
dst->ticket = mbedtls_malloc( src->ticket_len );
|
||||
dst->ticket = mbedtls_calloc( 1, src->ticket_len );
|
||||
if( dst->ticket == NULL )
|
||||
return( MBEDTLS_ERR_SSL_MALLOC_FAILED );
|
||||
|
||||
@ -929,7 +929,7 @@ int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
|
||||
if( ssl->compress_buf == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
|
||||
ssl->compress_buf = mbedtls_malloc( MBEDTLS_SSL_BUFFER_LEN );
|
||||
ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN );
|
||||
if( ssl->compress_buf == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
|
||||
@ -2454,14 +2454,14 @@ static int ssl_flight_append( mbedtls_ssl_context *ssl )
|
||||
mbedtls_ssl_flight_item *msg;
|
||||
|
||||
/* Allocate space for current message */
|
||||
if( ( msg = mbedtls_malloc( sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
|
||||
if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
|
||||
sizeof( mbedtls_ssl_flight_item ) ) );
|
||||
return( MBEDTLS_ERR_SSL_MALLOC_FAILED );
|
||||
}
|
||||
|
||||
if( ( msg->p = mbedtls_malloc( ssl->out_msglen ) ) == NULL )
|
||||
if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
|
||||
mbedtls_free( msg );
|
||||
@ -2924,7 +2924,7 @@ static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
|
||||
/* The bitmask needs one bit per byte of message excluding header */
|
||||
alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
|
||||
|
||||
ssl->handshake->hs_msg = mbedtls_malloc( alloc_len );
|
||||
ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
|
||||
if( ssl->handshake->hs_msg == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
|
||||
@ -3975,7 +3975,7 @@ int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
|
||||
mbedtls_free( ssl->session_negotiate->peer_cert );
|
||||
}
|
||||
|
||||
if( ( ssl->session_negotiate->peer_cert = mbedtls_malloc(
|
||||
if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
|
||||
sizeof( mbedtls_x509_crt ) ) ) == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
|
||||
@ -4898,17 +4898,17 @@ static int ssl_handshake_init( mbedtls_ssl_context *ssl )
|
||||
*/
|
||||
if( ssl->transform_negotiate == NULL )
|
||||
{
|
||||
ssl->transform_negotiate = mbedtls_malloc( sizeof(mbedtls_ssl_transform) );
|
||||
ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
|
||||
}
|
||||
|
||||
if( ssl->session_negotiate == NULL )
|
||||
{
|
||||
ssl->session_negotiate = mbedtls_malloc( sizeof(mbedtls_ssl_session) );
|
||||
ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
|
||||
}
|
||||
|
||||
if( ssl->handshake == NULL )
|
||||
{
|
||||
ssl->handshake = mbedtls_malloc( sizeof(mbedtls_ssl_handshake_params) );
|
||||
ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
|
||||
}
|
||||
|
||||
/* All pointers should exist and can be directly freed without issue */
|
||||
@ -5002,8 +5002,8 @@ int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
|
||||
/*
|
||||
* Prepare base structures
|
||||
*/
|
||||
if( ( ssl-> in_buf = mbedtls_malloc( len ) ) == NULL ||
|
||||
( ssl->out_buf = mbedtls_malloc( len ) ) == NULL )
|
||||
if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
|
||||
( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
|
||||
mbedtls_free( ssl->in_buf );
|
||||
@ -5309,7 +5309,7 @@ static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
|
||||
{
|
||||
mbedtls_ssl_key_cert *new;
|
||||
|
||||
new = mbedtls_malloc( sizeof( mbedtls_ssl_key_cert ) );
|
||||
new = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
|
||||
if( new == NULL )
|
||||
return( MBEDTLS_ERR_SSL_MALLOC_FAILED );
|
||||
|
||||
@ -5384,8 +5384,8 @@ int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
|
||||
mbedtls_free( conf->psk_identity );
|
||||
}
|
||||
|
||||
if( ( conf->psk = mbedtls_malloc( psk_len ) ) == NULL ||
|
||||
( conf->psk_identity = mbedtls_malloc( psk_identity_len ) ) == NULL )
|
||||
if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
|
||||
( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
|
||||
{
|
||||
mbedtls_free( conf->psk );
|
||||
conf->psk = NULL;
|
||||
@ -5413,7 +5413,7 @@ int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
|
||||
if( ssl->handshake->psk != NULL )
|
||||
mbedtls_free( ssl->conf->psk );
|
||||
|
||||
if( ( ssl->handshake->psk = mbedtls_malloc( psk_len ) ) == NULL )
|
||||
if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
|
||||
{
|
||||
mbedtls_free( ssl->handshake->psk );
|
||||
ssl->handshake->psk = NULL;
|
||||
@ -5492,7 +5492,7 @@ int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
|
||||
if( hostname_len + 1 == 0 )
|
||||
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
||||
|
||||
ssl->hostname = mbedtls_malloc( hostname_len + 1 );
|
||||
ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
|
||||
|
||||
if( ssl->hostname == NULL )
|
||||
return( MBEDTLS_ERR_SSL_MALLOC_FAILED );
|
||||
|
Reference in New Issue
Block a user