From f7f2caa607bd962e5e0baa29b31a10f7c4efbb26 Mon Sep 17 00:00:00 2001 From: Przemyslaw Czerpak Date: Fri, 30 Oct 2009 02:41:17 +0000 Subject: [PATCH] 2009-10-30 03:40 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/src/common/hbgete.c * harbour/src/common/hbffind.c * harbour/src/common/hbfsapi.c * harbour/src/rtl/fstemp.c * harbour/src/rtl/filesys.c * harbour/src/rtl/diskspac.c * harbour/src/rtl/disksphb.c * harbour/src/rtl/net.c ! fixed memory leak reported by Pritpal * cleanup some UNICODE conversions in Windows builds (mostly resolved some macros when UNICODE macro is also explicitly used and cleaned some buffers length passed to windows functions. * removed unnecessary casting - if it's not necessary for some strange reasons never cast strings to LP[C][TW]STR calling windows functions. Such casting does not make any conversions but only hides potential bugs. * harbour/contrib/hbwin/axcore.c * pacified warning --- harbour/ChangeLog | 21 +++++++++++++++++++++ harbour/contrib/hbwin/axcore.c | 2 +- harbour/src/common/hbffind.c | 6 +++--- harbour/src/common/hbfsapi.c | 6 +++--- harbour/src/common/hbgete.c | 9 +++++---- harbour/src/rtl/diskspac.c | 4 ++-- harbour/src/rtl/disksphb.c | 5 ++--- harbour/src/rtl/filesys.c | 6 +++--- harbour/src/rtl/fstemp.c | 20 +++++++++----------- harbour/src/rtl/net.c | 14 ++++++++------ 10 files changed, 57 insertions(+), 36 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index e769674151..5a83425629 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -17,6 +17,27 @@ past entries belonging to author(s): Viktor Szakats. */ +2009-10-30 03:40 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + * harbour/src/common/hbgete.c + * harbour/src/common/hbffind.c + * harbour/src/common/hbfsapi.c + * harbour/src/rtl/fstemp.c + * harbour/src/rtl/filesys.c + * harbour/src/rtl/diskspac.c + * harbour/src/rtl/disksphb.c + * harbour/src/rtl/net.c + ! fixed memory leak reported by Pritpal + * cleanup some UNICODE conversions in Windows builds (mostly resolved + some macros when UNICODE macro is also explicitly used and cleaned + some buffers length passed to windows functions. + * removed unnecessary casting - if it's not necessary for some strange + reasons never cast strings to LP[C][TW]STR calling windows functions. + Such casting does not make any conversions but only hides potential + bugs. + + * harbour/contrib/hbwin/axcore.c + * pacified warning + 2009-10-29 22:30 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/doc/xhb-diff.txt + added information about Harbour and xHarbour versions described by diff --git a/harbour/contrib/hbwin/axcore.c b/harbour/contrib/hbwin/axcore.c index ed34401d3a..78bf775e75 100644 --- a/harbour/contrib/hbwin/axcore.c +++ b/harbour/contrib/hbwin/axcore.c @@ -343,7 +343,7 @@ static HRESULT STDMETHODCALLTYPE Invoke( IDispatch* lpThis, DISPID dispid, REFII iCount = pParams->cArgs; - for( i = iRefs = 0; i < iCount && iRefs < HB_SIZEOFARRAY( refArray ); i++ ) + for( i = iRefs = 0; i < iCount && iRefs < ( int ) HB_SIZEOFARRAY( refArray ); i++ ) { if( pParams->rgvarg[ i ].n1.n2.vt & VT_BYREF ) refArray[ iRefs++ ].item = hb_stackAllocItem(); diff --git a/harbour/src/common/hbffind.c b/harbour/src/common/hbffind.c index 577a4ac38b..1e802e8274 100644 --- a/harbour/src/common/hbffind.c +++ b/harbour/src/common/hbffind.c @@ -533,10 +533,10 @@ static BOOL hb_fsFindNextLow( PHB_FFIND ffind ) ffind->bFirst = FALSE; ffind->szName[ 0 ] = '\0'; - bFound = GetVolumeInformation( ( LPCTSTR ) lpFileMask, szName, sizeof( szName ) - 1, NULL, NULL, NULL, NULL, 0 ); + bFound = GetVolumeInformation( lpFileMask, szName, sizeof( szName ) - 1, NULL, NULL, NULL, NULL, 0 ); HB_TCHAR_FREE( lpFileMask ); - HB_TCHAR_GETFROM( ffind->szName, szName, sizeof( ffind->szName ) - 1 ); + HB_TCHAR_GETFROM( ffind->szName, szName, sizeof( ffind->szName ) ); } #endif } @@ -548,7 +548,7 @@ static BOOL hb_fsFindNextLow( PHB_FFIND ffind ) ffind->bFirst = FALSE; - info->hFindFile = FindFirstFile( ( LPCTSTR ) lpFileMask, &info->pFindFileData ); + info->hFindFile = FindFirstFile( lpFileMask, &info->pFindFileData ); info->dwAttr = ( DWORD ) hb_fsAttrToRaw( ffind->attrmask ); if( ( info->hFindFile != INVALID_HANDLE_VALUE ) && HB_WIN_MATCH() ) diff --git a/harbour/src/common/hbfsapi.c b/harbour/src/common/hbfsapi.c index 65271e801a..e424c13b14 100644 --- a/harbour/src/common/hbfsapi.c +++ b/harbour/src/common/hbfsapi.c @@ -312,7 +312,7 @@ BOOL hb_fsNameExists( const char * pszFileName ) { LPTSTR lpFileName = HB_TCHAR_CONVTO( pszFileName ); - fExist = ( GetFileAttributes( ( LPCTSTR ) lpFileName ) != INVALID_FILE_ATTRIBUTES ); + fExist = ( GetFileAttributes( lpFileName ) != INVALID_FILE_ATTRIBUTES ); HB_TCHAR_FREE( lpFileName ); } @@ -370,7 +370,7 @@ BOOL hb_fsFileExists( const char * pszFileName ) LPTSTR lpFileName = HB_TCHAR_CONVTO( pszFileName ); DWORD dwAttr; - dwAttr = GetFileAttributes( ( LPCTSTR ) lpFileName ); + dwAttr = GetFileAttributes( lpFileName ); fExist = ( dwAttr != INVALID_FILE_ATTRIBUTES ) && ( dwAttr & ( FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE ) ) == 0; @@ -433,7 +433,7 @@ BOOL hb_fsDirExists( const char * pszDirName ) LPTSTR lpDirName = HB_TCHAR_CONVTO( pszDirName ); DWORD dwAttr; - dwAttr = GetFileAttributes( ( LPCTSTR ) lpDirName ); + dwAttr = GetFileAttributes( lpDirName ); fExist = ( dwAttr != INVALID_FILE_ATTRIBUTES ) && ( dwAttr & FILE_ATTRIBUTE_DIRECTORY ); diff --git a/harbour/src/common/hbgete.c b/harbour/src/common/hbgete.c index 6e5287604c..8ea9ca6b29 100644 --- a/harbour/src/common/hbgete.c +++ b/harbour/src/common/hbgete.c @@ -79,12 +79,13 @@ char * hb_getenv( const char * szName ) { LPTSTR lpBuffer = ( LPTSTR ) hb_xgrab( size * sizeof( TCHAR ) ); GetEnvironmentVariable( lpName, lpBuffer, size ); - pszBuffer = HB_TCHAR_CONVFROM( lpBuffer ); #if defined( UNICODE ) - HB_TCHAR_FREE( lpBuffer ); + pszBuffer = hb_wctomb( lpBuffer ); + hb_xfree( lpBuffer ); +#else + pszBuffer = lpBuffer; #endif } - HB_TCHAR_FREE( lpName ); } @@ -146,7 +147,7 @@ BOOL hb_getenv_buffer( const char * szName, char * szBuffer, int nSize ) if( lpBuffer && lpBuffer != buffer ) hb_xfree( lpBuffer ); #else - fRetVal = GetEnvironmentVariableA( szName, szBuffer, nSize ) != 0; + fRetVal = GetEnvironmentVariable( szName, szBuffer, nSize ) != 0; #endif } #elif defined( HB_OS_OS2 ) diff --git a/harbour/src/rtl/diskspac.c b/harbour/src/rtl/diskspac.c index 0b38178d60..00d2ec0307 100644 --- a/harbour/src/rtl/diskspac.c +++ b/harbour/src/rtl/diskspac.c @@ -125,7 +125,7 @@ HB_FUNC( DISKSPACE ) #if defined( HB_OS_WIN_CE ) TCHAR lpPath[ 4 ]; - lpPath[ 0 ] = ( TCHAR ) uiDrive + 'A' - 1; + lpPath[ 0 ] = ( TCHAR ) ( uiDrive + 'A' - 1 ); lpPath[ 1 ] = ':'; lpPath[ 2 ] = '\\'; lpPath[ 3 ] = '\0'; @@ -164,7 +164,7 @@ HB_FUNC( DISKSPACE ) DWORD dwNumberOfFreeClusters; DWORD dwTotalNumberOfClusters; - bError = ! GetDiskFreeSpace( ( LPCTSTR ) lpPath, + bError = ! GetDiskFreeSpace( lpPath, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, diff --git a/harbour/src/rtl/disksphb.c b/harbour/src/rtl/disksphb.c index 331157b4d5..00e6c0d4bf 100644 --- a/harbour/src/rtl/disksphb.c +++ b/harbour/src/rtl/disksphb.c @@ -249,12 +249,13 @@ HB_FUNC( HB_DISKSPACE ) DWORD dwNumberOfFreeClusters; DWORD dwTotalNumberOfClusters; - fResult = GetDiskFreeSpace( ( LPCTSTR ) lpPath, + fResult = GetDiskFreeSpace( lpPath, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters ); hb_fsSetIOError( fResult, 0 ); + HB_TCHAR_FREE( lpPath ); if( fResult ) { switch( uiType ) @@ -279,8 +280,6 @@ HB_FUNC( HB_DISKSPACE ) break; } } - - HB_TCHAR_FREE( lpPath ); } #endif SetErrorMode( uiErrMode ); diff --git a/harbour/src/rtl/filesys.c b/harbour/src/rtl/filesys.c index d124d354ca..603f78b9eb 100644 --- a/harbour/src/rtl/filesys.c +++ b/harbour/src/rtl/filesys.c @@ -661,7 +661,7 @@ HB_FHANDLE hb_fsOpen( const char * pFilename, USHORT uiFlags ) convert_open_flags( FALSE, FC_NORMAL, uiFlags, &dwMode, &dwShare, &dwCreat, &dwAttr ); hb_vmUnlock(); - hFile = CreateFile( ( LPCTSTR ) lpFilename, dwMode, dwShare, NULL, dwCreat, dwAttr, NULL ); + hFile = CreateFile( lpFilename, dwMode, dwShare, NULL, dwCreat, dwAttr, NULL ); hb_fsSetIOError( hFile != ( HANDLE ) INVALID_HANDLE_VALUE, 0 ); hb_vmLock(); @@ -723,7 +723,7 @@ HB_FHANDLE hb_fsCreate( const char * pFilename, ULONG ulAttr ) convert_open_flags( TRUE, ulAttr, FO_EXCLUSIVE, &dwMode, &dwShare, &dwCreat, &dwAttr ); hb_vmUnlock(); - hFile = CreateFile( ( LPCTSTR ) lpFilename, dwMode, dwShare, NULL, dwCreat, dwAttr, NULL ); + hFile = CreateFile( lpFilename, dwMode, dwShare, NULL, dwCreat, dwAttr, NULL ); hb_fsSetIOError( hFile != ( HANDLE ) INVALID_HANDLE_VALUE, 0 ); hb_vmLock(); @@ -785,7 +785,7 @@ HB_FHANDLE hb_fsCreateEx( const char * pFilename, ULONG ulAttr, USHORT uiFlags ) convert_open_flags( TRUE, ulAttr, uiFlags, &dwMode, &dwShare, &dwCreat, &dwAttr ); hb_vmUnlock(); - hFile = CreateFile( ( LPCTSTR ) lpFilename, dwMode, dwShare, NULL, dwCreat, dwAttr, NULL ); + hFile = CreateFile( lpFilename, dwMode, dwShare, NULL, dwCreat, dwAttr, NULL ); hb_fsSetIOError( hFile != ( HANDLE ) INVALID_HANDLE_VALUE, 0 ); hb_vmLock(); diff --git a/harbour/src/rtl/fstemp.c b/harbour/src/rtl/fstemp.c index 264032078f..344494fc85 100644 --- a/harbour/src/rtl/fstemp.c +++ b/harbour/src/rtl/fstemp.c @@ -150,17 +150,15 @@ static HB_FHANDLE hb_fsCreateTempLow( const char * pszDir, const char * pszPrefi else { #if defined( HB_OS_WIN ) - LPTSTR lpName = ( LPTSTR ) hb_xgrab( HB_PATH_MAX * sizeof( TCHAR ) ); + TCHAR lpName[ HB_PATH_MAX ]; - if( GetTempPath( ( DWORD ) ( HB_PATH_MAX - 1 ), lpName ) ) + if( GetTempPath( HB_PATH_MAX, lpName ) ) HB_TCHAR_GETFROM( pszName, lpName, HB_PATH_MAX ); else { pszName[ 0 ] = '.'; pszName[ 1 ] = '\0'; } - - HB_TCHAR_FREE( lpName ); #else char * pszTmpDir = hb_getenv( "TMPDIR" ); @@ -268,25 +266,25 @@ static BOOL hb_fsTempName( char * pszBuffer, const char * pszDir, const char * p #if defined( HB_IO_WIN ) { LPTSTR lpPrefix = pszPrefix ? HB_TCHAR_CONVTO( pszPrefix ) : NULL; - LPTSTR lpBuffer = ( LPTSTR ) hb_xgrab( HB_PATH_MAX * sizeof( TCHAR ) ); - TCHAR pTempDir[ HB_PATH_MAX ]; + TCHAR lpBuffer[ HB_PATH_MAX ]; + TCHAR lpTempDir[ HB_PATH_MAX ]; if( pszDir && pszDir[ 0 ] != '\0' ) - HB_TCHAR_SETTO( pTempDir, pszDir, HB_SIZEOFARRAY( pTempDir ) - 1 ); + HB_TCHAR_SETTO( lpTempDir, pszDir, HB_PATH_MAX ); else { - if( ! GetTempPath( ( DWORD ) HB_SIZEOFARRAY( pTempDir ) - 1, pTempDir ) ) + if( ! GetTempPath( HB_PATH_MAX, lpTempDir ) ) { hb_fsSetIOError( FALSE, 0 ); return FALSE; } } - pTempDir[ HB_PATH_MAX - 1 ] = L'\0'; + lpTempDir[ HB_PATH_MAX - 1 ] = L'\0'; - fResult = GetTempFileName( pTempDir, lpPrefix ? lpPrefix : TEXT( "hb" ), 0, lpBuffer ); + fResult = GetTempFileName( lpTempDir, lpPrefix ? lpPrefix : TEXT( "hb" ), 0, lpBuffer ); HB_TCHAR_GETFROM( pszBuffer, lpBuffer, HB_PATH_MAX ); - HB_TCHAR_FREE( lpBuffer ); + if( lpPrefix ) HB_TCHAR_FREE( lpPrefix ); } diff --git a/harbour/src/rtl/net.c b/harbour/src/rtl/net.c index 4a8d95e8a9..97b174932a 100644 --- a/harbour/src/rtl/net.c +++ b/harbour/src/rtl/net.c @@ -151,15 +151,16 @@ char * hb_netname( void ) #elif defined( HB_OS_WIN ) DWORD ulLen = MAX_COMPUTERNAME_LENGTH + 1; - LPTSTR lpValue = ( LPTSTR ) hb_xgrab( ulLen * sizeof( TCHAR ) ); + TCHAR lpValue[ MAX_COMPUTERNAME_LENGTH + 1 ]; char * pszValue; lpValue[ 0 ] = L'\0'; GetComputerName( lpValue, &ulLen ); - pszValue = HB_TCHAR_CONVFROM( lpValue ); #if defined( UNICODE ) - HB_TCHAR_FREE( lpValue ); + pszValue = hb_wctomb( lpValue ); +#else + pszValue = hb_strdup( lpValue ); #endif return pszValue; @@ -187,15 +188,16 @@ char * hb_username( void ) #elif defined( HB_OS_WIN ) DWORD ulLen = 256; - LPTSTR lpValue = ( LPTSTR ) hb_xgrab( ulLen * sizeof( TCHAR ) ); + TCHAR lpValue[ 256 ]; char * pszValue; lpValue[ 0 ] = L'\0'; GetUserName( lpValue, &ulLen ); - pszValue = HB_TCHAR_CONVFROM( lpValue ); #if defined( UNICODE ) - HB_TCHAR_FREE( lpValue ); + pszValue = hb_wctomb( lpValue ); +#else + pszValue = hb_strdup( lpValue ); #endif return pszValue;