Fix memory leak on failure in test_suite_ssl

This commit is contained in:
Hanno Becker 2019-03-01 11:21:44 +00:00
parent b2ca87d289
commit 81e16a34f5

View File

@ -12,7 +12,9 @@
do \ do \
{ \ { \
if( !( x ) ) \ if( !( x ) ) \
return( 1 ); \ { \
goto cleanup; \
} \
} while( 0 ) } while( 0 )
@ -24,7 +26,7 @@ static int build_transforms( mbedtls_ssl_transform *t_in,
mbedtls_cipher_info_t const *cipher_info; mbedtls_cipher_info_t const *cipher_info;
size_t keylen, maclen, ivlen; size_t keylen, maclen, ivlen;
unsigned char *key0, *key1; unsigned char *key0 = NULL, *key1 = NULL;
unsigned char iv_enc[16], iv_dec[16]; unsigned char iv_enc[16], iv_dec[16];
maclen = 0; maclen = 0;
@ -70,8 +72,6 @@ static int build_transforms( mbedtls_ssl_transform *t_in,
keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0, CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
keylen << 3, MBEDTLS_DECRYPT ) == 0 ); keylen << 3, MBEDTLS_DECRYPT ) == 0 );
free( key0 );
free( key1 );
/* Setup MAC contexts */ /* Setup MAC contexts */
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
@ -223,6 +223,11 @@ static int build_transforms( mbedtls_ssl_transform *t_in,
memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) ); memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) ); memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
cleanup:
free( key0 );
free( key1 );
return( 0 ); return( 0 );
} }
@ -293,7 +298,7 @@ void ssl_crypt_record( int cipher_type, int hash_id,
mbedtls_ssl_context ssl; /* ONLY for debugging */ mbedtls_ssl_context ssl; /* ONLY for debugging */
mbedtls_ssl_transform t0, t1; mbedtls_ssl_transform t0, t1;
unsigned char *buf; unsigned char *buf = NULL;
size_t const buflen = 512; size_t const buflen = 512;
mbedtls_record rec, rec_backup; mbedtls_record rec, rec_backup;
@ -373,6 +378,8 @@ void ssl_crypt_record( int cipher_type, int hash_id,
rec.data_len ) == 0 ); rec.data_len ) == 0 );
} }
exit:
/* Cleanup */ /* Cleanup */
mbedtls_ssl_free( &ssl ); mbedtls_ssl_free( &ssl );
mbedtls_ssl_transform_free( &t0 ); mbedtls_ssl_transform_free( &t0 );
@ -409,7 +416,7 @@ void ssl_crypt_record_small( int cipher_type, int hash_id,
mbedtls_ssl_context ssl; /* ONLY for debugging */ mbedtls_ssl_context ssl; /* ONLY for debugging */
mbedtls_ssl_transform t0, t1; mbedtls_ssl_transform t0, t1;
unsigned char *buf; unsigned char *buf = NULL;
size_t const buflen = 150; size_t const buflen = 150;
mbedtls_record rec, rec_backup; mbedtls_record rec, rec_backup;
@ -519,6 +526,8 @@ void ssl_crypt_record_small( int cipher_type, int hash_id,
TEST_ASSERT( seen_success == 1 ); TEST_ASSERT( seen_success == 1 );
} }
exit:
/* Cleanup */ /* Cleanup */
mbedtls_ssl_free( &ssl ); mbedtls_ssl_free( &ssl );
mbedtls_ssl_transform_free( &t0 ); mbedtls_ssl_transform_free( &t0 );