From cfb7a9917a79950b98847cedb9414b8137dadd9c Mon Sep 17 00:00:00 2001 From: Przemyslaw Czerpak Date: Thu, 10 Mar 2011 19:23:26 +0000 Subject: [PATCH] 2011-03-10 20:23 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/src/common/hbtrace.c ! use hb_getenv_buffer() instead of hb_getenv() to not allocate memory when trace system is activated and added protection against recursive calls. * harbour/src/common/hbprintf.c ! use internal function _hb_strnlen() instead of hb_strnlen() to avoid recursive calls in Harbour trace system --- harbour/ChangeLog | 10 ++++++++ harbour/src/common/hbprintf.c | 12 +++++++++- harbour/src/common/hbtrace.c | 44 ++++++++++++++--------------------- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index cca9d1337a..ac376c407c 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,16 @@ The license applies to all entries newer than 2009-04-28. */ +2011-03-10 20:23 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + * harbour/src/common/hbtrace.c + ! use hb_getenv_buffer() instead of hb_getenv() to not allocate + memory when trace system is activated and added protection + against recursive calls. + + * harbour/src/common/hbprintf.c + ! use internal function _hb_strnlen() instead of hb_strnlen() + to avoid recursive calls in Harbour trace system + 2011-03-10 19:04 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) * bin/hb3rdpat.hbs + Added -validate option diff --git a/harbour/src/common/hbprintf.c b/harbour/src/common/hbprintf.c index 60ce7ef870..9e23a1c429 100644 --- a/harbour/src/common/hbprintf.c +++ b/harbour/src/common/hbprintf.c @@ -695,6 +695,16 @@ static size_t put_hex( char *buffer, size_t bufsize, size_t size, return size; } +static int _hb_strnlen( const char * str, int len ) +{ + int i = 0; + + while( len-- && *str++ ) + ++i; + + return i; +} + static size_t put_str( char *buffer, size_t bufsize, size_t size, const char * str, int flags, int width, int precision ) { @@ -703,7 +713,7 @@ static size_t put_str( char *buffer, size_t bufsize, size_t size, if( precision < 0 ) precision = ( int ) strlen( str ); else if( precision > 0 ) - precision = ( int ) hb_strnlen( str, precision ); + precision = ( int ) _hb_strnlen( str, precision ); width -= precision; if( ( flags & _F_LEFTADJUSTED ) == 0 ) while( width > 0 ) diff --git a/harbour/src/common/hbtrace.c b/harbour/src/common/hbtrace.c index 7953c36624..a588bf67f6 100644 --- a/harbour/src/common/hbtrace.c +++ b/harbour/src/common/hbtrace.c @@ -161,7 +161,11 @@ int hb_tr_level( void ) { if( s_level == -1 ) { - char * env; + char env[ HB_PATH_MAX ]; + int enabled = s_enabled; + + /* protection against recursive or concurrent calls */ + s_enabled = 0; s_level = HB_TR_DEFAULT; @@ -169,8 +173,8 @@ int hb_tr_level( void ) if( s_fp == NULL ) { - env = hb_getenv( "HB_TR_OUTPUT" ); - if( env != NULL && env[ 0 ] != '\0' ) + if( hb_getenv_buffer( "HB_TR_OUTPUT", env, sizeof( env ) ) && + env[ 0 ] != '\0' ) { s_fp = hb_fopen( env, "w" ); @@ -179,15 +183,12 @@ int hb_tr_level( void ) } else s_fp = stderr; - - if( env ) - hb_xfree( env ); } /* ; */ - env = hb_getenv( "HB_TR_LEVEL" ); - if( env != NULL && env[ 0 ] != '\0' ) + if( hb_getenv_buffer( "HB_TR_LEVEL", env, sizeof( env ) ) && + env[ 0 ] != '\0' ) { int i; @@ -202,36 +203,25 @@ int hb_tr_level( void ) } } - if( env ) - hb_xfree( env ); - /* ; */ if( s_sysout < 0 ) { - env = hb_getenv( "HB_TR_SYSOUT" ); - if( env != NULL && env[ 0 ] != '\0' ) - s_sysout = 1; - else - s_sysout = 0; - - if( env ) - hb_xfree( env ); + s_sysout = ( hb_getenv_buffer( "HB_TR_SYSOUT", env, sizeof( env ) ) && + env[ 0 ] != '\0' ) ? 1 : 0; } /* ; */ if( s_flush < 0 ) { - env = hb_getenv( "HB_TR_FLUSH" ); - if( env != NULL && env[ 0 ] != '\0' ) - s_flush = 1; - else - s_flush = 0; - - if( env ) - hb_xfree( env ); + s_flush = ( hb_getenv_buffer( "HB_TR_FLUSH", env, sizeof( env ) ) && + env[ 0 ] != '\0' ) ? 1 : 0; } + + /* ; */ + + s_enabled = enabled; } return s_level;