From b74c245a20359595c00bb2647f969aaacd243a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 29 Jun 2015 20:08:23 +0200 Subject: [PATCH] Rework debug to not need dynamic alloc But introduces dependency on variadic macros --- include/mbedtls/debug.h | 15 ++++++------ library/debug.c | 34 +++++++++++++------------- tests/suites/test_suite_debug.function | 4 +-- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/include/mbedtls/debug.h b/include/mbedtls/debug.h index 618a68ce1..bbbc2e836 100644 --- a/include/mbedtls/debug.h +++ b/include/mbedtls/debug.h @@ -38,8 +38,11 @@ #if defined(MBEDTLS_DEBUG_C) +#define MBEDTLS_DEBUG_STRIP_PARENS( ... ) __VA_ARGS__ + #define MBEDTLS_SSL_DEBUG_MSG( level, args ) \ - mbedtls_debug_print_msg_free( ssl, level, __FILE__, __LINE__, mbedtls_debug_fmt args ) + mbedtls_debug_print_fmt( ssl, level, __FILE__, __LINE__, \ + MBEDTLS_DEBUG_STRIP_PARENS args ) #define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) \ mbedtls_debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret ) @@ -86,13 +89,9 @@ extern "C" { */ void mbedtls_debug_set_threshold( int threshold ); -char *mbedtls_debug_fmt( const char *format, ... ); - -void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, - const char *file, int line, const char *text ); - -void mbedtls_debug_print_msg_free( const mbedtls_ssl_context *ssl, int level, - const char *file, int line, char *text ); +void mbedtls_debug_print_fmt( const mbedtls_ssl_context *ssl, int level, + const char *file, int line, + const char *format, ... ); void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level, const char *file, int line, diff --git a/library/debug.c b/library/debug.c index 1660978d2..11076ea4c 100644 --- a/library/debug.c +++ b/library/debug.c @@ -52,35 +52,35 @@ void mbedtls_debug_set_threshold( int threshold ) debug_threshold = threshold; } -char *mbedtls_debug_fmt( const char *format, ... ) +void mbedtls_debug_print_fmt( const mbedtls_ssl_context *ssl, int level, + const char *file, int line, + const char *format, ... ) { va_list argp; - char *str = mbedtls_calloc( DEBUG_BUF_SIZE, 1 ); + char str[DEBUG_BUF_SIZE]; + int ret; - if( str == NULL ) - return( NULL ); + if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) + return; va_start( argp, format ); #if defined(_WIN32) - _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp ); + ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp ); #else - vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); + ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); #endif va_end( argp ); - return( str ); + if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 ) + { + str[ret] = '\n'; + str[ret + 1] = '\0'; + } + + ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str ); } -void mbedtls_debug_print_msg_free( const mbedtls_ssl_context *ssl, int level, - const char *file, int line, char *text ) -{ - if( text != NULL ) - mbedtls_debug_print_msg( ssl, level, file, line, text ); - - mbedtls_free( text ); -} - -void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, +static void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *text ) { char str[DEBUG_BUF_SIZE]; diff --git a/tests/suites/test_suite_debug.function b/tests/suites/test_suite_debug.function index 3cf6c9f07..9cfd68472 100644 --- a/tests/suites/test_suite_debug.function +++ b/tests/suites/test_suite_debug.function @@ -59,8 +59,8 @@ void debug_print_msg_threshold( int threshold, int level, char *file, int line, mbedtls_debug_set_threshold( threshold ); mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer); - mbedtls_debug_print_msg_free( &ssl, level, file, line, - mbedtls_debug_fmt("Text message, 2 == %d", 2 ) ); + mbedtls_debug_print_fmt( &ssl, level, file, line, + "Text message, 2 == %d", 2 ); TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );