Fix memory leaks in example programs.

This commit is contained in:
Janos Follath 2016-03-17 15:21:39 +00:00 committed by Simon Butcher
parent 8970fd6ab9
commit fa4a88a8eb
4 changed files with 13 additions and 10 deletions

View File

@ -69,6 +69,8 @@ int main( int argc, char *argv[] )
((void) argv); ((void) argv);
memset(result, 0, sizeof( result ) ); memset(result, 0, sizeof( result ) );
entropy_init( &entropy );
rsa_init( &rsa, RSA_PKCS_V15, 0 );
ret = 1; ret = 1;
if( argc != 1 ) if( argc != 1 )
@ -79,13 +81,12 @@ int main( int argc, char *argv[] )
polarssl_printf( "\n" ); polarssl_printf( "\n" );
#endif #endif
goto exit; return ret;
} }
polarssl_printf( "\n . Seeding the random number generator..." ); polarssl_printf( "\n . Seeding the random number generator..." );
fflush( stdout ); fflush( stdout );
entropy_init( &entropy );
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) strlen( pers ) ) ) != 0 )
@ -104,8 +105,6 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
rsa_init( &rsa, RSA_PKCS_V15, 0 );
if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
@ -171,6 +170,7 @@ int main( int argc, char *argv[] )
exit: exit:
ctr_drbg_free( &ctr_drbg ); ctr_drbg_free( &ctr_drbg );
entropy_free( &entropy ); entropy_free( &entropy );
rsa_free( &rsa );
#if defined(_WIN32) #if defined(_WIN32)
polarssl_printf( " + Press Enter to exit this program.\n" ); polarssl_printf( " + Press Enter to exit this program.\n" );

View File

@ -68,6 +68,7 @@ int main( int argc, char *argv[] )
unsigned char buf[512]; unsigned char buf[512];
const char *pers = "rsa_encrypt"; const char *pers = "rsa_encrypt";
rsa_init( &rsa, RSA_PKCS_V15, 0 );
ret = 1; ret = 1;
if( argc != 2 ) if( argc != 2 )
@ -104,8 +105,6 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
rsa_init( &rsa, RSA_PKCS_V15, 0 );
if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
{ {
@ -160,6 +159,7 @@ int main( int argc, char *argv[] )
exit: exit:
ctr_drbg_free( &ctr_drbg ); ctr_drbg_free( &ctr_drbg );
entropy_free( &entropy ); entropy_free( &entropy );
rsa_free( &rsa );
#if defined(_WIN32) #if defined(_WIN32)
polarssl_printf( " + Press Enter to exit this program.\n" ); polarssl_printf( " + Press Enter to exit this program.\n" );

View File

@ -67,6 +67,7 @@ int main( int argc, char *argv[] )
unsigned char buf[POLARSSL_MPI_MAX_SIZE]; unsigned char buf[POLARSSL_MPI_MAX_SIZE];
char filename[512]; char filename[512];
rsa_init( &rsa, RSA_PKCS_V15, 0 );
ret = 1; ret = 1;
if( argc != 2 ) if( argc != 2 )
@ -91,8 +92,6 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
rsa_init( &rsa, RSA_PKCS_V15, 0 );
if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
@ -160,6 +159,8 @@ int main( int argc, char *argv[] )
exit: exit:
rsa_free( &rsa );
#if defined(_WIN32) #if defined(_WIN32)
polarssl_printf( " + Press Enter to exit this program.\n" ); polarssl_printf( " + Press Enter to exit this program.\n" );
fflush( stdout ); getchar(); fflush( stdout ); getchar();

View File

@ -66,7 +66,9 @@ int main( int argc, char *argv[] )
unsigned char buf[POLARSSL_MPI_MAX_SIZE]; unsigned char buf[POLARSSL_MPI_MAX_SIZE];
char filename[512]; char filename[512];
rsa_init( &rsa, RSA_PKCS_V15, 0 );
ret = 1; ret = 1;
if( argc != 2 ) if( argc != 2 )
{ {
polarssl_printf( "usage: rsa_verify <filename>\n" ); polarssl_printf( "usage: rsa_verify <filename>\n" );
@ -88,8 +90,6 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
rsa_init( &rsa, RSA_PKCS_V15, 0 );
if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
{ {
@ -152,6 +152,8 @@ int main( int argc, char *argv[] )
exit: exit:
rsa_free( &rsa );
#if defined(_WIN32) #if defined(_WIN32)
polarssl_printf( " + Press Enter to exit this program.\n" ); polarssl_printf( " + Press Enter to exit this program.\n" );
fflush( stdout ); getchar(); fflush( stdout ); getchar();