Fix outcome file leak if execute_tests exits early
If there was a fatal error (bizarre behavior from the standard
library, or missing test data file), execute_tests did not close the
outcome file. Fix this.
@@ -525,15 +525,6 @@ int execute_tests( int argc , const char ** argv )
mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
#endif
- if( outcome_file_name != NULL )
- {
- outcome_file = fopen( outcome_file_name, "a" );
- if( outcome_file == NULL )
- {
- mbedtls_fprintf( stderr, "Unable to open outcome file. Continuing anyway.\n" );
- }
- }
-
/*
* The C standard doesn't guarantee that all-bits-0 is the representation
* of a NULL pointer. We do however use that in our code for initializing
@@ -555,6 +546,15 @@ int execute_tests( int argc , const char ** argv )
return( 1 );
}
+ if( outcome_file_name != NULL )
+ {
+ outcome_file = fopen( outcome_file_name, "a" );
+ if( outcome_file == NULL )
+ {
+ mbedtls_fprintf( stderr, "Unable to open outcome file. Continuing anyway.\n" );
+ }
+ }
+
while( arg_index < argc )
{
next_arg = argv[arg_index];
@@ -607,6 +607,8 @@ int execute_tests( int argc , const char ** argv )
{
mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
test_filename );
+ if( outcome_file != NULL )
+ fclose( outcome_file );
return( 1 );
}
