Made the checking for buffer overruns more generic

This commit is contained in:
Daniel Marjamäki 2008-03-24 10:03:45 +00:00
parent b7477cb27f
commit c327dc10d8
1 changed files with 62 additions and 8 deletions

View File

@ -274,26 +274,80 @@ static void CheckBufferOverrun_LocalVariable_CheckScope( const TOKEN *tok, const
// Function call.. // Function call..
// Todo: This is just experimental. It must be more versatile.. if ( match( tok, "var (" ) )
if ( match( tok, "var ( var )" ) && strcmp(varname, getstr(tok,2)) == 0 )
{ {
unsigned int parlevel = 0, par = 0;
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{
if ( tok2->str[0] == '(' )
{
parlevel++;
}
else if ( tok2->str[0] == ')' )
{
parlevel--;
if ( parlevel < 1 )
{
par = 0;
break;
}
}
else if ( parlevel == 1 && tok2->str[0] == ',' )
{
par++;
}
if ( parlevel == 1 &&
strchr( "(,", *getstr(tok2,0) ) &&
strcmp( varname, getstr(tok2, 1) ) == 0 &&
strchr( ",)", *getstr(tok2,2) ) )
{
par++;
break;
}
}
if ( par == 0 )
continue;
// Find function.. // Find function..
const TOKEN *ftok = FindFunction( tokens, tok->str ); const TOKEN *ftok = FindFunction( tokens, tok->str );
if ( ! ftok ) if ( ! ftok )
continue; continue;
// Parse head of function.. // Parse head of function..
while ( ftok ) ftok = gettok( ftok, 2 );
parlevel = 1;
while ( ftok && parlevel == 1 && par >= 1 )
{ {
if ( match( ftok, "var ) {" ) ) if ( ftok->str[0] == '(' )
parlevel++;
else if ( ftok->str[0] == ')' )
parlevel--;
else if ( ftok->str[0] == ',' )
par--;
else if (par==1 && parlevel==1 && (match(ftok,"var ,") || match(ftok,"var )")))
{ {
CheckBufferOverrun_LocalVariable_CheckScope( gettok(ftok,3), ftok->str, size, total_size ); // Parameter name..
const char *parname = ftok->str;
// Goto function body..
while ( ftok && ftok->str[0] != '{' )
ftok = ftok->next;
ftok = ftok ? ftok->next : 0;
// Check variable usage in the function..
CheckBufferOverrun_LocalVariable_CheckScope( ftok, parname, size, total_size );
// break out..
break; break;
} }
if ( ftok->str[0] == '{' )
break;
ftok = ftok->next; ftok = ftok->next;
} }
} }