Refactoring: Added helper function 'setindentlevel'

This commit is contained in:
Daniel Marjamäki 2008-03-22 20:14:25 +00:00
parent ede402a5ec
commit 0d0f562e90
3 changed files with 159 additions and 150 deletions

View File

@ -16,11 +16,9 @@ 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] == '(')
setindentlevel( tok, indentlevel );
if (tok->str[0] == '(')
parlevel++;
else if (tok->str[0] == ')')
parlevel--;
@ -113,11 +111,8 @@ static void CheckBufferOverrun_DynamicData()
int indentlevel = 0;
for (const TOKEN *tok = ftok; tok; tok = tok->next)
{
if (tok->str[0] == '{')
indentlevel++;
else if (tok->str[0] == '}')
if (setindentlevel(tok, indentlevel))
{
indentlevel--;
if (indentlevel <= 0)
break;
}
@ -160,160 +155,147 @@ static void CheckBufferOverrun_LocalVariable()
int indentlevel = 0;
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (tok->str[0]=='{')
indentlevel++;
setindentlevel( tok, indentlevel );
if (indentlevel < 0)
break;
else if (tok->str[0]=='}')
indentlevel--;
// Declaring array..
if ( ! match(tok, "type var [ num ] ;") )
continue;
else if (indentlevel > 0)
const char *varname = getstr(tok,1);
unsigned int size = strtoul(getstr(tok,3), NULL, 10);
int total_size = size * SizeOfType(tok->str);
if (total_size == 0)
continue;
int _indentlevel = 0;
for (const TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next)
{
// Declaring array..
if (match(tok, "type var [ num ] ;"))
setindentlevel(tok2, _indentlevel);
if ( _indentlevel < 0 )
break;
// Array index..
if (strcmp(tok2->str,varname)==0 &&
strcmp(getstr(tok2,1),"[")==0 &&
IsNumber(getstr(tok2,2)) &&
strcmp(getstr(tok2,3),"]")==0 )
{
const char *varname = getstr(tok,1);
unsigned int size = strtoul(getstr(tok,3), NULL, 10);
int total_size = size * SizeOfType(tok->str);
if (total_size == 0)
continue;
int _indentlevel = indentlevel;
for (const TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next)
const char *str = getstr(tok2, 2);
if (strtoul(str, NULL, 10) >= size)
{
if (tok2->str[0]=='{')
std::ostringstream ostr;
ostr << FileLine(tok2) << ": Array index out of bounds";
ReportErr(ostr.str());
}
}
// memset, memcmp, memcpy, strncpy, fgets..
if (strcmp(tok2->str,"memset")==0 ||
strcmp(tok2->str,"memcpy")==0 ||
strcmp(tok2->str,"memmove")==0 ||
strcmp(tok2->str,"memcmp")==0 ||
strcmp(tok2->str,"strncpy")==0 ||
strcmp(tok2->str,"fgets")==0 )
{
if (match(tok2->next,"( var , num , num )") ||
match(tok2->next,"( var , var , num )") )
{
const char *var1 = getstr(tok2, 2);
const char *var2 = getstr(tok2, 4);
const char *num = getstr(tok2, 6);
if ( atoi(num)>total_size &&
(strcmp(var1,varname)==0 ||
strcmp(var2,varname)==0 ) )
{
_indentlevel++;
std::ostringstream ostr;
ostr << FileLine(tok2) << ": Buffer overrun";
ReportErr(ostr.str());
}
else if (tok2->str[0]=='}')
}
}
// Loop..
const char *strindex = 0;
int value = 0;
if ( match(tok2, "for ( var = 0 ;") )
{
if (match(tok2,"for ( var = 0 ; var < num ; var + + )"))
{
strindex = getstr(tok2,2);
value = atoi(getstr(tok2,8));
}
else if (match(tok2,"for ( var = 0 ; var <= num ; var + + )"))
{
strindex = getstr(tok2,2);
value = 1 + atoi(getstr(tok2,8));
}
else if (match(tok2,"for ( var = 0 ; var < num ; + + var )"))
{
strindex = getstr(tok2,2);
value = atoi(getstr(tok2,8));
}
else if (match(tok2,"for ( var = 0 ; var <= num ; + + var )"))
{
strindex = getstr(tok2,2);
value = 1 + atoi(getstr(tok2,8));
}
}
if (strindex && value>(int)size)
{
const TOKEN *tok3 = tok2;
while (tok3 && strcmp(tok3->str,")"))
tok3 = tok3->next;
if (!tok3)
break;
tok3 = tok3->next;
if (tok3->str[0] == '{')
tok3 = tok3->next;
while (tok3 && !strchr(";}",tok3->str[0]))
{
if (strcmp(tok3->str,varname)==0 &&
strcmp(getstr(tok3,1),"[")==0 &&
strcmp(getstr(tok3,2),strindex)==0 &&
strcmp(getstr(tok3,3),"]")==0 )
{
_indentlevel--;
if (_indentlevel < indentlevel)
break;
std::ostringstream ostr;
ostr << FileLine(tok3) << ": Buffer overrun";
ReportErr(ostr.str());
break;
}
else
tok3 = tok3->next;
}
}
// Writing data into array..
if (match(tok2,"strcpy ( var , "))
{
int len = 0;
if (strcmp(getstr(tok2, 2), varname) == 0)
{
const char *str = getstr(tok2, 4);
if (str[0] == '\"')
{
// Array index..
if (strcmp(tok2->str,varname)==0 &&
strcmp(getstr(tok2,1),"[")==0 &&
IsNumber(getstr(tok2,2)) &&
strcmp(getstr(tok2,3),"]")==0 )
while (*str)
{
const char *str = getstr(tok2, 2);
if (strtoul(str, NULL, 10) >= size)
{
std::ostringstream ostr;
ostr << FileLine(tok2) << ": Array index out of bounds";
ReportErr(ostr.str());
}
}
// memset, memcmp, memcpy, strncpy, fgets..
if (strcmp(tok2->str,"memset")==0 ||
strcmp(tok2->str,"memcpy")==0 ||
strcmp(tok2->str,"memmove")==0 ||
strcmp(tok2->str,"memcmp")==0 ||
strcmp(tok2->str,"strncpy")==0 ||
strcmp(tok2->str,"fgets")==0 )
{
if (match(tok2->next,"( var , num , num )") ||
match(tok2->next,"( var , var , num )") )
{
const char *var1 = getstr(tok2, 2);
const char *var2 = getstr(tok2, 4);
const char *num = getstr(tok2, 6);
if ( atoi(num)>total_size &&
(strcmp(var1,varname)==0 ||
strcmp(var2,varname)==0 ) )
{
std::ostringstream ostr;
ostr << FileLine(tok2) << ": Buffer overrun";
ReportErr(ostr.str());
}
}
}
// Loop..
const char *strindex = 0;
int value = 0;
if ( match(tok2, "for ( var = 0 ;") )
{
if (match(tok2,"for ( var = 0 ; var < num ; var + + )"))
{
strindex = getstr(tok2,2);
value = atoi(getstr(tok2,8));
}
else if (match(tok2,"for ( var = 0 ; var <= num ; var + + )"))
{
strindex = getstr(tok2,2);
value = 1 + atoi(getstr(tok2,8));
}
else if (match(tok2,"for ( var = 0 ; var < num ; + + var )"))
{
strindex = getstr(tok2,2);
value = atoi(getstr(tok2,8));
}
else if (match(tok2,"for ( var = 0 ; var <= num ; + + var )"))
{
strindex = getstr(tok2,2);
value = 1 + atoi(getstr(tok2,8));
}
}
if (strindex && value>(int)size)
{
const TOKEN *tok3 = tok2;
while (tok3 && strcmp(tok3->str,")"))
tok3 = tok3->next;
if (!tok3)
break;
tok3 = tok3->next;
if (tok3->str[0] == '{')
tok3 = tok3->next;
while (tok3 && !strchr(";}",tok3->str[0]))
{
if (strcmp(tok3->str,varname)==0 &&
strcmp(getstr(tok3,1),"[")==0 &&
strcmp(getstr(tok3,2),strindex)==0 &&
strcmp(getstr(tok3,3),"]")==0 )
{
std::ostringstream ostr;
ostr << FileLine(tok3) << ": Buffer overrun";
ReportErr(ostr.str());
break;
}
tok3 = tok3->next;
}
}
// Writing data into array..
if (match(tok2,"strcpy ( var , "))
{
int len = 0;
if (strcmp(getstr(tok2, 2), varname) == 0)
{
const char *str = getstr(tok2, 4);
if (str[0] == '\"')
{
while (*str)
{
if (*str=='\\')
str++;
str++;
len++;
}
}
}
if (len > 2 && len >= (int)size + 2)
{
std::ostringstream ostr;
ostr << FileLine(tok2) << ": Buffer overrun";
ReportErr(ostr.str());
}
if (*str=='\\')
str++;
str++;
len++;
}
}
}
if (len > 2 && len >= (int)size + 2)
{
std::ostringstream ostr;
ostr << FileLine(tok2) << ": Buffer overrun";
ReportErr(ostr.str());
}
}
}
}

View File

@ -58,3 +58,14 @@ bool IsStandardType(const char str[])
}
//---------------------------------------------------------------------------
bool setindentlevel( const TOKEN *tok, int &indentlevel )
{
if ( tok->str[0] == '{' )
indentlevel++;
else if ( tok->str[0] == '}' )
indentlevel--;
return bool(tok->str[0] == '}');
}
//---------------------------------------------------------------------------

View File

@ -6,6 +6,10 @@
#include <string>
#include <sstream>
//---------------------------------------------------------------------------
// Report errors..
//---------------------------------------------------------------------------
struct TOKEN;
std::string FileLine(const TOKEN *tok);
@ -16,10 +20,22 @@ void ReportErr(const std::string &errmsg);
extern std::ostringstream errout;
//---------------------------------------------------------------------------
// Classify tokens..
//---------------------------------------------------------------------------
bool IsName(const char str[]);
bool IsNumber(const char str[]);
bool IsStandardType(const char str[]);
//---------------------------------------------------------------------------
// Iterating through tokens..
//---------------------------------------------------------------------------
bool setindentlevel( const TOKEN *tok, int &indentlevel );
//---------------------------------------------------------------------------
#endif