Avoid static buffer in debug module

Caused issues in threading situations
This commit is contained in:
Manuel Pégourié-Gonnard 2015-06-23 12:04:52 +02:00
parent 96fb685e31
commit d23f593737
3 changed files with 31 additions and 11 deletions

View file

@ -57,7 +57,7 @@
#define MBEDTLS_SSL_DEBUG_MSG( level, args ) \ #define MBEDTLS_SSL_DEBUG_MSG( level, args ) \
mbedtls_debug_print_msg( ssl, level, __FILE__, __LINE__, mbedtls_debug_fmt args ) mbedtls_debug_print_msg_free( ssl, level, __FILE__, __LINE__, mbedtls_debug_fmt args )
#define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) \ #define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) \
mbedtls_debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret ) mbedtls_debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret )
@ -118,6 +118,9 @@ char *mbedtls_debug_fmt( const char *format, ... );
void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
const char *file, int line, const char *text ); 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_ret( const mbedtls_ssl_context *ssl, int level, void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
const char *file, int line, const char *file, int line,
const char *text, int ret ); const char *text, int ret );

View file

@ -37,9 +37,14 @@
#if defined(MBEDTLS_PLATFORM_C) #if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
#else #else
#define mbedtls_snprintf snprintf #include <stdlib.h>
#define mbedtls_calloc calloc
#define mbedtls_free free
#define mbedtls_snprintf snprintf
#endif #endif
#define DEBUG_BUF_SIZE 512
static int debug_log_mode = MBEDTLS_DEBUG_DFL_MODE; static int debug_log_mode = MBEDTLS_DEBUG_DFL_MODE;
static int debug_threshold = 0; static int debug_threshold = 0;
@ -56,23 +61,35 @@ void mbedtls_debug_set_threshold( int threshold )
char *mbedtls_debug_fmt( const char *format, ... ) char *mbedtls_debug_fmt( const char *format, ... )
{ {
va_list argp; va_list argp;
static char str[512]; char *str = mbedtls_calloc( DEBUG_BUF_SIZE, 1 );
if( str == NULL )
return( NULL );
va_start( argp, format ); va_start( argp, format );
#if defined(_WIN32) #if defined(_WIN32)
_vsnprintf_s( str, sizeof( str ), _TRUNCATE, format, argp ); _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
#else #else
vsnprintf( str, sizeof( str ), format, argp ); vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
#endif #endif
va_end( argp ); va_end( argp );
return( str ); return( 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, void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
const char *file, int line, const char *text ) const char *file, int line, const char *text )
{ {
char str[512]; char str[DEBUG_BUF_SIZE];
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
return; return;
@ -91,7 +108,7 @@ void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
const char *file, int line, const char *file, int line,
const char *text, int ret ) const char *text, int ret )
{ {
char str[512]; char str[DEBUG_BUF_SIZE];
size_t idx = 0; size_t idx = 0;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
@ -118,7 +135,7 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
const char *file, int line, const char *text, const char *file, int line, const char *text,
const unsigned char *buf, size_t len ) const unsigned char *buf, size_t len )
{ {
char str[512]; char str[DEBUG_BUF_SIZE];
char txt[17]; char txt[17];
size_t i, idx = 0; size_t i, idx = 0;
@ -179,7 +196,7 @@ void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
const char *file, int line, const char *file, int line,
const char *text, const mbedtls_ecp_point *X ) const char *text, const mbedtls_ecp_point *X )
{ {
char str[512]; char str[DEBUG_BUF_SIZE];
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
return; return;
@ -197,7 +214,7 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
const char *file, int line, const char *file, int line,
const char *text, const mbedtls_mpi *X ) const char *text, const mbedtls_mpi *X )
{ {
char str[512]; char str[DEBUG_BUF_SIZE];
int j, k, zeros = 1; int j, k, zeros = 1;
size_t i, n, idx = 0; size_t i, n, idx = 0;

View file

@ -48,7 +48,7 @@ void debug_print_msg_threshold( int threshold, int level, char *file, int line,
mbedtls_debug_set_threshold( threshold ); mbedtls_debug_set_threshold( threshold );
mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer); mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
mbedtls_debug_print_msg( &ssl, level, file, line, mbedtls_debug_print_msg_free( &ssl, level, file, line,
mbedtls_debug_fmt("Text message, 2 == %d", 2 ) ); mbedtls_debug_fmt("Text message, 2 == %d", 2 ) );
TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 ); TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );