Correct memory-leak in pk_encrypt example program

This commit is contained in:
Hanno Becker 2018-08-23 14:36:33 +01:00
parent 6b7b35b127
commit 34cbd7e536

View file

@ -70,6 +70,8 @@ int main( int argc, char *argv[] )
ret = 1; ret = 1;
mbedtls_ctr_drbg_init( &ctr_drbg ); mbedtls_ctr_drbg_init( &ctr_drbg );
mbedtls_entropy_init( &entropy );
mbedtls_pk_init( &pk );
if( argc != 3 ) if( argc != 3 )
{ {
@ -85,7 +87,6 @@ int main( int argc, char *argv[] )
mbedtls_printf( "\n . Seeding the random number generator..." ); mbedtls_printf( "\n . Seeding the random number generator..." );
fflush( stdout ); fflush( stdout );
mbedtls_entropy_init( &entropy );
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) strlen( pers ) ) ) != 0 )
@ -97,8 +98,6 @@ int main( int argc, char *argv[] )
mbedtls_printf( "\n . Reading public key from '%s'", argv[1] ); mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
fflush( stdout ); fflush( stdout );
mbedtls_pk_init( &pk );
if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) 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 ); mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
@ -134,6 +133,7 @@ int main( int argc, char *argv[] )
{ {
ret = 1; ret = 1;
mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" ); mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
ret = 1;
goto exit; goto exit;
} }
@ -146,8 +146,10 @@ int main( int argc, char *argv[] )
mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" ); mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
exit: exit:
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_pk_free( &pk );
mbedtls_entropy_free( &entropy ); mbedtls_entropy_free( &entropy );
mbedtls_ctr_drbg_free( &ctr_drbg );
#if defined(MBEDTLS_ERROR_C) #if defined(MBEDTLS_ERROR_C)
if( ret != 0 ) if( ret != 0 )