From 55c11ba2833baac36dd80c824e3c9a6d7195fb76 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Thu, 23 Aug 2018 14:36:33 +0100 Subject: [PATCH] Correct memory-leak in pk_encrypt example program --- programs/pkey/pk_encrypt.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/programs/pkey/pk_encrypt.c b/programs/pkey/pk_encrypt.c index 400619c5c..24c5b566a 100644 --- a/programs/pkey/pk_encrypt.c +++ b/programs/pkey/pk_encrypt.c @@ -73,6 +73,8 @@ int main( int argc, char *argv[] ) const char *pers = "mbedtls_pk_encrypt"; mbedtls_ctr_drbg_init( &ctr_drbg ); + mbedtls_entropy_init( &entropy ); + mbedtls_pk_init( &pk ); if( argc != 3 ) { @@ -88,7 +90,6 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Seeding the random number generator..." ); fflush( stdout ); - mbedtls_entropy_init( &entropy ); if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen( pers ) ) ) != 0 ) @@ -100,8 +101,6 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Reading public key from '%s'", argv[1] ); fflush( stdout ); - mbedtls_pk_init( &pk ); - if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); @@ -136,6 +135,7 @@ int main( int argc, char *argv[] ) if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL ) { mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" ); + ret = 1; goto exit; } @@ -150,8 +150,10 @@ int main( int argc, char *argv[] ) exit_code = MBEDTLS_EXIT_SUCCESS; exit: - mbedtls_ctr_drbg_free( &ctr_drbg ); + + mbedtls_pk_free( &pk ); mbedtls_entropy_free( &entropy ); + mbedtls_ctr_drbg_free( &ctr_drbg ); #if defined(MBEDTLS_ERROR_C) if( exit_code != MBEDTLS_EXIT_SUCCESS )