2007-05-24 15:06:47 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "CheckBufferOverrun.h"
|
2007-07-17 08:15:50 +02:00
|
|
|
#include "tokenize.h"
|
2007-05-24 15:06:47 +02:00
|
|
|
#include "CommonCheck.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <stdlib.h> // <- strtoul
|
|
|
|
|
2008-03-22 18:09:08 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
extern bool ShowAll;
|
2007-05-24 15:06:47 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-07-18 08:12:16 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Writing dynamic data in buffer without bounds checking
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-03-22 12:46:06 +01:00
|
|
|
static void _DynamicDataCheck(const TOKEN *ftok, const TOKEN *tok)
|
2007-07-18 08:12:16 +02:00
|
|
|
{
|
|
|
|
const char *var2 = tok->str;
|
|
|
|
bool decl = false;
|
|
|
|
unsigned int Var2Count = 0;
|
2008-03-22 12:46:06 +01:00
|
|
|
for ( const TOKEN *tok2 = ftok; tok2; tok2 = tok2->next )
|
2007-07-18 08:12:16 +02:00
|
|
|
{
|
|
|
|
if (tok2 == tok)
|
|
|
|
break;
|
|
|
|
if (match(tok2,"char * var"))
|
|
|
|
{
|
|
|
|
decl |= (strcmp(getstr(tok2,2),var2)==0);
|
|
|
|
tok2 = gettok(tok2,3);
|
|
|
|
if ( strcmp(tok2->str, "=") == 0 )
|
|
|
|
{
|
|
|
|
Var2Count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 08:21:01 +02:00
|
|
|
if (match(tok2,"char var [ ]"))
|
2007-07-18 08:12:16 +02:00
|
|
|
{
|
2007-07-19 08:21:01 +02:00
|
|
|
decl |= (strcmp(getstr(tok2,1),var2)==0);
|
|
|
|
tok2 = gettok(tok2,3);
|
|
|
|
}
|
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
// If ShowAll, only strlen(var2) counts
|
|
|
|
if ( ShowAll )
|
2007-07-19 08:21:01 +02:00
|
|
|
{
|
|
|
|
if (match(tok2,"strlen ( var )") &&
|
|
|
|
strcmp(getstr(tok2,2),var2)==0)
|
|
|
|
{
|
|
|
|
Var2Count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
// If not ShowAll, all usage of "var2" counts
|
2007-07-19 08:21:01 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (strcmp(tok2->str,var2)==0)
|
|
|
|
{
|
|
|
|
Var2Count++;
|
|
|
|
break;
|
|
|
|
}
|
2007-07-18 08:12:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The size of Var2 isn't checked, is it?
|
|
|
|
if (decl && Var2Count == 0)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": A string with unknown length is copied to buffer.";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-22 18:09:08 +01:00
|
|
|
static void CheckBufferOverrun_DynamicData()
|
2007-07-18 08:12:16 +02:00
|
|
|
{
|
2008-03-24 08:24:49 +01:00
|
|
|
for (const TOKEN *ftok = FindFunction(tokens,0); ftok; ftok = FindFunction(ftok->next,0))
|
2007-07-18 08:12:16 +02:00
|
|
|
{
|
|
|
|
int indentlevel = 0;
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = ftok; tok; tok = tok->next)
|
2007-07-18 08:12:16 +02:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
if (tok->str[0] == '{')
|
|
|
|
indentlevel++;
|
|
|
|
else if (tok->str[0] == '}')
|
2007-07-18 08:12:16 +02:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
indentlevel--;
|
|
|
|
if (indentlevel <= 0)
|
|
|
|
break;
|
2007-07-18 08:12:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (match(tok,"strcpy ( var , var )") ||
|
|
|
|
match(tok,"strcat ( var , var )") )
|
|
|
|
{
|
|
|
|
_DynamicDataCheck(ftok,gettok(tok,4));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match(tok,"sprintf ( var"))
|
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
for ( const TOKEN *tok2 = gettok(tok,3); tok2; tok2 = tok2->next )
|
2007-07-18 08:12:16 +02:00
|
|
|
{
|
|
|
|
if (tok2->str[0] == ')')
|
|
|
|
break;
|
|
|
|
if (match(tok2,", var ,") || match(tok2,", var )"))
|
|
|
|
{
|
|
|
|
_DynamicDataCheck(ftok,tok2->next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-24 15:06:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Buffer overrun..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-03-23 17:18:31 +01:00
|
|
|
static void CheckBufferOverrun_LocalVariable_CheckScope( const TOKEN *tok, const char varname[], const int size, const int total_size )
|
2007-05-24 15:06:47 +02:00
|
|
|
{
|
|
|
|
int indentlevel = 0;
|
2008-03-23 17:18:31 +01:00
|
|
|
for ( ; tok; tok = tok->next )
|
2007-05-24 15:06:47 +02:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
if (tok->str[0]=='{')
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
indentlevel++;
|
2008-03-23 17:18:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
2007-05-24 15:06:47 +02:00
|
|
|
|
2008-03-23 17:18:31 +01:00
|
|
|
if (tok->str[0]=='}')
|
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
indentlevel--;
|
2008-03-23 17:18:31 +01:00
|
|
|
if ( indentlevel < 0 )
|
|
|
|
return;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-22 21:14:25 +01:00
|
|
|
|
2008-03-23 17:18:31 +01:00
|
|
|
// Array index..
|
|
|
|
if (strcmp(tok->str,varname)==0 && match(tok->next,"[ num ]"))
|
2007-05-24 15:06:47 +02:00
|
|
|
{
|
2008-03-23 17:18:31 +01:00
|
|
|
const char *str = getstr(tok, 2);
|
|
|
|
if (strtol(str, NULL, 10) >= size)
|
2008-03-22 21:14:25 +01:00
|
|
|
{
|
2008-03-23 17:18:31 +01:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Array index out of bounds";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// memset, memcmp, memcpy, strncpy, fgets..
|
|
|
|
if (strcmp(tok->str,"memset")==0 ||
|
|
|
|
strcmp(tok->str,"memcpy")==0 ||
|
|
|
|
strcmp(tok->str,"memmove")==0 ||
|
|
|
|
strcmp(tok->str,"memcmp")==0 ||
|
|
|
|
strcmp(tok->str,"strncpy")==0 ||
|
|
|
|
strcmp(tok->str,"fgets")==0 )
|
|
|
|
{
|
|
|
|
if (match(tok->next,"( var , num , num )") ||
|
|
|
|
match(tok->next,"( var , var , num )") )
|
|
|
|
{
|
|
|
|
const char *var1 = getstr(tok, 2);
|
|
|
|
const char *var2 = getstr(tok, 4);
|
|
|
|
const char *num = getstr(tok, 6);
|
|
|
|
|
|
|
|
if ( atoi(num)>total_size &&
|
|
|
|
(strcmp(var1,varname)==0 ||
|
|
|
|
strcmp(var2,varname)==0 ) )
|
2008-03-22 21:14:25 +01:00
|
|
|
{
|
2008-03-23 17:18:31 +01:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Buffer overrun";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Loop..
|
2008-03-24 08:24:49 +01:00
|
|
|
if ( match(tok, "for (") )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-03-24 08:24:49 +01:00
|
|
|
const TOKEN *tok2 = gettok( tok, 2 );
|
|
|
|
|
|
|
|
// 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;
|
2008-03-23 17:18:31 +01:00
|
|
|
|
2008-03-24 08:24:49 +01:00
|
|
|
// 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;
|
2008-03-23 17:18:31 +01:00
|
|
|
|
2008-03-24 08:24:49 +01:00
|
|
|
// Goto the end of the for loop..
|
|
|
|
while (tok2 && strcmp(tok2->str,")"))
|
|
|
|
tok2 = tok2->next;
|
|
|
|
if (!gettok(tok2,5))
|
|
|
|
break;
|
|
|
|
int indentlevel2 = 0;
|
|
|
|
while (tok2)
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-03-24 08:24:49 +01:00
|
|
|
if ( tok2->str[0] == ';' && indentlevel == 0 )
|
2008-03-23 17:18:31 +01:00
|
|
|
break;
|
2008-03-24 08:24:49 +01:00
|
|
|
|
|
|
|
if ( tok2->str[0] == '{' )
|
|
|
|
indentlevel2++;
|
|
|
|
|
|
|
|
if ( tok2->str[0] == '}' )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-03-24 08:24:49 +01:00
|
|
|
indentlevel2--;
|
|
|
|
if ( indentlevel2 <= 0 )
|
2008-03-23 17:18:31 +01:00
|
|
|
break;
|
2008-03-24 08:24:49 +01:00
|
|
|
}
|
2008-03-23 17:18:31 +01:00
|
|
|
|
2008-03-24 08:24:49 +01:00
|
|
|
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;
|
2008-03-23 17:18:31 +01:00
|
|
|
}
|
2008-03-24 08:24:49 +01:00
|
|
|
|
|
|
|
tok2 = tok2->next;
|
2008-03-23 17:18:31 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Writing data into array..
|
|
|
|
if (match(tok,"strcpy ( var , "))
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
if (strcmp(getstr(tok, 2), varname) == 0)
|
|
|
|
{
|
|
|
|
const char *str = getstr(tok, 4);
|
|
|
|
if (str[0] == '\"')
|
|
|
|
{
|
|
|
|
while (*str)
|
2008-03-23 14:38:01 +01:00
|
|
|
{
|
2008-03-23 17:18:31 +01:00
|
|
|
if (*str=='\\')
|
|
|
|
str++;
|
|
|
|
str++;
|
|
|
|
len++;
|
2007-05-24 15:06:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-23 17:18:31 +01:00
|
|
|
if (len > 2 && len >= (int)size + 2)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Buffer overrun";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Function call..
|
|
|
|
// Todo: This is just experimental. It must be more versatile..
|
|
|
|
if ( match( tok, "var ( var )" ) && strcmp(varname, getstr(tok,2)) == 0 )
|
|
|
|
{
|
|
|
|
// Find function..
|
2008-03-24 08:24:49 +01:00
|
|
|
const TOKEN *ftok = FindFunction( tokens, tok->str );
|
2008-03-23 17:18:31 +01:00
|
|
|
if ( ! ftok )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Parse head of function..
|
|
|
|
while ( ftok )
|
|
|
|
{
|
|
|
|
if ( match( ftok, "var ) {" ) )
|
|
|
|
{
|
|
|
|
CheckBufferOverrun_LocalVariable_CheckScope( gettok(ftok,3), ftok->str, size, total_size );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ftok->str[0] == '{' )
|
|
|
|
break;
|
|
|
|
|
|
|
|
ftok = ftok->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckBufferOverrun_LocalVariable()
|
|
|
|
{
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
|
|
|
if (tok->str[0]=='{')
|
|
|
|
indentlevel++;
|
|
|
|
|
|
|
|
else if (tok->str[0]=='}')
|
|
|
|
indentlevel--;
|
|
|
|
|
|
|
|
else if (indentlevel > 0 && match(tok, "type var [ num ] ;"))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
CheckBufferOverrun_LocalVariable_CheckScope( gettok(tok,5), varname, size, total_size );
|
2007-05-24 15:06:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-03-23 15:15:44 +01:00
|
|
|
static void CheckBufferOverrun_StructVariable_CheckVar( const TOKEN *tok1, const char varname[], const char arrname[], const int arrsize )
|
2008-03-21 19:55:29 +01:00
|
|
|
{
|
|
|
|
const char *badpattern[] = {"varname",".","arrname","[","","]",NULL};
|
|
|
|
badpattern[0] = varname;
|
|
|
|
badpattern[2] = arrname;
|
2008-03-22 12:46:06 +01:00
|
|
|
const TOKEN *tok2 = findtoken( tok1, badpattern );
|
2008-03-21 19:55:29 +01:00
|
|
|
while (tok2)
|
|
|
|
{
|
|
|
|
if ( IsNumber( getstr(tok2, 4) ) )
|
|
|
|
{
|
|
|
|
if ( atoi( getstr(tok2, 4) ) >= arrsize )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << FileLine(tok2) << ": Array index out of bounds";
|
|
|
|
ReportErr(errmsg.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tok2 = findtoken( tok2->next, badpattern );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-03-21 18:25:25 +01:00
|
|
|
static void CheckBufferOverrun_StructVariable()
|
|
|
|
{
|
|
|
|
const char *declstruct_pattern[] = {"struct","","{",0};
|
2008-03-22 12:46:06 +01:00
|
|
|
for ( const TOKEN * tok = findtoken( tokens, declstruct_pattern );
|
2008-03-21 18:25:25 +01:00
|
|
|
tok;
|
|
|
|
tok = findtoken( tok->next, declstruct_pattern ) )
|
|
|
|
{
|
|
|
|
const char *structname = tok->next->str;
|
|
|
|
|
|
|
|
if ( ! IsName( structname ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Found a struct declaration. Search for arrays..
|
|
|
|
for ( TOKEN * tok2 = tok->next->next; tok2; tok2 = tok2->next )
|
|
|
|
{
|
|
|
|
if ( tok2->str[0] == '}' )
|
|
|
|
break;
|
|
|
|
|
2008-03-21 19:55:29 +01:00
|
|
|
if ( strchr( ";{,(", tok2->str[0] ) )
|
|
|
|
{
|
2008-03-21 18:25:25 +01:00
|
|
|
// Declare array..
|
|
|
|
if ( match(tok2->next, "var var [ num ] ;") )
|
|
|
|
{
|
2008-03-21 19:55:29 +01:00
|
|
|
const char *arrname = getstr(tok2, 2);
|
|
|
|
const char *arrsize = getstr(tok2, 4);
|
2008-03-21 18:25:25 +01:00
|
|
|
|
2008-03-22 12:46:06 +01:00
|
|
|
for ( const TOKEN *tok3 = tokens; tok3; tok3 = tok3->next )
|
2008-03-21 18:25:25 +01:00
|
|
|
{
|
2008-03-21 19:55:29 +01:00
|
|
|
if ( strcmp(tok3->str, structname) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Declare variable: Fred fred1;
|
|
|
|
if ( match( tok3->next, "var ;" ) )
|
2008-03-21 18:25:25 +01:00
|
|
|
{
|
2008-03-21 19:55:29 +01:00
|
|
|
const char *varname = tok3->next->str;
|
2008-03-23 15:15:44 +01:00
|
|
|
CheckBufferOverrun_StructVariable_CheckVar( tok3, varname, arrname, atoi(arrsize) );
|
2008-03-21 19:55:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Declare pointer: Fred *fred1
|
|
|
|
else if ( match(tok3->next, "* var") && tok3->next->next->next && strchr(",);=", tok3->next->next->next->str[0]) )
|
|
|
|
{
|
|
|
|
const char *varname = tok3->next->next->str;
|
2008-03-23 15:15:44 +01:00
|
|
|
CheckBufferOverrun_StructVariable_CheckVar( tok3, varname, arrname, atoi(arrsize) );
|
2008-03-21 18:25:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckBufferOverrun()
|
|
|
|
{
|
2008-03-22 18:09:08 +01:00
|
|
|
CheckBufferOverrun_DynamicData();
|
2008-03-21 18:25:25 +01:00
|
|
|
CheckBufferOverrun_LocalVariable();
|
|
|
|
CheckBufferOverrun_StructVariable();
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 15:06:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-25 08:44:53 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Dangerous functions
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void WarningDangerousFunctions()
|
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-05-25 08:44:53 +02:00
|
|
|
{
|
|
|
|
if (match(tok, "gets ("))
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Found 'gets'. You should use 'fgets' instead";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (match(tok, "scanf (") && strcmp(getstr(tok,2),"\"%s\"") == 0)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Found 'scanf'. You should use 'fgets' instead";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
|