Combined 'findfunction' and 'FindFunction'. Updated "CheckBufferOverrun"

(checking for-loops).
This commit is contained in:
Daniel Marjamäki 2008-03-24 07:24:49 +00:00
parent 4c17dd9499
commit b7477cb27f
3 changed files with 60 additions and 77 deletions

View File

@ -11,40 +11,6 @@
extern bool ShowAll; 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 // 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() 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; int indentlevel = 0;
for (const TOKEN *tok = ftok; tok; tok = tok->next) for (const TOKEN *tok = ftok; tok; tok = tok->next)
@ -218,48 +184,62 @@ static void CheckBufferOverrun_LocalVariable_CheckScope( const TOKEN *tok, const
// Loop.. // Loop..
if ( match(tok, "for ( var = 0 ;") ) if ( match(tok, "for (") )
{ {
const char *strindex = 0; const TOKEN *tok2 = gettok( tok, 2 );
int value = 0;
if (match(tok,"for ( var = 0 ; var < num ; var + + )") || // for - setup..
match(tok,"for ( var = 0 ; var < num ; + + var )") ) if ( match(tok2, "var = 0 ;") )
{ tok2 = gettok(tok2, 4);
strindex = getstr(tok,2); else if ( match(tok2, "type var = 0 ;") )
value = atoi(getstr(tok,8)); tok2 = gettok(tok2, 5);
} else if ( match(tok2, "type type var = 0 ;") )
else if (match(tok,"for ( var = 0 ; var <= num ; var + + )") || tok2 = gettok(tok2, 6);
match(tok,"for ( var = 0 ; var <= num ; + + var )") ) else
{ continue;
strindex = getstr(tok,2);
value = 1 + atoi(getstr(tok,8));
}
if (strindex && value>(int)size) // for - condition..
{ if ( ! match(tok2, "var < num ;") && ! match(tok2, "var <= num ;"))
const TOKEN *tok2 = tok; continue;
while (tok2 && strcmp(tok2->str,")"))
tok2 = tok2->next; // Get index variable and stopsize.
if (!tok2) const char *strindex = tok2->str;
break; 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; tok2 = tok2->next;
if (tok2->str[0] == '{') if (!gettok(tok2,5))
tok2 = tok2->next; break;
while (tok2 && !strchr(";}",tok2->str[0])) int indentlevel2 = 0;
{ while (tok2)
if ( match( tok2, "var [ var ]" ) && {
strcmp(tok2->str,varname)==0 && if ( tok2->str[0] == ';' && indentlevel == 0 )
strcmp(getstr(tok2,2),strindex)==0 ) break;
{
std::ostringstream ostr;
ostr << FileLine(tok2) << ": Buffer overrun";
ReportErr(ostr.str());
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; 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 ) if ( match( tok, "var ( var )" ) && strcmp(varname, getstr(tok,2)) == 0 )
{ {
// Find function.. // Find function..
const TOKEN *ftok = FindFunction( tok->str ); const TOKEN *ftok = FindFunction( tokens, tok->str );
if ( ! ftok ) if ( ! ftok )
continue; continue;

View File

@ -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; int indentlevel = 0;
for ( const TOKEN *tok = tokens; tok; tok = tok->next ) for ( ; tok; tok = tok->next )
{ {
if ( tok->str[0] == '{' ) if ( tok->str[0] == '{' )
indentlevel++; indentlevel++;
@ -69,7 +69,7 @@ const TOKEN *FindFunction( const char funcname[] )
else if ( tok->str[0] == '}' ) else if ( tok->str[0] == '}' )
indentlevel--; 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.. // Check if this is the first token of a function implementation..
bool haspar = false; bool haspar = false;
@ -79,12 +79,15 @@ const TOKEN *FindFunction( const char funcname[] )
haspar |= bool(tok2->str[0] == '('); haspar |= bool(tok2->str[0] == '(');
if ( ! haspar && match(tok2,"var (") ) if ( ! haspar && match(tok2,"var (") )
{ {
if ( strcmp(funcname, tok2->str) != 0 ) if ( funcname && strcmp(funcname, tok2->str) != 0 )
break; break;
foundname = true; foundname = true;
} }
if ( tok2->str[0] == ';' ) if ( tok2->str[0] == ';' )
{
tok = tok2;
break; break;
}
if ( tok2->str[0] == '{' ) if ( tok2->str[0] == '{' )
break; break;
if ( foundname && haspar && match(tok2, ") {") ) if ( foundname && haspar && match(tok2, ") {") )

View File

@ -21,7 +21,7 @@ bool IsNumber(const char str[]);
bool IsStandardType(const char str[]); bool IsStandardType(const char str[]);
const TOKEN *FindFunction( const char funcname[] ); const TOKEN *FindFunction( const TOKEN *tok, const char funcname[] );
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif #endif