2008-11-06 01:11:37 +01:00
|
|
|
/*
|
2008-10-26 08:55:15 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007 Daniel Marjamäki
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
2008-03-24 12:00:04 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Buffer overrun..
|
2007-05-24 15:06:47 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2008-03-24 12:00:04 +01:00
|
|
|
|
2007-05-24 15:06:47 +02:00
|
|
|
#include "CheckBufferOverrun.h"
|
|
|
|
#include "CommonCheck.h"
|
|
|
|
|
2008-03-25 06:52:28 +01:00
|
|
|
#include <algorithm>
|
2007-05-24 15:06:47 +02:00
|
|
|
#include <sstream>
|
2008-03-24 12:00:04 +01:00
|
|
|
#include <list>
|
2008-08-30 20:29:37 +02:00
|
|
|
#include <cstring>
|
2007-05-24 15:06:47 +02:00
|
|
|
|
|
|
|
#include <stdlib.h> // <- strtoul
|
|
|
|
|
2008-03-22 18:09:08 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
extern bool ShowAll;
|
2007-05-24 15:06:47 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-03-24 12:00:04 +01:00
|
|
|
// CallStack used when parsing into subfunctions.
|
|
|
|
static std::list<const TOKEN *> CallStack;
|
2007-07-18 08:12:16 +02:00
|
|
|
|
2008-03-25 18:10:52 +01:00
|
|
|
|
2008-03-24 12:00:04 +01:00
|
|
|
// Modified version of 'ReportError' that also reports the callstack
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[])
|
2008-11-11 18:22:40 +01:00
|
|
|
{
|
2008-03-24 12:00:04 +01:00
|
|
|
std::ostringstream ostr;
|
|
|
|
std::list<const TOKEN *>::const_iterator it;
|
|
|
|
for ( it = CallStack.begin(); it != CallStack.end(); it++ )
|
|
|
|
ostr << FileLine(*it) << " -> ";
|
|
|
|
ostr << FileLine(tok) << ": " << errmsg;
|
|
|
|
ReportErr(ostr.str());
|
2007-07-18 08:12:16 +02:00
|
|
|
}
|
2008-03-24 17:24:07 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-24 15:06:47 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2008-03-24 12:51:16 +01:00
|
|
|
// Check array usage..
|
2007-05-24 15:06:47 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, const char *varname[], const int size, const int total_size )
|
2007-05-24 15:06:47 +02:00
|
|
|
{
|
2008-03-24 17:24:07 +01:00
|
|
|
unsigned int varc = 1;
|
|
|
|
while ( varname[varc] )
|
|
|
|
varc++;
|
|
|
|
varc = 2 * ( varc - 1 );
|
|
|
|
|
2008-03-26 08:18:40 +01:00
|
|
|
|
|
|
|
// Array index..
|
2008-03-28 08:18:03 +01:00
|
|
|
if ( Match(tok, "%var1% [ %num% ]", varname) )
|
2008-03-26 08:18:40 +01:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
const char *num = Tokenizer::getstr(tok, 2 + varc);
|
2008-03-26 08:18:40 +01:00
|
|
|
if (strtol(num, NULL, 10) >= size)
|
|
|
|
{
|
|
|
|
ReportError(tok->next, "Array index out of bounds");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-11-11 18:22:40 +01:00
|
|
|
if (Match(tok, "{"))
|
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
|
|
|
}
|
2007-05-24 15:06:47 +02:00
|
|
|
|
2008-11-11 18:22:40 +01:00
|
|
|
else if (Match(tok, "}"))
|
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
|
|
|
if ( indentlevel < 0 )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Array index..
|
2008-11-06 00:49:46 +01:00
|
|
|
if ( !Match(tok, "%var%") && !Match(tok,"[.&]") && Match(tok->next, "%var1% [ %num% ]", varname) )
|
2007-05-24 15:06:47 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
const char *num = Tokenizer::getstr(tok->next, 2 + varc);
|
2008-03-24 17:24:07 +01:00
|
|
|
if (strtol(num, NULL, 10) >= size)
|
2008-03-22 21:14:25 +01:00
|
|
|
{
|
2008-03-26 08:18:40 +01:00
|
|
|
ReportError(tok->next, "Array index out of bounds");
|
2008-03-23 17:18:31 +01:00
|
|
|
}
|
2008-11-09 08:19:53 +01:00
|
|
|
tok = Tokenizer::gettok(tok, 4);
|
2008-03-23 17:18:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// memset, memcmp, memcpy, strncpy, fgets..
|
2008-11-11 18:22:40 +01:00
|
|
|
if (Match(tok,"memset") ||
|
|
|
|
Match(tok,"memcpy") ||
|
|
|
|
Match(tok,"memmove") ||
|
|
|
|
Match(tok,"memcmp") ||
|
|
|
|
Match(tok,"strncpy") ||
|
|
|
|
Match(tok,"fgets") )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-03-28 08:18:03 +01:00
|
|
|
if ( Match( tok->next, "( %var1% , %num% , %num% )", varname ) ||
|
|
|
|
Match( tok->next, "( %var% , %var1% , %num% )", varname ) )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
const char *num = Tokenizer::getstr(tok, varc + 6);
|
2008-03-24 17:24:07 +01:00
|
|
|
if ( atoi(num) > total_size )
|
2008-03-22 21:14:25 +01:00
|
|
|
{
|
2008-03-24 12:00:04 +01:00
|
|
|
ReportError(tok, "Buffer overrun");
|
2008-03-23 17:18:31 +01:00
|
|
|
}
|
|
|
|
}
|
2008-03-24 17:24:07 +01:00
|
|
|
continue;
|
2008-03-23 17:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Loop..
|
2008-03-28 08:18:03 +01:00
|
|
|
if ( Match(tok, "for (") )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
const TOKEN *tok2 = Tokenizer::gettok( tok, 2 );
|
2008-03-24 08:24:49 +01:00
|
|
|
|
|
|
|
// for - setup..
|
2008-03-28 08:18:03 +01:00
|
|
|
if ( Match(tok2, "%var% = 0 ;") )
|
2008-11-09 08:19:53 +01:00
|
|
|
tok2 = Tokenizer::gettok(tok2, 4);
|
2008-03-28 08:18:03 +01:00
|
|
|
else if ( Match(tok2, "%type% %var% = 0 ;") )
|
2008-11-09 08:19:53 +01:00
|
|
|
tok2 = Tokenizer::gettok(tok2, 5);
|
2008-03-28 08:18:03 +01:00
|
|
|
else if ( Match(tok2, "%type% %type% %var% = 0 ;") )
|
2008-11-09 08:19:53 +01:00
|
|
|
tok2 = Tokenizer::gettok(tok2, 6);
|
2008-03-24 08:24:49 +01:00
|
|
|
else
|
|
|
|
continue;
|
2008-03-23 17:18:31 +01:00
|
|
|
|
2008-03-24 08:24:49 +01:00
|
|
|
// for - condition..
|
2008-03-28 08:18:03 +01:00
|
|
|
if ( ! Match(tok2, "%var% < %num% ;") && ! Match(tok2, "%var% <= %num% ;"))
|
2008-03-24 08:24:49 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get index variable and stopsize.
|
|
|
|
const char *strindex = tok2->str;
|
2008-11-09 08:19:53 +01:00
|
|
|
int value = (tok2->next->str[1] ? 1 : 0) + atoi(Tokenizer::getstr(tok2, 2));
|
2008-03-24 08:24:49 +01:00
|
|
|
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..
|
2008-11-11 18:22:40 +01:00
|
|
|
while (tok2 && !Match(tok2,")"))
|
2008-03-24 08:24:49 +01:00
|
|
|
tok2 = tok2->next;
|
2008-11-09 08:19:53 +01:00
|
|
|
if (!Tokenizer::gettok(tok2,5))
|
2008-03-24 08:24:49 +01:00
|
|
|
break;
|
2008-03-24 17:24:07 +01:00
|
|
|
|
|
|
|
std::ostringstream pattern;
|
|
|
|
pattern << "%var1% [ " << strindex << " ]";
|
|
|
|
|
2008-03-24 08:24:49 +01:00
|
|
|
int indentlevel2 = 0;
|
|
|
|
while (tok2)
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-11-11 18:22:40 +01:00
|
|
|
if ( Match(tok2, ";") && indentlevel2 == 0 )
|
2008-03-23 17:18:31 +01:00
|
|
|
break;
|
2008-03-24 08:24:49 +01:00
|
|
|
|
2008-11-11 18:22:40 +01:00
|
|
|
if ( Match(tok2, "{") )
|
2008-03-24 08:24:49 +01:00
|
|
|
indentlevel2++;
|
|
|
|
|
2008-11-11 18:22:40 +01:00
|
|
|
if ( Match(tok2, "}") )
|
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-28 08:18:03 +01:00
|
|
|
if ( Match( tok2, pattern.str().c_str(), varname ) )
|
2008-03-24 08:24:49 +01:00
|
|
|
{
|
2008-03-24 17:24:07 +01:00
|
|
|
ReportError(tok2, "Buffer overrun");
|
|
|
|
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..
|
2008-03-28 08:18:03 +01:00
|
|
|
if ( Match(tok, "strcpy ( %var1% , %str% )", varname) )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
|
|
|
int len = 0;
|
2008-11-09 08:19:53 +01:00
|
|
|
const char *str = Tokenizer::getstr(tok, varc + 4 );
|
2008-03-24 17:24:07 +01:00
|
|
|
while ( *str )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-03-24 17:24:07 +01:00
|
|
|
if (*str=='\\')
|
2008-03-24 12:51:16 +01:00
|
|
|
str++;
|
2008-03-24 17:24:07 +01:00
|
|
|
str++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
if (len > 2 && len >= (int)size + 2)
|
|
|
|
{
|
|
|
|
ReportError(tok, "Buffer overrun");
|
2008-03-23 17:18:31 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Function call..
|
2008-04-01 08:21:12 +02:00
|
|
|
// It's not interesting to check what happens when the whole struct is
|
|
|
|
// sent as the parameter, that is checked separately anyway.
|
2008-03-28 08:18:03 +01:00
|
|
|
if ( Match( tok, "%var% (" ) )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-03-24 12:00:04 +01:00
|
|
|
// Don't make recursive checking..
|
|
|
|
if (std::find(CallStack.begin(), CallStack.end(), tok) != CallStack.end())
|
|
|
|
continue;
|
|
|
|
|
2008-03-24 11:03:45 +01:00
|
|
|
unsigned int parlevel = 0, par = 0;
|
|
|
|
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
|
|
|
|
{
|
2008-11-11 18:22:40 +01:00
|
|
|
if ( Match(tok2, "(") )
|
2008-03-24 11:03:45 +01:00
|
|
|
{
|
|
|
|
parlevel++;
|
|
|
|
}
|
|
|
|
|
2008-11-11 18:22:40 +01:00
|
|
|
else if ( Match(tok2, ")") )
|
2008-03-24 11:03:45 +01:00
|
|
|
{
|
|
|
|
parlevel--;
|
|
|
|
if ( parlevel < 1 )
|
|
|
|
{
|
|
|
|
par = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-11 18:22:40 +01:00
|
|
|
else if ( parlevel == 1 && Match(tok2, ",") )
|
2008-03-24 11:03:45 +01:00
|
|
|
{
|
|
|
|
par++;
|
|
|
|
}
|
|
|
|
|
2008-04-02 11:37:05 +02:00
|
|
|
if ( parlevel == 1 && Match(tok2, "[(,] %var1% [,)]", varname) )
|
2008-03-24 11:03:45 +01:00
|
|
|
{
|
|
|
|
par++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( par == 0 )
|
|
|
|
continue;
|
|
|
|
|
2008-03-23 17:18:31 +01:00
|
|
|
// Find function..
|
2008-03-25 18:10:52 +01:00
|
|
|
const TOKEN *ftok = GetFunctionTokenByName( tok->str );
|
2008-03-23 17:18:31 +01:00
|
|
|
if ( ! ftok )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Parse head of function..
|
2008-11-09 08:19:53 +01:00
|
|
|
ftok = Tokenizer::gettok( ftok, 2 );
|
2008-03-24 11:03:45 +01:00
|
|
|
parlevel = 1;
|
|
|
|
while ( ftok && parlevel == 1 && par >= 1 )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-11-11 18:22:40 +01:00
|
|
|
if ( Match(ftok, "(") )
|
2008-03-24 11:03:45 +01:00
|
|
|
parlevel++;
|
|
|
|
|
2008-11-11 18:22:40 +01:00
|
|
|
else if ( Match(ftok, ")") )
|
2008-03-24 11:03:45 +01:00
|
|
|
parlevel--;
|
|
|
|
|
2008-11-11 18:22:40 +01:00
|
|
|
else if ( Match(ftok, ",") )
|
2008-03-24 11:03:45 +01:00
|
|
|
par--;
|
|
|
|
|
2008-03-28 08:18:03 +01:00
|
|
|
else if (par==1 && parlevel==1 && (Match(ftok, "%var% ,") || Match(ftok, "%var% )")))
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-03-24 11:03:45 +01:00
|
|
|
// Parameter name..
|
2008-03-24 17:24:07 +01:00
|
|
|
const char *parname[2];
|
|
|
|
parname[0] = ftok->str;
|
|
|
|
parname[1] = 0;
|
2008-03-23 17:18:31 +01:00
|
|
|
|
2008-03-24 11:03:45 +01:00
|
|
|
// Goto function body..
|
2008-11-11 18:22:40 +01:00
|
|
|
while ( ftok && !Match(ftok,"{") )
|
2008-03-24 11:03:45 +01:00
|
|
|
ftok = ftok->next;
|
|
|
|
ftok = ftok ? ftok->next : 0;
|
|
|
|
|
|
|
|
// Check variable usage in the function..
|
2008-03-24 12:00:04 +01:00
|
|
|
CallStack.push_back( tok );
|
2008-03-24 17:24:07 +01:00
|
|
|
CheckBufferOverrun_CheckScope( ftok, parname, size, total_size );
|
2008-03-24 12:00:04 +01:00
|
|
|
CallStack.pop_back();
|
2008-03-24 11:03:45 +01:00
|
|
|
|
|
|
|
// break out..
|
2008-03-23 17:18:31 +01:00
|
|
|
break;
|
2008-03-24 11:03:45 +01:00
|
|
|
}
|
2008-03-23 17:18:31 +01:00
|
|
|
|
|
|
|
ftok = ftok->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-24 12:51:16 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checking local variables in a scope
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable()
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
2008-11-11 18:22:40 +01:00
|
|
|
if (Match(tok, "{"))
|
2008-03-23 17:18:31 +01:00
|
|
|
indentlevel++;
|
|
|
|
|
2008-11-11 18:22:40 +01:00
|
|
|
else if (Match(tok, "}"))
|
2008-03-23 17:18:31 +01:00
|
|
|
indentlevel--;
|
|
|
|
|
2008-08-12 08:39:41 +02:00
|
|
|
else if (indentlevel > 0)
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-08-12 08:39:41 +02:00
|
|
|
const char *varname[2] = {0};
|
|
|
|
unsigned int size = 0;
|
|
|
|
const char *type = 0;
|
|
|
|
|
|
|
|
if (Match(tok, "%type% %var% [ %num% ] ;"))
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
varname[0] = Tokenizer::getstr(tok,1);
|
|
|
|
size = strtoul(Tokenizer::getstr(tok,3), NULL, 10);
|
2008-08-12 08:39:41 +02:00
|
|
|
type = tok->str;
|
|
|
|
}
|
|
|
|
else if (indentlevel > 0 && Match(tok, "[*;{}] %var% = new %type% [ %num% ]"))
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
varname[0] = Tokenizer::getstr(tok,1);
|
|
|
|
size = strtoul(Tokenizer::getstr(tok,6), NULL, 10);
|
|
|
|
type = Tokenizer::getstr(tok, 4);
|
2008-08-12 08:39:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-09 08:19:53 +01:00
|
|
|
int total_size = size * Tokenizer::SizeOfType(type);
|
2008-03-23 17:18:31 +01:00
|
|
|
if (total_size == 0)
|
|
|
|
continue;
|
|
|
|
|
2008-03-24 12:00:04 +01:00
|
|
|
// The callstack is empty
|
|
|
|
CallStack.clear();
|
2008-11-09 08:19:53 +01:00
|
|
|
CheckBufferOverrun_CheckScope( Tokenizer::gettok(tok,5), varname, size, total_size );
|
2007-05-24 15:06:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-03-24 12:00:04 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checking member variables of structs..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
|
2008-03-21 18:25:25 +01:00
|
|
|
{
|
2008-03-27 06:41:21 +01:00
|
|
|
const char *declstruct_pattern[] = {"","","{",0};
|
2008-11-09 08:19:53 +01:00
|
|
|
for ( const TOKEN * tok = Tokenizer::findtoken( tokens, declstruct_pattern );
|
2008-03-21 18:25:25 +01:00
|
|
|
tok;
|
2008-11-09 08:19:53 +01:00
|
|
|
tok = Tokenizer::findtoken( tok->next, declstruct_pattern ) )
|
2008-03-21 18:25:25 +01:00
|
|
|
{
|
2008-11-11 18:22:40 +01:00
|
|
|
if (!Match(tok,"struct") && !Match(tok,"class"))
|
2008-03-27 06:41:21 +01:00
|
|
|
continue;
|
|
|
|
|
2008-03-21 18:25:25 +01:00
|
|
|
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 )
|
|
|
|
{
|
2008-11-11 18:22:40 +01:00
|
|
|
if ( Match(tok2, "}") )
|
2008-03-21 18:25:25 +01:00
|
|
|
break;
|
|
|
|
|
2008-11-11 18:22:40 +01:00
|
|
|
if (!Match(tok2,"[;{,(]"))
|
2008-05-10 15:48:15 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int ivar = 0;
|
|
|
|
if ( Match(tok2->next, "%type% %var% [ %num% ] ;") )
|
|
|
|
ivar = 2;
|
|
|
|
else if ( Match(tok2->next, "%type% %type% %var% [ %num% ] ;") )
|
|
|
|
ivar = 3;
|
|
|
|
else if ( Match(tok2->next, "%type% * %var% [ %num% ] ;") )
|
|
|
|
ivar = 3;
|
|
|
|
else if ( Match(tok2->next, "%type% %type% * %var% [ %num% ] ;") )
|
|
|
|
ivar = 4;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *varname[3] = {0,0,0};
|
2008-11-09 08:19:53 +01:00
|
|
|
varname[1] = Tokenizer::getstr(tok2, ivar);
|
|
|
|
int arrsize = atoi(Tokenizer::getstr(tok2, ivar+2));
|
|
|
|
int total_size = arrsize * Tokenizer::SizeOfType(tok2->next->str);
|
2008-05-10 15:48:15 +02:00
|
|
|
if (total_size == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for ( const TOKEN *tok3 = tokens; tok3; tok3 = tok3->next )
|
2008-03-21 19:55:29 +01:00
|
|
|
{
|
2008-05-10 15:48:15 +02:00
|
|
|
if ( strcmp(tok3->str, structname) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Declare variable: Fred fred1;
|
|
|
|
if ( Match( tok3->next, "%var% ;" ) )
|
2008-11-09 08:19:53 +01:00
|
|
|
varname[0] = Tokenizer::getstr(tok3, 1);
|
2008-05-10 15:48:15 +02:00
|
|
|
|
|
|
|
// Declare pointer: Fred *fred1
|
|
|
|
else if ( Match(tok3->next, "* %var% [,);=]") )
|
2008-11-09 08:19:53 +01:00
|
|
|
varname[0] = Tokenizer::getstr(tok3, 2);
|
2008-05-10 15:48:15 +02:00
|
|
|
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Goto end of statement.
|
|
|
|
const TOKEN *CheckTok = NULL;
|
|
|
|
while ( tok3 )
|
2008-03-21 18:25:25 +01:00
|
|
|
{
|
2008-05-10 15:48:15 +02:00
|
|
|
// End of statement.
|
2008-11-11 18:22:40 +01:00
|
|
|
if ( Match(tok3, ";") )
|
2008-05-10 15:48:15 +02:00
|
|
|
{
|
|
|
|
CheckTok = tok3;
|
|
|
|
break;
|
|
|
|
}
|
2008-03-27 06:41:21 +01:00
|
|
|
|
2008-05-10 15:48:15 +02:00
|
|
|
// End of function declaration..
|
|
|
|
if ( Match(tok3, ") ;") )
|
|
|
|
break;
|
2008-03-21 18:25:25 +01:00
|
|
|
|
2008-05-10 15:48:15 +02:00
|
|
|
// Function implementation..
|
|
|
|
if ( Match(tok3, ") {") )
|
2008-03-21 18:25:25 +01:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
CheckTok = Tokenizer::gettok(tok3, 2);
|
2008-05-10 15:48:15 +02:00
|
|
|
break;
|
2008-03-21 18:25:25 +01:00
|
|
|
}
|
2008-05-10 15:48:15 +02:00
|
|
|
|
|
|
|
tok3 = tok3->next;
|
2008-03-21 18:25:25 +01:00
|
|
|
}
|
2008-05-10 15:48:15 +02:00
|
|
|
|
|
|
|
if ( ! CheckTok )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check variable usage..
|
|
|
|
CheckBufferOverrun_CheckScope( CheckTok, varname, arrsize, total_size );
|
2008-03-21 18:25:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckBufferOverrunClass::CheckBufferOverrun()
|
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
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckBufferOverrunClass::WarningDangerousFunctions()
|
2007-05-25 08:44:53 +02:00
|
|
|
{
|
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
|
|
|
{
|
2008-03-28 08:18:03 +01:00
|
|
|
if (Match(tok, "gets ("))
|
2007-05-25 08:44:53 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Found 'gets'. You should use 'fgets' instead";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
|
2008-11-09 08:19:53 +01:00
|
|
|
else if (Match(tok, "scanf (") && strcmp(Tokenizer::getstr(tok,2),"\"%s\"") == 0)
|
2007-05-25 08:44:53 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Found 'scanf'. You should use 'fgets' instead";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
|