From d5b327f2481eea3890abfa2e0bef7dd8ef3e192a Mon Sep 17 00:00:00 2001 From: Przemyslaw Czerpak Date: Mon, 25 Aug 2008 18:16:02 +0000 Subject: [PATCH] 2008-08-25 20:14 UTC+0200 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/contrib/hbw32/w32_ole.c * removed hack with malloc()/free() directly used to avoid memory leak reports - it's not necessary in Harbour. * harbour/contrib/hbfbird/firebird.c * harbour/contrib/examples/pp/hbppcore.c ! fixed buffer size calculation in hbstrnc*() functions * harbour/contrib/hbziparch/hbzipnew.cpp % use hb_strdup() instead of hb_xgrab()/hb_strncpy() * harbour/contrib/hbnf/getenvrn.c ! use hb_xgrab() instead of hb_xalloc() - the returned value was not checked and internal error is for sure better then GPF on NULL pointer * harbour/source/rdd/dbfntx/dbfntx1.c ! use memcpy() instead of hb_strncpy() to avoid problems when there is no place for tailing 0 --- harbour/ChangeLog | 21 +++++++++++++++++++ harbour/contrib/examples/pp/hbppcore.c | 6 +++--- harbour/contrib/hbfbird/firebird.c | 4 ++++ harbour/contrib/hbnf/getenvrn.c | 4 ++-- harbour/contrib/hbw32/w32_ole.c | 12 +++++------ harbour/contrib/hbziparch/hbzipnew.cpp | 3 +-- harbour/source/rdd/dbfntx/dbfntx1.c | 29 ++++++++++++++++++++------ 7 files changed, 59 insertions(+), 20 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 4879047745..1d14a98c98 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,6 +8,27 @@ 2008-12-31 13:59 UTC+0100 Foo Bar (foo.bar foobar.org) */ +2008-08-25 20:14 UTC+0200 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + * harbour/contrib/hbw32/w32_ole.c + * removed hack with malloc()/free() directly used to avoid + memory leak reports - it's not necessary in Harbour. + + * harbour/contrib/hbfbird/firebird.c + * harbour/contrib/examples/pp/hbppcore.c + ! fixed buffer size calculation in hbstrnc*() functions + + * harbour/contrib/hbziparch/hbzipnew.cpp + % use hb_strdup() instead of hb_xgrab()/hb_strncpy() + + * harbour/contrib/hbnf/getenvrn.c + ! use hb_xgrab() instead of hb_xalloc() - the returned value + was not checked and internal error is for sure better then + GPF on NULL pointer + + * harbour/source/rdd/dbfntx/dbfntx1.c + ! use memcpy() instead of hb_strncpy() to avoid problems when + there is no place for tailing 0 + 2008-08-25 19:49 UTC+0200 Viktor Szakats (harbour.01 syenar hu) * include/hbsetup.h ! Fixed problem where Darwin autodetection went wrong diff --git a/harbour/contrib/examples/pp/hbppcore.c b/harbour/contrib/examples/pp/hbppcore.c index b9ea2aa531..29e4c946ba 100644 --- a/harbour/contrib/examples/pp/hbppcore.c +++ b/harbour/contrib/examples/pp/hbppcore.c @@ -2178,7 +2178,7 @@ static int WorkMarkers( char **ptrmp, char **ptri, char *ptro, int *lenres, BOOL maxlenreal = HB_PP_STR_SIZE; if( s_expreal == NULL ) - s_expreal = ( char * ) hb_xgrab( maxlenreal ); + s_expreal = ( char * ) hb_xgrab( maxlenreal + 1 ); /* Copying a match pattern to 'exppatt' */ lenpatt = stroncpy( exppatt, *ptrmp, 4 ); @@ -2376,7 +2376,7 @@ static int WorkMarkers( char **ptrmp, char **ptri, char *ptro, int *lenres, BOOL if( ! com_or_tra ) { /* translate */ - hb_strncpy( s_expreal + 1, *ptri, lenreal ); + hb_strncpy( s_expreal + 1, *ptri, HB_PP_STR_SIZE - 1 ); s_expreal[0] = '&'; s_expreal[lenreal + 1] = '\0'; *ptri += lenreal; @@ -2406,7 +2406,7 @@ static int WorkMarkers( char **ptrmp, char **ptri, char *ptro, int *lenres, BOOL * is preprocessed into: * &a ++( b ) */ - hb_strncpy( s_expreal + 1, *ptri, lenreal ); + hb_strncpy( s_expreal + 1, *ptri, HB_PP_STR_SIZE - 1 ); s_expreal[0] = '&'; s_expreal[lenreal + 1] = '\0'; *ptri += lenreal; diff --git a/harbour/contrib/hbfbird/firebird.c b/harbour/contrib/hbfbird/firebird.c index 57a27e766a..828a67229f 100644 --- a/harbour/contrib/hbfbird/firebird.c +++ b/harbour/contrib/hbfbird/firebird.c @@ -117,11 +117,15 @@ HB_FUNC( FBCONNECT ) dpb[ i++ ] = isc_dpb_version1; dpb[ i++ ] = isc_dpb_user_name; len = strlen( user ); + if( len > ( int ) ( sizeof( dpb ) - i - 4 ) ) + len = ( int ) ( sizeof( dpb ) - i - 4 ); dpb[ i++ ] = ( char ) len; hb_strncpy( &( dpb[ i ] ), user, len ); i += len; dpb[ i++ ] = isc_dpb_password; len = strlen( passwd ); + if( len > ( int ) ( sizeof( dpb ) - i - 2 ) ) + len = ( int ) ( sizeof( dpb ) - i - 2 ); dpb[ i++ ] = len; hb_strncpy( &( dpb[ i ] ), passwd, len ); i += len; diff --git a/harbour/contrib/hbnf/getenvrn.c b/harbour/contrib/hbnf/getenvrn.c index 9ac39a1a18..de6551cb48 100644 --- a/harbour/contrib/hbnf/getenvrn.c +++ b/harbour/contrib/hbnf/getenvrn.c @@ -151,7 +151,7 @@ HB_FUNC( FT_GETE ) /* add 1 more byte for final nul character */ buffsize++; /* now allocate that much memory and make sure 1st byte is a nul */ - buffer = ( char * ) hb_xalloc( buffsize + 1 ); + buffer = ( char * ) hb_xgrab( buffsize + 1 ); buffer[0] = '\0'; } @@ -218,7 +218,7 @@ HB_FUNC( FT_GETE ) buffsize++; /* now allocate that much memory and make sure 1st byte is a nul */ - buffer = ( char * ) hb_xalloc( buffsize + 1 ); + buffer = ( char * ) hb_xgrab( buffsize + 1 ); buffer[0] = '\0'; } x = 0; diff --git a/harbour/contrib/hbw32/w32_ole.c b/harbour/contrib/hbw32/w32_ole.c index bbcc97fbe8..50abc3ec9a 100644 --- a/harbour/contrib/hbw32/w32_ole.c +++ b/harbour/contrib/hbw32/w32_ole.c @@ -1605,6 +1605,7 @@ static void OleThrowError( void ) { PHB_ITEM pReturn; char *sDescription; + BOOL fFree = FALSE; hb_vmPushSymbol( hb_dynsymSymbol( s_pSym_cClassName ) ); hb_vmPush( hb_stackSelfItem() ); @@ -1612,11 +1613,8 @@ static void OleThrowError( void ) if( s_nOleError == DISP_E_EXCEPTION ) { - // Intentional to avoid report of memory leak if fatal error. - char * sTemp = hb_oleWideToAnsi( excep.bstrDescription ); - sDescription = ( char * ) malloc( strlen( sTemp ) + 1 ); - hb_strncpy( sDescription, sTemp, strlen( sTemp ) ); - hb_xfree( sTemp ); + sDescription = hb_oleWideToAnsi( excep.bstrDescription ); + fFree = TRUE; } else sDescription = Ole2TxtError(); @@ -1625,9 +1623,9 @@ static void OleThrowError( void ) pReturn = hb_errRT_SubstParams( hb_parcx( -1 ), EG_OLEEXECPTION, (ULONG) s_nOleError, sDescription, hb_itemGetSymbol( hb_stackBaseItem() )->szName ); - if( s_nOleError == DISP_E_EXCEPTION ) + if( fFree ) { - free( ( void * ) sDescription ); + hb_xfree( ( void * ) sDescription ); } if( pReturn ) diff --git a/harbour/contrib/hbziparch/hbzipnew.cpp b/harbour/contrib/hbziparch/hbzipnew.cpp index 322f0c0e1a..5a16f7773a 100644 --- a/harbour/contrib/hbziparch/hbzipnew.cpp +++ b/harbour/contrib/hbziparch/hbzipnew.cpp @@ -875,8 +875,7 @@ const char * hb_GetZipComment( char * szFile ) else szReturn = ""; - szTempR = ( char * ) hb_xgrab( strlen( ( const char * ) szReturn ) + 1 ); - hb_strncpy( szTempR, ( char * ) szReturn, strlen( ( const char * ) szReturn ) ); + szTempR = hb_strdup( szReturn ); szZip.Close(); diff --git a/harbour/source/rdd/dbfntx/dbfntx1.c b/harbour/source/rdd/dbfntx/dbfntx1.c index de3fb8efc5..09c6b68cd3 100644 --- a/harbour/source/rdd/dbfntx/dbfntx1.c +++ b/harbour/source/rdd/dbfntx/dbfntx1.c @@ -1588,7 +1588,7 @@ static LPTAGINFO hb_ntxTagLoad( LPNTXINDEX pIndex, ULONG ulBlock, static void hb_ntxIndexTagAdd( LPNTXINDEX pIndex, LPTAGINFO pTag ) { LPCTXHEADER lpCTX = ( LPCTXHEADER ) pIndex->HeaderBuff; - int iTags = HB_GET_LE_UINT16( lpCTX->ntags ), i; + int iTags = HB_GET_LE_UINT16( lpCTX->ntags ), iLen, i; LPCTXTAGITEM pTagItem = ( LPCTXTAGITEM ) lpCTX->tags; for( i = 0; i < iTags; pTagItem++, i++ ) @@ -1600,7 +1600,11 @@ static void hb_ntxIndexTagAdd( LPNTXINDEX pIndex, LPTAGINFO pTag ) { ++iTags; HB_PUT_LE_UINT16( lpCTX->ntags, iTags ); - hb_strncpy( ( char * ) pTagItem->tag_name, pTag->TagName, NTX_MAX_TAGNAME ); + iLen = ( int ) strlen( pTag->TagName ); + if( iLen > NTX_MAX_TAGNAME ) + iLen = NTX_MAX_TAGNAME; + memcpy( pTagItem->tag_name, pTag->TagName, iLen ); + memset( pTagItem->tag_name + iLen, 0, sizeof( pTagItem->tag_name ) - iLen ); } HB_PUT_LE_UINT32( pTagItem->tag_header, pTag->HeadBlock ); pIndex->Update = TRUE; @@ -1652,7 +1656,7 @@ static ERRCODE hb_ntxTagHeaderSave( LPTAGINFO pTag ) { LPNTXINDEX pIndex = pTag->Owner; NTXHEADER Header; - int iSize = 12, type, version = 0; + int iSize = 12, type, version = 0, iLen; ULONG next = 0; if( pIndex->Compound ) @@ -1708,11 +1712,24 @@ static ERRCODE hb_ntxTagHeaderSave( LPTAGINFO pTag ) Header.unique[0] = pTag->UniqueKey ? 1 : 0; Header.descend[0] = pTag->AscendKey ? 0 : 1; Header.custom[0] = pTag->Custom ? 1 : 0; - hb_strncpy( ( char * ) Header.key_expr, pTag->KeyExpr, NTX_MAX_EXP ); + iLen = ( int ) strlen( pTag->KeyExpr ); + if( iLen > NTX_MAX_EXP ) + iLen = NTX_MAX_EXP; + memcpy( Header.key_expr, pTag->KeyExpr, iLen ); if( pTag->ForExpr ) - hb_strncpy( ( char * ) Header.for_expr, pTag->ForExpr, NTX_MAX_EXP ); + { + iLen = ( int ) strlen( pTag->ForExpr ); + if( iLen > NTX_MAX_EXP ) + iLen = NTX_MAX_EXP; + memcpy( Header.for_expr, pTag->ForExpr, iLen ); + } if( pTag->fTagName ) - hb_strncpy( ( char * ) Header.tag_name, pTag->TagName, NTX_MAX_TAGNAME ); + { + iLen = ( int ) strlen( pTag->TagName ); + if( iLen > NTX_MAX_TAGNAME ) + iLen = NTX_MAX_TAGNAME; + memcpy( Header.tag_name, pTag->TagName, iLen ); + } iSize = sizeof( NTXHEADER ); }