aboutsummaryrefslogtreecommitdiff
path: root/tests/suites/helpers.function
diff options
context:
space:
mode:
authorgufe44 <56860520+gufe44@users.noreply.github.com>2020-07-30 09:02:27 +0200
committergufe44 <gu981@protonmail.com>2020-08-21 08:32:26 +0200
commit067f6e01f1a82041b6f9fa34e0de8149b211c64e (patch)
tree02dca62fc44528276d25cae1d29c54492ce8ac27 /tests/suites/helpers.function
parentee7e85f5b94a49cbe7a1977e4ac089e4c9013aa8 (diff)
downloadmbedtls-067f6e01f1a82041b6f9fa34e0de8149b211c64e.zip
mbedtls-067f6e01f1a82041b6f9fa34e0de8149b211c64e.tar.gz
mbedtls-067f6e01f1a82041b6f9fa34e0de8149b211c64e.tar.bz2
Fix bug in redirection of unit test outputs
Avoid replacing handle. stdout is defined as a macro on several platforms. Signed-off-by: gufe44 <gu981@protonmail.com>
Diffstat (limited to 'tests/suites/helpers.function')
-rw-r--r--tests/suites/helpers.function53
1 files changed, 30 insertions, 23 deletions
diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function
index a5285a3..7425a35 100644
--- a/tests/suites/helpers.function
+++ b/tests/suites/helpers.function
@@ -446,44 +446,51 @@ void mbedtls_param_failed( const char *failure_condition,
#endif
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
-static int redirect_output( FILE** out_stream, const char* path )
+static int redirect_output( FILE* out_stream, const char* path )
{
- int stdout_fd = dup( fileno( *out_stream ) );
+ int out_fd, dup_fd;
+ FILE* path_stream;
- if( stdout_fd == -1 )
+ out_fd = fileno( out_stream );
+ dup_fd = dup( out_fd );
+
+ if( dup_fd == -1 )
{
- return -1;
+ return( -1 );
}
- fflush( *out_stream );
- fclose( *out_stream );
- *out_stream = fopen( path, "w" );
+ path_stream = fopen( path, "w" );
+ if( path_stream == NULL )
+ {
+ close( dup_fd );
+ return( -1 );
+ }
- if( *out_stream == NULL )
+ fflush( out_stream );
+ if( dup2( fileno( path_stream ), out_fd ) == -1 )
{
- close( stdout_fd );
- return -1;
+ close( dup_fd );
+ fclose( path_stream );
+ return( -1 );
}
- return stdout_fd;
+ fclose( path_stream );
+ return( dup_fd );
}
-static int restore_output( FILE** out_stream, int old_fd )
+static int restore_output( FILE* out_stream, int dup_fd )
{
- fflush( *out_stream );
- fclose( *out_stream );
+ int out_fd = fileno( out_stream );
- *out_stream = fdopen( old_fd, "w" );
- if( *out_stream == NULL )
+ fflush( out_stream );
+ if( dup2( dup_fd, out_fd ) == -1 )
{
- return -1;
+ close( out_fd );
+ close( dup_fd );
+ return( -1 );
}
- return 0;
-}
-
-static void close_output( FILE* out_stream )
-{
- fclose( out_stream );
+ close( dup_fd );
+ return( 0 );
}
#endif /* __unix__ || __APPLE__ __MACH__ */