From b7477cb27f6bbc774222eb694131463e795cbdd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 24 Mar 2008 07:24:49 +0000 Subject: [PATCH] Combined 'findfunction' and 'FindFunction'. Updated "CheckBufferOverrun" (checking for-loops). --- CheckBufferOverrun.cpp | 124 +++++++++++++++++------------------------ CommonCheck.cpp | 11 ++-- CommonCheck.h | 2 +- 3 files changed, 60 insertions(+), 77 deletions(-) diff --git a/CheckBufferOverrun.cpp b/CheckBufferOverrun.cpp index af41d7947..e6d6c412c 100644 --- a/CheckBufferOverrun.cpp +++ b/CheckBufferOverrun.cpp @@ -11,40 +11,6 @@ extern bool ShowAll; //--------------------------------------------------------------------------- -static const TOKEN *findfunction(const TOKEN *tok) -{ - int indentlevel = 0, parlevel = 0; - for (; tok; tok = tok->next) - { - if (tok->str[0] == '{') - indentlevel++; - else if (tok->str[0] == '}') - indentlevel--; - else if (tok->str[0] == '(') - parlevel++; - else if (tok->str[0] == ')') - parlevel--; - - if (!tok->next) - break; - - if (indentlevel==0 && parlevel==0 && IsName(tok->str) && tok->next->str[0]=='(') - { - for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next) - { - if (tok2->str[0] == ')' && tok2->next) - { - if (tok2->next->str[0] == '{') - return tok; - break; - } - } - } - } - - return 0; -} - //--------------------------------------------------------------------------- // Writing dynamic data in buffer without bounds checking //--------------------------------------------------------------------------- @@ -108,7 +74,7 @@ static void _DynamicDataCheck(const TOKEN *ftok, const TOKEN *tok) static void CheckBufferOverrun_DynamicData() { - for (const TOKEN *ftok = findfunction(tokens); ftok; ftok = findfunction(ftok->next)) + for (const TOKEN *ftok = FindFunction(tokens,0); ftok; ftok = FindFunction(ftok->next,0)) { int indentlevel = 0; for (const TOKEN *tok = ftok; tok; tok = tok->next) @@ -218,48 +184,62 @@ static void CheckBufferOverrun_LocalVariable_CheckScope( const TOKEN *tok, const // Loop.. - if ( match(tok, "for ( var = 0 ;") ) + if ( match(tok, "for (") ) { - const char *strindex = 0; - int value = 0; + const TOKEN *tok2 = gettok( tok, 2 ); - if (match(tok,"for ( var = 0 ; var < num ; var + + )") || - match(tok,"for ( var = 0 ; var < num ; + + var )") ) - { - strindex = getstr(tok,2); - value = atoi(getstr(tok,8)); - } - else if (match(tok,"for ( var = 0 ; var <= num ; var + + )") || - match(tok,"for ( var = 0 ; var <= num ; + + var )") ) - { - strindex = getstr(tok,2); - value = 1 + atoi(getstr(tok,8)); - } + // for - setup.. + if ( match(tok2, "var = 0 ;") ) + tok2 = gettok(tok2, 4); + else if ( match(tok2, "type var = 0 ;") ) + tok2 = gettok(tok2, 5); + else if ( match(tok2, "type type var = 0 ;") ) + tok2 = gettok(tok2, 6); + else + continue; - if (strindex && value>(int)size) - { - const TOKEN *tok2 = tok; - while (tok2 && strcmp(tok2->str,")")) - tok2 = tok2->next; - if (!tok2) - break; + // for - condition.. + if ( ! match(tok2, "var < num ;") && ! match(tok2, "var <= num ;")) + continue; + + // Get index variable and stopsize. + const char *strindex = tok2->str; + int value = (tok2->next->str[1] ? 1 : 0) + atoi(getstr(tok2, 2)); + if ( value <= size ) + continue; + + // Goto the end of the for loop.. + while (tok2 && strcmp(tok2->str,")")) tok2 = tok2->next; - if (tok2->str[0] == '{') - tok2 = tok2->next; - while (tok2 && !strchr(";}",tok2->str[0])) - { - if ( match( tok2, "var [ var ]" ) && - strcmp(tok2->str,varname)==0 && - strcmp(getstr(tok2,2),strindex)==0 ) - { - std::ostringstream ostr; - ostr << FileLine(tok2) << ": Buffer overrun"; - ReportErr(ostr.str()); - break; - } + if (!gettok(tok2,5)) + break; + int indentlevel2 = 0; + while (tok2) + { + if ( tok2->str[0] == ';' && indentlevel == 0 ) + break; - tok2 = tok2->next; + if ( tok2->str[0] == '{' ) + indentlevel2++; + + if ( tok2->str[0] == '}' ) + { + indentlevel2--; + if ( indentlevel2 <= 0 ) + break; } + + if ( match( tok2, "var [ var ]" ) && + strcmp(tok2->str,varname)==0 && + strcmp(getstr(tok2,2),strindex)==0 ) + { + std::ostringstream ostr; + ostr << FileLine(tok2) << ": Buffer overrun"; + ReportErr(ostr.str()); + break; + } + + tok2 = tok2->next; } continue; } @@ -298,7 +278,7 @@ static void CheckBufferOverrun_LocalVariable_CheckScope( const TOKEN *tok, const if ( match( tok, "var ( var )" ) && strcmp(varname, getstr(tok,2)) == 0 ) { // Find function.. - const TOKEN *ftok = FindFunction( tok->str ); + const TOKEN *ftok = FindFunction( tokens, tok->str ); if ( ! ftok ) continue; diff --git a/CommonCheck.cpp b/CommonCheck.cpp index af31a4e8a..e07ecd64c 100644 --- a/CommonCheck.cpp +++ b/CommonCheck.cpp @@ -58,10 +58,10 @@ bool IsStandardType(const char str[]) } //--------------------------------------------------------------------------- -const TOKEN *FindFunction( const char funcname[] ) +const TOKEN *FindFunction( const TOKEN *tok, const char funcname[] ) { int indentlevel = 0; - for ( const TOKEN *tok = tokens; tok; tok = tok->next ) + for ( ; tok; tok = tok->next ) { if ( tok->str[0] == '{' ) indentlevel++; @@ -69,7 +69,7 @@ const TOKEN *FindFunction( const char funcname[] ) else if ( tok->str[0] == '}' ) indentlevel--; - else if (indentlevel==0 && IsName(tok->str)) + else if (indentlevel==0 && match(tok,"var (")) { // Check if this is the first token of a function implementation.. bool haspar = false; @@ -79,12 +79,15 @@ const TOKEN *FindFunction( const char funcname[] ) haspar |= bool(tok2->str[0] == '('); if ( ! haspar && match(tok2,"var (") ) { - if ( strcmp(funcname, tok2->str) != 0 ) + if ( funcname && strcmp(funcname, tok2->str) != 0 ) break; foundname = true; } if ( tok2->str[0] == ';' ) + { + tok = tok2; break; + } if ( tok2->str[0] == '{' ) break; if ( foundname && haspar && match(tok2, ") {") ) diff --git a/CommonCheck.h b/CommonCheck.h index 07a0b26ec..24dc1d099 100644 --- a/CommonCheck.h +++ b/CommonCheck.h @@ -21,7 +21,7 @@ bool IsNumber(const char str[]); bool IsStandardType(const char str[]); -const TOKEN *FindFunction( const char funcname[] ); +const TOKEN *FindFunction( const TOKEN *tok, const char funcname[] ); //--------------------------------------------------------------------------- #endif