From 14552ab56fdf0e80a2adefaecd09afb0350adfda Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Sat, 5 Dec 2009 14:35:57 +0000 Subject: [PATCH] 2009-12-05 15:33 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) * contrib/hbide/ideparseexpr.c ! Fixed to not contain non-ANSI comments. % Minor optimizations. ! Fixed linearfind() to return incorrect result when called in case insensitive mode. (not exploited by current code) % Marked worker functions as static. + Using Harbour types HB_BOOL and HB_SIZE. (type usage is still inconsistent) % Elminiated HB_ISBYREF() call. Not needed. ! Fixed to use hb_strncat() instead of strcat(). ! Fixed unsafe '!' oparator usage. (MSVC C++ warning) ; TOFIX: There is still one unsafe strncpy() usage, but I can't fix it. Anyone? --- harbour/ChangeLog | 14 +++ harbour/contrib/hbide/ideparseexpr.c | 179 +++++++++++++-------------- 2 files changed, 100 insertions(+), 93 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index d1cb3fef93..2924e36cee 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -17,6 +17,20 @@ past entries belonging to author(s): Viktor Szakats. */ +2009-12-05 15:33 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) + * contrib/hbide/ideparseexpr.c + ! Fixed to not contain non-ANSI comments. + % Minor optimizations. + ! Fixed linearfind() to return incorrect result when called in + case insensitive mode. (not exploited by current code) + % Marked worker functions as static. + + Using Harbour types HB_BOOL and HB_SIZE. (type usage is still inconsistent) + % Elminiated HB_ISBYREF() call. Not needed. + ! Fixed to use hb_strncat() instead of strcat(). + ! Fixed unsafe '!' oparator usage. (MSVC C++ warning) + ; TOFIX: There is still one unsafe strncpy() usage, but I can't fix it. + Anyone? + 2009-12-05 15:14 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) * contrib/hbide/ideparseexpr.c ! Fixed to not use explicit values for const variable sizes. diff --git a/harbour/contrib/hbide/ideparseexpr.c b/harbour/contrib/hbide/ideparseexpr.c index d60ff49896..33cffc5064 100644 --- a/harbour/contrib/hbide/ideparseexpr.c +++ b/harbour/contrib/hbide/ideparseexpr.c @@ -73,38 +73,34 @@ #define MAX_LINE_LEN 2047 -static const char good[] = "''_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890."; -static const char * adouble[] = { "*/", "/*", "//", "->", "::", "||", "++", "--", "**", ":=", - "<=", ">=", "<>", "!=", "==", "+=", "-=", "*=", "/=", "%=", - "^=", "&&", "^^", ">>", "<<", "=>", "&=", "|=" }; +static const char s_good[] = "''_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890."; +static const char * s_adouble[] = { "*/", "/*", "//", "->", "::", "||", "++", "--", "**", ":=", + "<=", ">=", "<>", "!=", "==", "+=", "-=", "*=", "/=", "%=", + "^=", "&&", "^^", ">>", "<<", "=>", "&=", "|=" }; -static int lengood = HB_SIZEOFARRAY( good ) - 1; -static int lendouble = HB_SIZEOFARRAY( adouble ); +static int s_lengood = HB_SIZEOFARRAY( s_good ) - 1; +static int s_lendouble = HB_SIZEOFARRAY( s_adouble ); /*----------------------------------------------------------------------*/ -UINT linearfind( const char** array, const char* cText, UINT lenarray, UINT lentext, int lMatchCase ) +static HB_SIZE linearfind( const char** array, const char* cText, HB_SIZE lenarray, HB_SIZE lentext, HB_BOOL bMatchCase ) { - UINT i; + HB_SIZE i; - if( lMatchCase ) + if( bMatchCase ) { - for( i = 0 ; i < lenarray ; i++ ) + for( i = 0; i < lenarray; i++ ) { if( strncmp( cText, array[ i ], lentext + 1 ) == 0 ) - { - return( i + 1 ); - } + return i + 1; } } else { - for( i = 0 ; i < lenarray ; i++ ) + for( i = 0; i < lenarray; i++ ) { if( hb_strnicmp( cText, array[ i ], lentext + 1 ) == 0 ) - { - return( i++ ); - } + return i + 1; } } @@ -113,72 +109,74 @@ UINT linearfind( const char** array, const char* cText, UINT lenarray, UINT lent /*----------------------------------------------------------------------*/ -BOOL strempty( char* string ) +static HB_BOOL strempty( const char * string ) { - BOOL lRet = TRUE; - UINT i = 0; + HB_SIZE i = 0; while( string[ i ] != 0 ) { - if( string[ i++ ] != 32 ) - { - lRet = FALSE; - break; - } + if( string[ i++ ] != ' ' ) + return HB_FALSE; } - return lRet; + + return HB_TRUE; } /*----------------------------------------------------------------------*/ -UINT atbuff( const char * chars, const char * string, UINT StartFrom, UINT Target, UINT len_chars, UINT len ) +static HB_SIZE atbuff( const char * chars, const char * string, HB_SIZE StartFrom, HB_SIZE Target, HB_SIZE len_chars, HB_SIZE len ) { - UINT x ; - UINT Counter = 0; - if( len >= len_chars && StartFrom <= len - len_chars ) + { + HB_SIZE x; + HB_SIZE Counter = 0; + for( x = StartFrom; x <= ( len - len_chars ); x++ ) + { if( strncmp( string + x, chars, len_chars ) == 0 ) + { if( ++Counter == Target ) - return( x + 1 ); + return x + 1; + } + } + } + return 0; } /*----------------------------------------------------------------------*/ -static int _GetWord( const char * cText, BOOL lHonorSpacing, char * cWord, int * pnpos ) +static int _GetWord( const char * cText, HB_BOOL lHonorSpacing, char * cWord, int * pnpos ) { - int maxlen = strlen( cText ); - int npos = 0; - int wordlen = 0; - char temp; - char ch; - char csingle[ 2 ]; - char cdouble[ 3 ]; - - // workaround -// good[ 0 ] = '"'; - - csingle[ 1 ] = 0; - cdouble[ 2 ] = 0; + int maxlen = strlen( cText ); + int npos = 0; + int wordlen = 0; if( maxlen > 0 ) { + char temp; + char ch; + char csingle[ 2 ]; + char cdouble[ 3 ]; + + csingle[ 1 ] = 0; + cdouble[ 2 ] = 0; + ch = cText[ 0 ]; - if( ch == ',' ) // lists + if( ch == ',' ) /* lists */ { - cWord[ wordlen++ ] = ch ; - npos++ ; + cWord[ wordlen++ ] = ch; + npos++; } - else // literals + else /* literals */ { if( ch == '"' || ch == '\'' ) { - temp = ch; + temp = ch; cWord[ wordlen++ ] = ch; npos++; - ch = ' '; + ch = ' '; while( ( npos < maxlen ) && ( ch != temp ) ) { ch = cText[ npos ]; @@ -189,12 +187,12 @@ static int _GetWord( const char * cText, BOOL lHonorSpacing, char * cWord, int * else { csingle[0] = ch; - if( atbuff( csingle, good, 0, 1, 1, lengood ) ) //ch $ good ) // variables, commands, function names + if( atbuff( csingle, s_good, 0, 1, 1, s_lengood ) ) /* ch $ s_good ) // variables, commands, function names */ { - while( ( npos < maxlen ) && atbuff( csingle, good, 0, 1, 1, lengood ) ) + while( ( npos < maxlen ) && atbuff( csingle, s_good, 0, 1, 1, s_lengood ) ) { cWord[ wordlen++ ] = ch; - npos++ ; + npos++; ch = cText[ npos ]; csingle[ 0 ] = ch; } @@ -211,11 +209,11 @@ static int _GetWord( const char * cText, BOOL lHonorSpacing, char * cWord, int * if( !lHonorSpacing ) { - cWord[ 0 ] = ' '; //reduce spaces to 1 + cWord[ 0 ] = ' '; /* reduce spaces to 1 */ wordlen = 1; } } - else //operators, punctuation + else /* operators, punctuation */ { cWord[ wordlen++ ]= ch; npos ++; @@ -224,7 +222,7 @@ static int _GetWord( const char * cText, BOOL lHonorSpacing, char * cWord, int * { cdouble[ 0 ] = cWord[ 0 ]; cdouble[ 1 ] = ch; - if( linearfind( adouble, cdouble, lendouble, 2, TRUE ) ) //if( (cWord + ch) $ adouble) //aScan( adouble, cWord + ch ) > 0 + if( linearfind( s_adouble, cdouble, s_lendouble, 2, HB_TRUE ) ) /* if( (cWord + ch) $ s_adouble) //aScan( s_adouble, cWord + ch ) > 0 */ { cWord[ wordlen++ ] = ch; npos ++; @@ -235,7 +233,7 @@ static int _GetWord( const char * cText, BOOL lHonorSpacing, char * cWord, int * } } - cWord[ wordlen ] = 0 ; + cWord[ wordlen ] = 0; *pnpos = npos; return wordlen; @@ -249,37 +247,37 @@ static int _GetWord( const char * cText, BOOL lHonorSpacing, char * cWord, int * HB_FUNC( PARSEXPR ) { const char * c = hb_parcx( 1 ); - BOOL lHonorSpacing = hb_parl( 2 ); - BOOL lInRemark = HB_ISLOG( 3 ) ? hb_parl( 3 ) : FALSE; - BOOL lKeepComments = HB_ISLOG( 5 ) ? hb_parl( 5 ) : TRUE; - BOOL bPRG = HB_ISLOG( 6 ) ? hb_parl( 6 ) : TRUE; - BOOL lKeepSpaces = HB_ISLOG( 7 ) ? hb_parl( 7 ) : TRUE; - PHB_ITEM aExpr = hb_itemArrayNew( 0 ); - PHB_ITEM element = hb_itemNew( NULL ); - BOOL lFirst = TRUE; - int lenprocessed = 0; - int lenwords = 0; - int wordlen ; - int npos ; + HB_BOOL lHonorSpacing = hb_parl( 2 ); + HB_BOOL lInRemark = HB_ISLOG( 3 ) ? hb_parl( 3 ) : HB_FALSE; + HB_BOOL lKeepComments = HB_ISLOG( 5 ) ? hb_parl( 5 ) : HB_TRUE; + HB_BOOL bPRG = HB_ISLOG( 6 ) ? hb_parl( 6 ) : HB_TRUE; + HB_BOOL lKeepSpaces = HB_ISLOG( 7 ) ? hb_parl( 7 ) : HB_TRUE; + PHB_ITEM aExpr = hb_itemArrayNew( 0 ); + PHB_ITEM element = hb_itemNew( NULL ); + HB_BOOL lFirst = HB_TRUE; + int lenprocessed = 0; + int lenwords = 0; + int wordlen; + int npos; char NextWord[ MAX_LINE_LEN + 1 ]; NextWord[ 0 ] = 0; - while( ( wordlen = _GetWord( c, lHonorSpacing, NextWord, &lenprocessed ) ) != 0 ) + while( ( wordlen = _GetWord( c, lHonorSpacing, NextWord, &lenprocessed ) ) != 0 ) { c += lenprocessed; - if( strncmp( NextWord, "*/", 3 ) == 0 ) // remark end + if( strncmp( NextWord, "*/", 3 ) == 0 ) /* remark end */ { if( lKeepComments ) { lenwords++; hb_arrayAdd( aExpr, hb_itemPutC( element, NextWord ) ); } - lInRemark = FALSE; + lInRemark = HB_FALSE; } - else if( ( strncmp( NextWord, "/*", 3 ) == 0) || lInRemark ) // remark start + else if( ( strncmp( NextWord, "/*", 3 ) == 0 ) || lInRemark ) /* remark start */ { lInRemark = ( ( npos = atbuff( "*/", c, 0, 1, 2, strlen( c ) ) ) == 0 ); @@ -288,7 +286,7 @@ HB_FUNC( PARSEXPR ) if( lKeepComments ) { - strcat( NextWord, c ); + hb_strncat( NextWord, c, sizeof( NextWord ) - 1 ); lenwords++; hb_arrayAdd( aExpr, hb_itemPutC( element, NextWord ) ); } @@ -298,29 +296,29 @@ HB_FUNC( PARSEXPR ) { if( lKeepComments ) { - strncpy( NextWord + wordlen, c, npos+1 ); + strncpy( NextWord + wordlen, c, npos + 1 ); NextWord[ wordlen + npos + 1 ] = 0; lenwords++; hb_arrayAdd( aExpr, hb_itemPutC( element, NextWord ) ); } - c += ( npos + 1 ) ;//2 ); + c += ( npos + 1 ); } } - else if( strncmp( NextWord, "//", 3 ) == 0 || ( bPRG && strncmp( NextWord, "&&", 3 ) == 0 ) ) // inline remark + else if( strncmp( NextWord, "//", 3 ) == 0 || ( bPRG && strncmp( NextWord, "&&", 3 ) == 0 ) ) /* inline remark */ { if( lKeepComments ) { - strcat( NextWord, c ); + hb_strncat( NextWord, c, sizeof( NextWord ) - 1 ); lenwords++; hb_arrayAdd( aExpr, hb_itemPutC( element, NextWord ) ); } break; } - else if( strncmp( NextWord, "**", 3 ) == 0 && ( lFirst && bPRG ) ) + else if( strncmp( NextWord, "**", 3 ) == 0 && lFirst && bPRG ) { if( lKeepComments ) { - strcat( NextWord, c ); + hb_strncat( NextWord, c, sizeof( NextWord ) - 1 ); lenwords++; hb_arrayAdd( aExpr, hb_itemPutC( element, NextWord ) ); } @@ -330,24 +328,19 @@ HB_FUNC( PARSEXPR ) { if( lKeepSpaces || ! strempty( NextWord ) ) { - lenwords++; - hb_arrayAdd( aExpr, hb_itemPutC( element, NextWord ) ); + lenwords++; + hb_arrayAdd( aExpr, hb_itemPutC( element, NextWord ) ); } } if( ! strempty( NextWord ) ) - { - lFirst = FALSE; - } - } // end while - - if( !lKeepComments && !lenwords > 0 && hb_arrayGetCPtr( aExpr, lenwords ) ) - { - hb_arraySize( aExpr, lenwords ); + lFirst = HB_FALSE; } - if( HB_ISBYREF( 3 ) ) - hb_storl( lInRemark, 3 ); + if( ! lKeepComments && !( lenwords > 0 ) && hb_arrayGetCPtr( aExpr, lenwords ) ) + hb_arraySize( aExpr, lenwords ); + + hb_storl( lInRemark, 3 ); hb_itemRelease( element ); hb_itemReturnRelease( aExpr );