From 23221548b4e6ad9f14e75bf193203728b3845bb5 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Sat, 23 Aug 2008 10:43:25 +0000 Subject: [PATCH] 2008-08-23 11:38 UTC+0200 Viktor Szakats (harbour.01 syenar hu) * source/pp/hbpp.c * source/vm/cmdarg.c * source/vm/memvars.c * source/rtl/dates.c * source/rtl/fstemp.c * source/rtl/gtchrmap.c * source/rtl/gtcrs/gtcrs.c * source/rtl/gtpca/gtpca.c * source/rtl/gtstd/gtstd.c * source/rdd/dbf1.c * source/rdd/dbfntx/dbfntx1.c * contrib/hbct/disk.c * contrib/hbfbird/firebird.c * contrib/hbw32/w32_ole.c * contrib/xhb/hboutdbg.c * contrib/examples/pp/hbppcore.c * strncpy() -> hb_strncpy() * strncat() -> hb_strncat() ! In most of these places the terminating zero was not explicitly set by the caller before. * contrib/hbnf/getenvrn.c * contrib/hbw32/dllcall.c * contrib/hbw32/w32_ole.c * contrib/hbwhat32/_wincdlg.c * contrib/hbwhat32/_windll.c * contrib/hbwhat32/_winmain.c * contrib/hbziparch/hbzipnew.cpp ! strcpy() -> hb_strncpy() ! strcat() -> hb_strncat() ; TOFIX1: There are still two strcpy()s left in SQL_SPRINTF(). Xavi, could you pls take a look at it? ; TOFIX2: There are still a few strcpy()s in hbwince.c and dbgentry.c. * source/pp/hbpp.c * source/rtl/gtcrs/gtcrs.c * source/rtl/gtpca/gtpca.c * source/rtl/gtstd/gtstd.c * contrib/hbfbird/firebird.c * contrib/hbwhat32/_wincdlg.c * contrib/hbwhat32/_windll.c ! Fixed wrong buffer sizes (or no buffer limits) in some string copy operations (potential buffer overruns). * contrib/hbnf/getenvrn.c ! Fixed bad leak in FT_GETE() on Win32 systems. * source/rtl/fstemp.c ! hb_fsTempName(): Fixed potential buffer overrun by one byte. * Minor formatting. * source/rtl/fstemp.c * source/rtl/hbffind.c * contrib/xhb/hboutdbg.c * Using sizeof() instead of explicit size in string copy operations. ; TODO: There is a lot more to fix like this. ; Przemek, could you double check these changes pls? I can send you the .diff if it helps. --- harbour/ChangeLog | 62 +++++++++++++++++++++++++- harbour/contrib/examples/pp/hbppcore.c | 16 +++---- harbour/contrib/hbct/disk.c | 6 +-- harbour/contrib/hbfbird/firebird.c | 6 +-- harbour/contrib/hbnf/getenvrn.c | 33 +++++++------- harbour/contrib/hbw32/dllcall.c | 4 +- harbour/contrib/hbw32/w32_ole.c | 6 +-- harbour/contrib/hbwhat32/_wincdlg.c | 12 ++--- harbour/contrib/hbwhat32/_windll.c | 10 ++--- harbour/contrib/hbwhat32/_winmain.c | 2 +- harbour/contrib/hbziparch/hbzipnew.cpp | 2 +- harbour/contrib/xhb/hboutdbg.c | 2 +- harbour/source/pp/hbpp.c | 4 +- harbour/source/rdd/dbf1.c | 6 +-- harbour/source/rdd/dbfntx/dbfntx1.c | 8 ++-- harbour/source/rtl/dates.c | 2 +- harbour/source/rtl/fstemp.c | 18 ++++---- harbour/source/rtl/gtchrmap.c | 7 ++- harbour/source/rtl/gtcrs/gtcrs.c | 19 ++++---- harbour/source/rtl/gtpca/gtpca.c | 8 ++-- harbour/source/rtl/gtstd/gtstd.c | 8 ++-- harbour/source/rtl/hbffind.c | 8 ++-- harbour/source/vm/cmdarg.c | 3 +- harbour/source/vm/memvars.c | 3 +- 24 files changed, 154 insertions(+), 101 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 5841f1b662..517d509359 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,11 +8,71 @@ 2008-12-31 13:59 UTC+0100 Foo Bar */ +2008-08-23 11:38 UTC+0200 Viktor Szakats (harbour.01 syenar hu) + * source/pp/hbpp.c + * source/vm/cmdarg.c + * source/vm/memvars.c + * source/rtl/dates.c + * source/rtl/fstemp.c + * source/rtl/gtchrmap.c + * source/rtl/gtcrs/gtcrs.c + * source/rtl/gtpca/gtpca.c + * source/rtl/gtstd/gtstd.c + * source/rdd/dbf1.c + * source/rdd/dbfntx/dbfntx1.c + * contrib/hbct/disk.c + * contrib/hbfbird/firebird.c + * contrib/hbw32/w32_ole.c + * contrib/xhb/hboutdbg.c + * contrib/examples/pp/hbppcore.c + * strncpy() -> hb_strncpy() + * strncat() -> hb_strncat() + ! In most of these places the terminating zero + was not explicitly set by the caller before. + + * contrib/hbnf/getenvrn.c + * contrib/hbw32/dllcall.c + * contrib/hbw32/w32_ole.c + * contrib/hbwhat32/_wincdlg.c + * contrib/hbwhat32/_windll.c + * contrib/hbwhat32/_winmain.c + * contrib/hbziparch/hbzipnew.cpp + ! strcpy() -> hb_strncpy() + ! strcat() -> hb_strncat() + ; TOFIX1: There are still two strcpy()s left in SQL_SPRINTF(). + Xavi, could you pls take a look at it? + ; TOFIX2: There are still a few strcpy()s in hbwince.c and + dbgentry.c. + + * source/pp/hbpp.c + * source/rtl/gtcrs/gtcrs.c + * source/rtl/gtpca/gtpca.c + * source/rtl/gtstd/gtstd.c + * contrib/hbfbird/firebird.c + * contrib/hbwhat32/_wincdlg.c + * contrib/hbwhat32/_windll.c + ! Fixed wrong buffer sizes (or no buffer limits) in some + string copy operations (potential buffer overruns). + + * contrib/hbnf/getenvrn.c + ! Fixed bad leak in FT_GETE() on Win32 systems. + + * source/rtl/fstemp.c + ! hb_fsTempName(): Fixed potential buffer overrun by one byte. + * Minor formatting. + + * source/rtl/fstemp.c + * source/rtl/hbffind.c + * contrib/xhb/hboutdbg.c + * Using sizeof() instead of explicit size in string copy operations. + ; TODO: There is a lot more to fix like this. + 2008-08-22 22:51 UTC+0200 Viktor Szakats (harbour.01 syenar hu) * utils/hbtest/rt_array.prg + Added failing test case of doing AEval() and resizing the evaluated array in the eval codeblock. + ; TOFIX * utils/hbdoc/ft_funcs.prg * utils/hbdoc/hbdoc.prg @@ -25,7 +85,7 @@ * contrib/xhb/Makefile * contrib/xhb/common.mak + contrib/xhb/arrayblk.prg - + Added HB_ARRAYBLOCK() + + Added HB_ARRAYBLOCK() from xhb. 2008-08-22 17:03 UTC+0200 Viktor Szakats (harbour.01 syenar hu) * contrib/make_gcc_all.sh diff --git a/harbour/contrib/examples/pp/hbppcore.c b/harbour/contrib/examples/pp/hbppcore.c index 323364e70a..b9ea2aa531 100644 --- a/harbour/contrib/examples/pp/hbppcore.c +++ b/harbour/contrib/examples/pp/hbppcore.c @@ -2376,13 +2376,13 @@ static int WorkMarkers( char **ptrmp, char **ptri, char *ptro, int *lenres, BOOL if( ! com_or_tra ) { /* translate */ - strncpy( s_expreal + 1, *ptri, lenreal ); - s_expreal[0] = '&'; - s_expreal[lenreal + 1] = '\0'; - *ptri += lenreal; - SearnRep( exppatt, s_expreal, lenreal + 1, ptro, lenres ); - rezrestr = 1; - break; + hb_strncpy( s_expreal + 1, *ptri, lenreal ); + s_expreal[0] = '&'; + s_expreal[lenreal + 1] = '\0'; + *ptri += lenreal; + SearnRep( exppatt, s_expreal, lenreal + 1, ptro, lenres ); + rezrestr = 1; + break; } else { @@ -2406,7 +2406,7 @@ static int WorkMarkers( char **ptrmp, char **ptri, char *ptro, int *lenres, BOOL * is preprocessed into: * &a ++( b ) */ - strncpy( s_expreal + 1, *ptri, lenreal ); + hb_strncpy( s_expreal + 1, *ptri, lenreal ); s_expreal[0] = '&'; s_expreal[lenreal + 1] = '\0'; *ptri += lenreal; diff --git a/harbour/contrib/hbct/disk.c b/harbour/contrib/hbct/disk.c index 31366ac7e0..a8d8c60b8d 100644 --- a/harbour/contrib/hbct/disk.c +++ b/harbour/contrib/hbct/disk.c @@ -217,12 +217,12 @@ HB_FUNC( VOLUME ) { if( fname->szPath ) { - strncpy( sRootBuf, fname->szPath, 3 ); + hb_strncpy( sRootBuf, fname->szPath, 3 ); sRoot = sRootBuf; } if( fname->szName ) { - strncpy( sVolNameBuf, fname->szName, 11 ); + hb_strncpy( sVolNameBuf, fname->szName, 11 ); sVolName = sVolNameBuf; } @@ -230,7 +230,7 @@ HB_FUNC( VOLUME ) } else { - strncpy( sVolNameBuf, ( char * ) sDiskName, 11 ); + hb_strncpy( sVolNameBuf, ( char * ) sDiskName, 11 ); sVolName = sVolNameBuf; } if( fFree ) diff --git a/harbour/contrib/hbfbird/firebird.c b/harbour/contrib/hbfbird/firebird.c index 518158dd7b..57a27e766a 100644 --- a/harbour/contrib/hbfbird/firebird.c +++ b/harbour/contrib/hbfbird/firebird.c @@ -118,12 +118,12 @@ HB_FUNC( FBCONNECT ) dpb[ i++ ] = isc_dpb_user_name; len = strlen( user ); dpb[ i++ ] = ( char ) len; - strncpy( &( dpb[ i ] ), user, len ); + hb_strncpy( &( dpb[ i ] ), user, len ); i += len; dpb[ i++ ] = isc_dpb_password; len = strlen( passwd ); dpb[ i++ ] = len; - strncpy( &( dpb[ i ] ), passwd, len ); + hb_strncpy( &( dpb[ i ] ), passwd, len ); i += len; if( isc_attach_database( status, 0, db_connect, &db, i, dpb ) ) @@ -253,7 +253,7 @@ HB_FUNC( FBQUERY ) PHB_ITEM aTemp; PHB_ITEM aNew; - strncpy( sel_str, hb_parcx( 2 ), sizeof( sel_str ) ); + hb_strncpy( sel_str, hb_parcx( 2 ), sizeof( sel_str ) - 1 ); if( ISPOINTER( 4 ) ) { diff --git a/harbour/contrib/hbnf/getenvrn.c b/harbour/contrib/hbnf/getenvrn.c index 4c4bc7a6dd..9ac39a1a18 100644 --- a/harbour/contrib/hbnf/getenvrn.c +++ b/harbour/contrib/hbnf/getenvrn.c @@ -164,9 +164,9 @@ HB_FUNC( FT_GETE ) if( rettype == CHARTYPE ) { /* tack string onto end of buffer */ - strcat( buffer, environ[x] ); + hb_strncat( buffer, environ[x], buffsize ); /* add crlf at end of each string */ - strcat( buffer, CRLF ); + hb_strncat( buffer, CRLF, buffsize ); } else if( rettype == ARRAYTYPE ) /* store string to next array element */ @@ -204,29 +204,26 @@ HB_FUNC( FT_GETE ) { for( sCurEnv = ( LPSTR ) lpEnviron; *sCurEnv; sCurEnv++ ) { + if( !*sCurEnv ) + /* null string, we're done */ + break; - { - if( !*sCurEnv ) - /* null string, we're done */ - break; - /* add length of this string plus 2 for the crlf */ - buffsize += ( strlen( ( char * ) sCurEnv ) + 2 ); - } - /* add 1 more byte for final nul character */ - buffsize++; + /* add length of this string plus 2 for the crlf */ + buffsize += ( strlen( ( char * ) sCurEnv ) + 2 ); - /* now allocate that much memory and make sure 1st byte is a nul */ - buffer = ( char * ) hb_xalloc( buffsize ); - strcpy( buffer, "\0" ); while( *sCurEnv ) sCurEnv++; } + /* 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[0] = '\0'; } x = 0; for( sCurEnv = ( LPSTR ) lpEnviron; *sCurEnv; sCurEnv++ ) { - - if( !*sCurEnv ) /* null string, we're done */ break; @@ -234,9 +231,9 @@ HB_FUNC( FT_GETE ) if( rettype == CHARTYPE ) { /* tack string onto end of buffer */ - strcat( buffer, ( char * ) sCurEnv ); + hb_strncat( buffer, ( char * ) sCurEnv, buffsize ); /* add crlf at end of each string */ - strcat( buffer, CRLF ); + hb_strncat( buffer, CRLF, buffsize ); } if( rettype == ARRAYTYPE ) diff --git a/harbour/contrib/hbw32/dllcall.c b/harbour/contrib/hbw32/dllcall.c index 3ce1c05797..d385ea9f9e 100644 --- a/harbour/contrib/hbw32/dllcall.c +++ b/harbour/contrib/hbw32/dllcall.c @@ -755,7 +755,7 @@ HB_FUNC( DLLPREPARECALL ) xec->lpFunc = ( LPVOID ) GetProcAddress( xec->hDLL, xec->cProc ? ( LPCSTR ) xec->cProc : ( LPCSTR ) ( HB_PTRDIFF ) xec->wOrdinal ); if( ! xec->lpFunc && xec->cProc ) /* try with ANSI suffix? */ - xec->lpFunc = ( LPVOID ) GetProcAddress( xec->hDLL, ( LPCSTR ) strcat( xec->cProc, "A" ) ); + xec->lpFunc = ( LPVOID ) GetProcAddress( xec->hDLL, ( LPCSTR ) hb_strncat( xec->cProc, "A", hb_parclen( 3 ) + 1 ) ); if( xec->lpFunc ) { @@ -810,7 +810,7 @@ static LPVOID hb_getprocaddress( HMODULE hDLL, int i ) { char * pszFuncName = ( char * ) hb_xgrab( hb_parclen( i ) + 2 ); hb_strncpy( pszFuncName, hb_parc( i ), hb_parclen( i ) ); - lpFunction = ( LPVOID ) GetProcAddress( hDLL, strcat( pszFuncName, "A" ) ); + lpFunction = ( LPVOID ) GetProcAddress( hDLL, hb_strncat( pszFuncName, "A", hb_parclen( i ) + 1 ) ); hb_xfree( pszFuncName ); } diff --git a/harbour/contrib/hbw32/w32_ole.c b/harbour/contrib/hbw32/w32_ole.c index 4c5a8165fc..0689d06602 100644 --- a/harbour/contrib/hbw32/w32_ole.c +++ b/harbour/contrib/hbw32/w32_ole.c @@ -1660,7 +1660,7 @@ static void OleThrowError( void ) // Intentional to avoid report of memory leak if fatal error. char *sTemp = hb_oleWideToAnsi( excep.bstrDescription ); sDescription = (char *) malloc( strlen( sTemp ) + 1 ); - strcpy( sDescription, sTemp ); + hb_strncpy( sDescription, sTemp, strlen( sTemp ) ); hb_xfree( sTemp ); } else @@ -1967,9 +1967,9 @@ OleGetID: sOleClassName = (char *) hb_xgrab( iClassNameLen + 1 + iMsgNameLen + 1 ); - strncpy( sOleClassName, hb_parc( - 1 ), iClassNameLen ); + hb_strncpy( sOleClassName, hb_parc( - 1 ), iClassNameLen ); sOleClassName[ iClassNameLen ] = ':'; - strcpy( sOleClassName + iClassNameLen + 1, hb_itemGetSymbol( hb_stackBaseItem() )->szName ); + hb_strncpy( sOleClassName + iClassNameLen + 1, hb_itemGetSymbol( hb_stackBaseItem() )->szName, iMsgNameLen ); //HB_TRACE(HB_TR_INFO, ("Class: '%s'\n", sOleClassName)); diff --git a/harbour/contrib/hbwhat32/_wincdlg.c b/harbour/contrib/hbwhat32/_wincdlg.c index 2e27c04f0e..5428e076b8 100644 --- a/harbour/contrib/hbwhat32/_wincdlg.c +++ b/harbour/contrib/hbwhat32/_wincdlg.c @@ -197,9 +197,9 @@ HB_FUNC( CHOOSECOLOR ) HB_FUNC( _GETOPENFILENAME ) { OPENFILENAME ofn; - char *szFileName =(char*) hb_xgrab( hb_parcsiz(2)); + char *szFileName = ( char * ) hb_xgrab( hb_parclen( 2 ) + 1 ); - strcpy( szFileName, hb_parcx( 2 ) ); + hb_strncpy( szFileName, hb_parcx( 2 ), hb_parclen( 2 ) ); ZeroMemory( &ofn, sizeof(ofn) ); ofn.hInstance = GetModuleHandle(NULL) ; @@ -236,10 +236,10 @@ HB_FUNC( _GETOPENFILENAME ) HB_FUNC( _GETSAVEFILENAME ) { OPENFILENAME ofn; - char szFileName[MAX_PATH+1] ; - strcpy( szFileName, hb_parc (2) ); - ZeroMemory(&ofn, sizeof(ofn)); - ofn.hInstance = GetModuleHandle(NULL) ; + char szFileName[ MAX_PATH + 1 ]; + hb_strncpy( szFileName, hb_parc( 2 ), MAX_PATH ); + ZeroMemory( &ofn, sizeof( ofn ) ); + ofn.hInstance = GetModuleHandle(NULL); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = ISNIL (1) ? GetActiveWindow() : (HWND) hb_parnl(1); ofn.lpstrTitle = hb_parc (3); diff --git a/harbour/contrib/hbwhat32/_windll.c b/harbour/contrib/hbwhat32/_windll.c index 3fb82f6657..7a40192e8e 100644 --- a/harbour/contrib/hbwhat32/_windll.c +++ b/harbour/contrib/hbwhat32/_windll.c @@ -84,7 +84,7 @@ HB_FUNC( FREELIBRARYANDEXITTHREAD ) HB_FUNC( GETPROCADDRESS ) { ULONG dwProcAddr; - char cFuncName[MAX_PATH]; + char cFuncName[ MAX_PATH + 1 ]; if ((dwProcAddr = (ULONG) GetProcAddress( (HMODULE) hb_parnl(1), ISCHAR( 2 ) ? (LPCSTR) hb_parcx(2) : @@ -92,10 +92,10 @@ HB_FUNC( GETPROCADDRESS ) { if ( ISCHAR( 2 ) ) { - // try forced ANSI flavour ? - strcpy(cFuncName, hb_parcx(2)); - strcat(cFuncName, "A"); - dwProcAddr = (ULONG) GetProcAddress((HMODULE) hb_parnl(1), cFuncName); + // try forced ANSI flavour ? + hb_strncpy( cFuncName, hb_parcx( 2 ), sizeof( cFuncName ) - 2 ); + hb_strncat( cFuncName, "A", sizeof( cFuncName ) - 1 ); + dwProcAddr = (ULONG) GetProcAddress((HMODULE) hb_parnl(1), cFuncName); } } diff --git a/harbour/contrib/hbwhat32/_winmain.c b/harbour/contrib/hbwhat32/_winmain.c index 1fa249a50f..6943138f12 100644 --- a/harbour/contrib/hbwhat32/_winmain.c +++ b/harbour/contrib/hbwhat32/_winmain.c @@ -76,7 +76,7 @@ int WINAPI WinMain( HINSTANCE hInstance, /* handle to current instance */ hb_hPrevInstance = hPrevInstance; hb_iCmdShow = iCmdShow; - strcpy( pArgs, lpCmdLine ); + hb_strncpy( pArgs, lpCmdLine, strlen( lpCmdLine ) ); HB_TRACE(HB_TR_DEBUG, ("WinMain(%p, %p, %s, %d)", hInstance, hPrevInstance, lpCmdLine, iCmdShow)); diff --git a/harbour/contrib/hbziparch/hbzipnew.cpp b/harbour/contrib/hbziparch/hbzipnew.cpp index 747f0ed7dd..322f0c0e1a 100644 --- a/harbour/contrib/hbziparch/hbzipnew.cpp +++ b/harbour/contrib/hbziparch/hbzipnew.cpp @@ -876,7 +876,7 @@ const char * hb_GetZipComment( char * szFile ) szReturn = ""; szTempR = ( char * ) hb_xgrab( strlen( ( const char * ) szReturn ) + 1 ); - strcpy( szTempR, ( char * ) szReturn ); + hb_strncpy( szTempR, ( char * ) szReturn, strlen( ( const char * ) szReturn ) ); szZip.Close(); diff --git a/harbour/contrib/xhb/hboutdbg.c b/harbour/contrib/xhb/hboutdbg.c index b119aa8542..7f1c393f05 100644 --- a/harbour/contrib/xhb/hboutdbg.c +++ b/harbour/contrib/xhb/hboutdbg.c @@ -146,7 +146,7 @@ HB_FUNC( HB_OUTDEBUGNAME ) if ( s_iDebugFd == 0 && pName != NULL) { - strncpy( s_szDebugName, hb_itemGetCPtr( pName ), 127 ); + hb_strncpy( s_szDebugName, hb_itemGetCPtr( pName ), sizeof( s_szDebugName ) - 1 ); s_iUseDebugName = 1; hb_retl( TRUE ); diff --git a/harbour/source/pp/hbpp.c b/harbour/source/pp/hbpp.c index 876d44a993..56750600d4 100644 --- a/harbour/source/pp/hbpp.c +++ b/harbour/source/pp/hbpp.c @@ -171,11 +171,11 @@ static int hb_pp_writeRules( FILE * fout, PHB_PP_RULE pFirst, char * szName ) if( pRule->pMatch ) snprintf( szMatch, sizeof( szMatch ), "s_%cm%03d", szName[0], iRule ); else - strncpy( szMatch, "NULL ", sizeof( szResult ) ); + hb_strncpy( szMatch, "NULL ", sizeof( szMatch ) - 1 ); if( pRule->pResult ) snprintf( szResult, sizeof( szResult ), "s_%cr%03d", szName[0], iRule ); else - strncpy( szResult, "NULL ", sizeof( szResult ) ); + hb_strncpy( szResult, "NULL ", sizeof( szResult ) - 1 ); ulRepeatBits = 0; for( u = 0, ulBit = 1; u < pRule->markers; ++u, ulBit <<= 1 ) diff --git a/harbour/source/rdd/dbf1.c b/harbour/source/rdd/dbf1.c index cdaa5535eb..e5c9052749 100644 --- a/harbour/source/rdd/dbf1.c +++ b/harbour/source/rdd/dbf1.c @@ -2749,7 +2749,7 @@ static ERRCODE hb_dbfCreate( DBFAREAP pArea, LPDBOPENINFO pCreateInfo ) } else { - hb_strncpy( ( char * ) szFileName, ( char * ) pCreateInfo->abName, _POSIX_PATH_MAX ); + hb_strncpy( ( char * ) szFileName, ( char * ) pCreateInfo->abName, sizeof( szFileName ) - 1 ); } hb_xfree( pFileName ); @@ -2883,8 +2883,8 @@ static ERRCODE hb_dbfCreate( DBFAREAP pArea, LPDBOPENINFO pCreateInfo ) for( uiCount = 0; uiCount < pArea->uiFieldCount; uiCount++ ) { LPFIELD pField = pArea->lpFields + uiCount; - strncpy( ( char * ) pThisField->bName, - hb_dynsymName( ( PHB_DYNS ) pField->sym ), 10 ); + hb_strncpy( ( char * ) pThisField->bName, + hb_dynsymName( ( PHB_DYNS ) pField->sym ), 10 ); pArea->pFieldOffset[ uiCount ] = pArea->uiRecordLen; /* field offset */ if( pArea->bTableType == DB_DBF_VFP ) diff --git a/harbour/source/rdd/dbfntx/dbfntx1.c b/harbour/source/rdd/dbfntx/dbfntx1.c index db5d3804ec..7202c11a97 100644 --- a/harbour/source/rdd/dbfntx/dbfntx1.c +++ b/harbour/source/rdd/dbfntx/dbfntx1.c @@ -1600,7 +1600,7 @@ static void hb_ntxIndexTagAdd( LPNTXINDEX pIndex, LPTAGINFO pTag ) { ++iTags; HB_PUT_LE_UINT16( lpCTX->ntags, iTags ); - strncpy( ( char * ) pTagItem->tag_name, pTag->TagName, NTX_MAX_TAGNAME ); + hb_strncpy( ( char * ) pTagItem->tag_name, pTag->TagName, NTX_MAX_TAGNAME ); } HB_PUT_LE_UINT32( pTagItem->tag_header, pTag->HeadBlock ); pIndex->Update = TRUE; @@ -1708,11 +1708,11 @@ 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; - strncpy( ( char * ) Header.key_expr, pTag->KeyExpr, NTX_MAX_EXP ); + hb_strncpy( ( char * ) Header.key_expr, pTag->KeyExpr, NTX_MAX_EXP ); if( pTag->ForExpr ) - strncpy( ( char * ) Header.for_expr, pTag->ForExpr, NTX_MAX_EXP ); + hb_strncpy( ( char * ) Header.for_expr, pTag->ForExpr, NTX_MAX_EXP ); if( pTag->fTagName ) - strncpy( ( char * ) Header.tag_name, pTag->TagName, NTX_MAX_TAGNAME ); + hb_strncpy( ( char * ) Header.tag_name, pTag->TagName, NTX_MAX_TAGNAME ); iSize = sizeof( NTXHEADER ); } diff --git a/harbour/source/rtl/dates.c b/harbour/source/rtl/dates.c index 7e1687b149..c5dc97e451 100644 --- a/harbour/source/rtl/dates.c +++ b/harbour/source/rtl/dates.c @@ -218,7 +218,7 @@ HB_EXPORT char * hb_dateFormat( const char * szDate, char * szFormattedDate, con { /* Not a valid date string, so return a blank date with separators */ format_count = size; /* size is either 8 or 10 */ - strncpy( szFormattedDate, szDateFormat, size ); + hb_strncpy( szFormattedDate, szDateFormat, size ); for( digit_count = 0; digit_count < size; digit_count++ ) { diff --git a/harbour/source/rtl/fstemp.c b/harbour/source/rtl/fstemp.c index 50fa56f8ef..4836813b46 100644 --- a/harbour/source/rtl/fstemp.c +++ b/harbour/source/rtl/fstemp.c @@ -73,9 +73,9 @@ static BOOL hb_fsTempName( BYTE * pszBuffer, const BYTE * pszDir, const BYTE * p char cTempDir[ _POSIX_PATH_MAX + 1 ]; - if( pszDir != NULL && pszDir[0] != '\0' ) + if( pszDir != NULL && pszDir[ 0 ] != '\0' ) { - strncpy( (char *) cTempDir, (const char *) pszDir, _POSIX_PATH_MAX + 1 ); + hb_strncpy( ( char * ) cTempDir, ( const char * ) pszDir, sizeof( cTempDir ) - 1 ); } else { @@ -174,9 +174,9 @@ HB_EXPORT FHANDLE hb_fsCreateTemp( const BYTE * pszDir, const BYTE * pszPrefix, do { - pszName[0] = '\0'; + pszName[ 0 ] = '\0'; - if( pszDir != NULL && pszDir[0] != '\0' ) + if( pszDir != NULL && pszDir[ 0 ] != '\0' ) { hb_strncpy( ( char * ) pszName, ( char * ) pszDir, _POSIX_PATH_MAX ); } @@ -190,8 +190,8 @@ HB_EXPORT FHANDLE hb_fsCreateTemp( const BYTE * pszDir, const BYTE * pszPrefix, if( !fsGetTempDirByCase( pszName, P_tmpdir ) ) #endif { - pszName[0] = '.'; - pszName[1] = '\0'; + pszName[ 0 ] = '.'; + pszName[ 1 ] = '\0'; } } if( pszTmpDir ) @@ -222,8 +222,8 @@ HB_EXPORT FHANDLE hb_fsCreateTemp( const BYTE * pszDir, const BYTE * pszPrefix, hb_set.HB_SET_DIRCASE != HB_SET_CASE_UPPER ) { hb_strncat( ( char * ) pszName, "XXXXXX", _POSIX_PATH_MAX ); - fd = (FHANDLE) mkstemp( ( char * ) pszName ); - hb_fsSetIOError( fd != (FHANDLE) -1, 0 ); + fd = ( FHANDLE ) mkstemp( ( char * ) pszName ); + hb_fsSetIOError( fd != ( FHANDLE ) -1, 0 ); } else #endif @@ -242,7 +242,7 @@ HB_EXPORT FHANDLE hb_fsCreateTemp( const BYTE * pszDir, const BYTE * pszPrefix, fd = hb_fsCreateEx( pszName, ulAttr, FO_EXCLUSIVE | FO_EXCL ); } - if( fd != (FHANDLE) FS_ERROR ) + if( fd != ( FHANDLE ) FS_ERROR ) return fd; } while( --iAttemptLeft ); diff --git a/harbour/source/rtl/gtchrmap.c b/harbour/source/rtl/gtchrmap.c index 269251d4c3..d20b008704 100644 --- a/harbour/source/rtl/gtchrmap.c +++ b/harbour/source/rtl/gtchrmap.c @@ -382,8 +382,7 @@ static int hb_gt_chrmapread( const char *pszFile, const char *pszTerm, int *nTra if( fp != NULL ) { - strncpy( buf, pszTerm, sizeof( buf ) ); - buf[ sizeof( buf ) - 1 ] = '\0'; + hb_strncpy( buf, pszTerm, sizeof( buf ) - 1 ); isTerm = 0; pTerm = buf; while( pTerm ) @@ -431,8 +430,8 @@ int hb_gt_chrmapinit( int *piTransTbl, const char *pszTerm, BOOL fSetACSC ) if( pszFile != NULL && sizeof( szFile ) > strlen( pszFile ) + strlen( s_szDefaultCharMapFile ) ) { - hb_strncpy( szFile, pszFile, _POSIX_PATH_MAX ); - hb_strncat( szFile, s_szDefaultCharMapFile, _POSIX_PATH_MAX ); + hb_strncpy( szFile, pszFile, sizeof( szFile ) - 1 ); + hb_strncat( szFile, s_szDefaultCharMapFile, sizeof( szFile ) - 1 ); nRet = hb_gt_chrmapread( szFile, pszTerm, piTransTbl ); } } diff --git a/harbour/source/rtl/gtcrs/gtcrs.c b/harbour/source/rtl/gtcrs/gtcrs.c index 83568f8bda..b492b2c951 100644 --- a/harbour/source/rtl/gtcrs/gtcrs.c +++ b/harbour/source/rtl/gtcrs/gtcrs.c @@ -1935,8 +1935,7 @@ static InOutBase *create_ioBase( char *term, int infd, int outfd, int errfd, { if ( ( i = ptr - term ) >= sizeof( buf ) ) i = sizeof( buf ) - 1; - strncpy( buf, term, i ); - buf[i] = '\0'; + hb_strncpy( buf, term, i ); if ( i ) crsterm = buf; } @@ -2824,10 +2823,10 @@ static BOOL hb_gt_crs_SetDispCP( PHB_GT pGT, char *pszTermCDP, char *pszHostCDP, char *pszHostLetters = ( char * ) hb_xgrab( cdpHost->nChars * 2 + 1 ); char *pszTermLetters = ( char * ) hb_xgrab( cdpTerm->nChars * 2 + 1 ); - strncpy( pszHostLetters, cdpHost->CharsUpper, cdpHost->nChars + 1 ); - strncat( pszHostLetters, cdpHost->CharsLower, cdpHost->nChars + 1 ); - strncpy( pszTermLetters, cdpTerm->CharsUpper, cdpTerm->nChars + 1 ); - strncat( pszTermLetters, cdpTerm->CharsLower, cdpTerm->nChars + 1 ); + hb_strncpy( pszHostLetters, cdpHost->CharsUpper, cdpHost->nChars * 2 ); + hb_strncat( pszHostLetters, cdpHost->CharsLower, cdpHost->nChars * 2 ); + hb_strncpy( pszTermLetters, cdpTerm->CharsUpper, cdpTerm->nChars * 2 ); + hb_strncat( pszTermLetters, cdpTerm->CharsLower, cdpTerm->nChars * 2 ); setDispTrans( s_ioBase, pszHostLetters, pszTermLetters, fBox ? 1 : 0 ); @@ -2868,10 +2867,10 @@ static BOOL hb_gt_crs_SetKeyCP( PHB_GT pGT, char *pszTermCDP, char *pszHostCDP ) char *pszHostLetters = ( char * ) hb_xgrab( cdpHost->nChars * 2 + 1 ); char *pszTermLetters = ( char * ) hb_xgrab( cdpTerm->nChars * 2 + 1 ); - strncpy( pszHostLetters, cdpHost->CharsUpper, cdpHost->nChars + 1 ); - strncat( pszHostLetters, cdpHost->CharsLower, cdpHost->nChars + 1 ); - strncpy( pszTermLetters, cdpTerm->CharsUpper, cdpTerm->nChars + 1 ); - strncat( pszTermLetters, cdpTerm->CharsLower, cdpTerm->nChars + 1 ); + hb_strncpy( pszHostLetters, cdpHost->CharsUpper, cdpHost->nChars * 2 ); + hb_strncat( pszHostLetters, cdpHost->CharsLower, cdpHost->nChars * 2 ); + hb_strncpy( pszTermLetters, cdpTerm->CharsUpper, cdpTerm->nChars * 2 ); + hb_strncat( pszTermLetters, cdpTerm->CharsLower, cdpTerm->nChars * 2 ); setKeyTrans( s_ioBase, ( unsigned char * ) pszTermLetters, ( unsigned char * ) pszHostLetters ); diff --git a/harbour/source/rtl/gtpca/gtpca.c b/harbour/source/rtl/gtpca/gtpca.c index 16653cf5df..9f6e295881 100644 --- a/harbour/source/rtl/gtpca/gtpca.c +++ b/harbour/source/rtl/gtpca/gtpca.c @@ -760,10 +760,10 @@ static BOOL hb_gt_pca_SetKeyCP( PHB_GT pGT, char *pszTermCDP, char *pszHostCDP ) char *pszHostLetters = ( char * ) hb_xgrab( cdpHost->nChars * 2 + 1 ); char *pszTermLetters = ( char * ) hb_xgrab( cdpTerm->nChars * 2 + 1 ); - strncpy( pszHostLetters, cdpHost->CharsUpper, cdpHost->nChars + 1 ); - strncat( pszHostLetters, cdpHost->CharsLower, cdpHost->nChars + 1 ); - strncpy( pszTermLetters, cdpTerm->CharsUpper, cdpTerm->nChars + 1 ); - strncat( pszTermLetters, cdpTerm->CharsLower, cdpTerm->nChars + 1 ); + hb_strncpy( pszHostLetters, cdpHost->CharsUpper, cdpHost->nChars * 2 ); + hb_strncat( pszHostLetters, cdpHost->CharsLower, cdpHost->nChars * 2 ); + hb_strncpy( pszTermLetters, cdpTerm->CharsUpper, cdpTerm->nChars * 2 ); + hb_strncat( pszTermLetters, cdpTerm->CharsLower, cdpTerm->nChars * 2 ); hb_gt_pca_setKeyTrans( pszTermLetters, pszHostLetters ); diff --git a/harbour/source/rtl/gtstd/gtstd.c b/harbour/source/rtl/gtstd/gtstd.c index edbc5f673b..0d72afdd5e 100644 --- a/harbour/source/rtl/gtstd/gtstd.c +++ b/harbour/source/rtl/gtstd/gtstd.c @@ -553,10 +553,10 @@ static BOOL hb_gt_std_SetKeyCP( PHB_GT pGT, char *pszTermCDP, char *pszHostCDP ) char *pszHostLetters = ( char * ) hb_xgrab( cdpHost->nChars * 2 + 1 ); char *pszTermLetters = ( char * ) hb_xgrab( cdpTerm->nChars * 2 + 1 ); - strncpy( pszHostLetters, cdpHost->CharsUpper, cdpHost->nChars + 1 ); - strncat( pszHostLetters, cdpHost->CharsLower, cdpHost->nChars + 1 ); - strncpy( pszTermLetters, cdpTerm->CharsUpper, cdpTerm->nChars + 1 ); - strncat( pszTermLetters, cdpTerm->CharsLower, cdpTerm->nChars + 1 ); + hb_strncpy( pszHostLetters, cdpHost->CharsUpper, cdpHost->nChars * 2 ); + hb_strncat( pszHostLetters, cdpHost->CharsLower, cdpHost->nChars * 2 ); + hb_strncpy( pszTermLetters, cdpTerm->CharsUpper, cdpTerm->nChars * 2 ); + hb_strncat( pszTermLetters, cdpTerm->CharsLower, cdpTerm->nChars * 2 ); hb_gt_std_setKeyTrans( HB_GTSTD_GET( pGT ), pszTermLetters, pszHostLetters ); diff --git a/harbour/source/rtl/hbffind.c b/harbour/source/rtl/hbffind.c index 9aead68b6c..85766f9dd7 100644 --- a/harbour/source/rtl/hbffind.c +++ b/harbour/source/rtl/hbffind.c @@ -466,7 +466,7 @@ static BOOL hb_fsFindNextLow( PHB_FFIND ffind ) if( bFound ) { - hb_strncpy( ffind->szName, info->entry.ff_name, _POSIX_PATH_MAX ); + hb_strncpy( ffind->szName, info->entry.ff_name, sizeof( ffind->szName ) - 1 ); ffind->size = info->entry.ff_fsize; raw_attr = info->entry.ff_attrib; @@ -531,7 +531,7 @@ static BOOL hb_fsFindNextLow( PHB_FFIND ffind ) stat( info->entry.achName, &sStat ); - hb_strncpy( ffind->szName, info->entry.achName, _POSIX_PATH_MAX ); + hb_strncpy( ffind->szName, info->entry.achName, sizeof( ffind->szName ) - 1 ); ffind->size = sStat.st_size; raw_attr = info->entry.attrFile; @@ -601,7 +601,7 @@ static BOOL hb_fsFindNextLow( PHB_FFIND ffind ) if( bFound ) { - hb_strncpy( ffind->szName, info->pFindFileData.cFileName, _POSIX_PATH_MAX ); + hb_strncpy( ffind->szName, info->pFindFileData.cFileName, sizeof( ffind->szName ) - 1 ); if( info->pFindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ffind->size = 0; @@ -715,7 +715,7 @@ static BOOL hb_fsFindNextLow( PHB_FFIND ffind ) if( stat( dirname, &sStat ) == 0 ) #endif { - hb_strncpy( ffind->szName, info->entry->d_name, _POSIX_PATH_MAX ); + hb_strncpy( ffind->szName, info->entry->d_name, sizeof( ffind->szName ) - 1 ); ffind->size = sStat.st_size; raw_attr = sStat.st_mode; diff --git a/harbour/source/vm/cmdarg.c b/harbour/source/vm/cmdarg.c index b31695f0d6..892dfed510 100644 --- a/harbour/source/vm/cmdarg.c +++ b/harbour/source/vm/cmdarg.c @@ -226,8 +226,7 @@ static char * hb_cmdargGet( const char * pszName, BOOL bRetValue ) ulLen = pszEnd > pszNext ? pszEnd - pszNext : 0; pszRetVal = ( char * ) hb_xgrab( ulLen + 1 ); - strncpy( pszRetVal, pszNext, ulLen ); - pszRetVal[ ulLen ] = '\0'; + hb_strncpy( pszRetVal, pszNext, ulLen ); } else pszRetVal = ""; diff --git a/harbour/source/vm/memvars.c b/harbour/source/vm/memvars.c index 9f3c15ff7f..e27065bfd2 100644 --- a/harbour/source/vm/memvars.c +++ b/harbour/source/vm/memvars.c @@ -1299,8 +1299,7 @@ static HB_DYNS_FUNC( hb_memvarSave ) memset( buffer, 0, HB_MEM_REC_LEN ); /* NOTE: Save only the first 10 characters of the name */ - strncpy( ( char * ) buffer, pDynSymbol->pSymbol->szName, 10 ); - buffer[ 10 ] = '\0'; + hb_strncpy( ( char * ) buffer, pDynSymbol->pSymbol->szName, 10 ); if( HB_IS_STRING( pItem ) && ( hb_itemGetCLen( pItem ) + 1 ) <= SHRT_MAX ) {