CheckBufferOverrun: increased constness of _tokenizer member
This commit is contained in:
parent
07b5ebe72b
commit
a60dad3562
|
@ -25,7 +25,7 @@
|
|||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <list>
|
||||
#include <cstring>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
#include <stdlib.h> // <- strtoul
|
||||
|
@ -35,16 +35,16 @@
|
|||
// CallStack used when parsing into subfunctions.
|
||||
|
||||
|
||||
CheckBufferOverrunClass::CheckBufferOverrunClass( Tokenizer *tokenizer, ErrorLogger *errorLogger )
|
||||
{
|
||||
_tokenizer = tokenizer;
|
||||
_errorLogger = errorLogger;
|
||||
}
|
||||
|
||||
CheckBufferOverrunClass::~CheckBufferOverrunClass()
|
||||
{
|
||||
|
||||
}
|
||||
CheckBufferOverrunClass::CheckBufferOverrunClass( const Tokenizer *tokenizer, ErrorLogger *errorLogger )
|
||||
{
|
||||
_tokenizer = tokenizer;
|
||||
_errorLogger = errorLogger;
|
||||
}
|
||||
|
||||
CheckBufferOverrunClass::~CheckBufferOverrunClass()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Modified version of 'ReportError' that also reports the callstack
|
||||
void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[])
|
||||
|
@ -396,30 +396,30 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
|
|||
int total_size = arrsize * _tokenizer->SizeOfType(tok2->next->str);
|
||||
if (total_size == 0)
|
||||
continue;
|
||||
|
||||
|
||||
// Class member variable => Check functions
|
||||
if ( Tokenizer::Match(tok, "class") )
|
||||
{
|
||||
std::string func_pattern(structname + std::string(" :: %var% ("));
|
||||
const TOKEN *tok3 = Tokenizer::findmatch(_tokenizer->tokens(), func_pattern.c_str());
|
||||
while ( tok3 )
|
||||
{
|
||||
for ( const TOKEN *tok4 = tok3; tok4; tok4 = tok4->next )
|
||||
{
|
||||
if ( Tokenizer::Match(tok4,"[;{}]") )
|
||||
break;
|
||||
|
||||
if ( Tokenizer::Match(tok4, ") {") )
|
||||
{
|
||||
const char *names[2] = {varname[1], 0};
|
||||
CheckBufferOverrun_CheckScope( Tokenizer::gettok(tok4, 2), names, arrsize, total_size );
|
||||
break;
|
||||
}
|
||||
}
|
||||
tok3 = Tokenizer::findmatch(tok3->next, func_pattern.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Class member variable => Check functions
|
||||
if ( Tokenizer::Match(tok, "class") )
|
||||
{
|
||||
std::string func_pattern(structname + std::string(" :: %var% ("));
|
||||
const TOKEN *tok3 = Tokenizer::findmatch(_tokenizer->tokens(), func_pattern.c_str());
|
||||
while ( tok3 )
|
||||
{
|
||||
for ( const TOKEN *tok4 = tok3; tok4; tok4 = tok4->next )
|
||||
{
|
||||
if ( Tokenizer::Match(tok4,"[;{}]") )
|
||||
break;
|
||||
|
||||
if ( Tokenizer::Match(tok4, ") {") )
|
||||
{
|
||||
const char *names[2] = {varname[1], 0};
|
||||
CheckBufferOverrun_CheckScope( Tokenizer::gettok(tok4, 2), names, arrsize, total_size );
|
||||
break;
|
||||
}
|
||||
}
|
||||
tok3 = Tokenizer::findmatch(tok3->next, func_pattern.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
for ( const TOKEN *tok3 = _tokenizer->tokens(); tok3; tok3 = tok3->next )
|
||||
{
|
||||
|
@ -461,9 +461,9 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
|
|||
}
|
||||
|
||||
tok3 = tok3->next;
|
||||
}
|
||||
|
||||
if ( ! tok3 )
|
||||
}
|
||||
|
||||
if ( ! tok3 )
|
||||
break;
|
||||
|
||||
if ( ! CheckTok )
|
||||
|
@ -476,8 +476,8 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
|
|||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void CheckBufferOverrunClass::CheckBufferOverrun()
|
||||
{
|
||||
|
|
|
@ -21,31 +21,31 @@
|
|||
#ifndef CheckBufferOverrunH
|
||||
#define CheckBufferOverrunH
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#include "tokenize.h"
|
||||
#include "errorlogger.h"
|
||||
|
||||
class CheckBufferOverrunClass
|
||||
{
|
||||
public:
|
||||
CheckBufferOverrunClass( Tokenizer *tokenizer, ErrorLogger *errorLogger );
|
||||
~CheckBufferOverrunClass();
|
||||
|
||||
#include "tokenize.h"
|
||||
#include "errorlogger.h"
|
||||
|
||||
class CheckBufferOverrunClass
|
||||
{
|
||||
public:
|
||||
CheckBufferOverrunClass( const Tokenizer *tokenizer, ErrorLogger *errorLogger );
|
||||
~CheckBufferOverrunClass();
|
||||
|
||||
// Buffer overrun..
|
||||
void CheckBufferOverrun();
|
||||
|
||||
|
||||
// Dangerous functions that can cause buffer overruns
|
||||
void WarningDangerousFunctions();
|
||||
private:
|
||||
void CheckBufferOverrun_StructVariable();
|
||||
void CheckBufferOverrun_LocalVariable();
|
||||
void CheckBufferOverrun_CheckScope( const TOKEN *tok, const char *varname[], const int size, const int total_size );
|
||||
void ReportError(const TOKEN *tok, const char errmsg[]);
|
||||
|
||||
Tokenizer *_tokenizer;
|
||||
ErrorLogger *_errorLogger;
|
||||
std::list<const TOKEN *> CallStack;
|
||||
void WarningDangerousFunctions();
|
||||
private:
|
||||
void CheckBufferOverrun_StructVariable();
|
||||
void CheckBufferOverrun_LocalVariable();
|
||||
void CheckBufferOverrun_CheckScope( const TOKEN *tok, const char *varname[], const int size, const int total_size );
|
||||
void ReportError(const TOKEN *tok, const char errmsg[]);
|
||||
|
||||
const Tokenizer *_tokenizer;
|
||||
ErrorLogger *_errorLogger;
|
||||
std::list<const TOKEN *> CallStack;
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
|
94
tokenize.cpp
94
tokenize.cpp
|
@ -225,12 +225,16 @@ void Tokenizer::combine_2tokens(TOKEN *tok, const char str1[], const char str2[]
|
|||
|
||||
|
||||
|
||||
int Tokenizer::SizeOfType(const char type[])
|
||||
int Tokenizer::SizeOfType(const char type[]) const
|
||||
{
|
||||
if (!type)
|
||||
return 0;
|
||||
|
||||
return TypeSize[type];
|
||||
std::map<std::string, unsigned int>::const_iterator it = TypeSize.find(type);
|
||||
if ( it == TypeSize.end() )
|
||||
return 0;
|
||||
|
||||
return it->second;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
@ -1031,53 +1035,53 @@ void Tokenizer::SimplifyTokenList()
|
|||
while (!Tokenizer::Match(tok->next,"0"))
|
||||
DeleteNextToken(tok);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for ( bool done = false; !done; done = true)
|
||||
{
|
||||
done &= simplifyConditions();
|
||||
}
|
||||
|
||||
|
||||
for ( bool done = false; !done; done = true)
|
||||
{
|
||||
done &= simplifyConditions();
|
||||
};
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
bool Tokenizer::simplifyConditions()
|
||||
{
|
||||
bool ret = true;
|
||||
|
||||
for ( TOKEN *tok = _tokens; tok; tok = tok->next )
|
||||
{
|
||||
if (Match(tok, "( true &&") || Match(tok, "&& true &&") || Match(tok->next, "&& true )"))
|
||||
{
|
||||
DeleteNextToken( tok );
|
||||
DeleteNextToken( tok );
|
||||
ret = false;
|
||||
}
|
||||
|
||||
else if (Match(tok, "( false ||") || Match(tok, "|| false ||") || Match(tok->next, "|| false )"))
|
||||
{
|
||||
DeleteNextToken( tok );
|
||||
DeleteNextToken( tok );
|
||||
ret = false;
|
||||
}
|
||||
|
||||
// Change numeric constant in condition to "true" or "false"
|
||||
const TOKEN *tok2 = gettok(tok, 2);
|
||||
if ((Match(tok, "(") || Match(tok, "&&") || Match(tok, "||")) &&
|
||||
Match(tok->next, "%num%") &&
|
||||
(Match(tok2, ")") || Match(tok2, "&&") || Match(tok2, "||")) )
|
||||
{
|
||||
tok->next->setstr((strcmp(tok->next->str, "0")!=0) ? "true" : "false");
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Tokenizer::simplifyConditions()
|
||||
{
|
||||
bool ret = true;
|
||||
|
||||
for ( TOKEN *tok = _tokens; tok; tok = tok->next )
|
||||
{
|
||||
if (Match(tok, "( true &&") || Match(tok, "&& true &&") || Match(tok->next, "&& true )"))
|
||||
{
|
||||
DeleteNextToken( tok );
|
||||
DeleteNextToken( tok );
|
||||
ret = false;
|
||||
}
|
||||
|
||||
else if (Match(tok, "( false ||") || Match(tok, "|| false ||") || Match(tok->next, "|| false )"))
|
||||
{
|
||||
DeleteNextToken( tok );
|
||||
DeleteNextToken( tok );
|
||||
ret = false;
|
||||
}
|
||||
|
||||
// Change numeric constant in condition to "true" or "false"
|
||||
const TOKEN *tok2 = gettok(tok, 2);
|
||||
if ((Match(tok, "(") || Match(tok, "&&") || Match(tok, "||")) &&
|
||||
Match(tok->next, "%num%") &&
|
||||
(Match(tok2, ")") || Match(tok2, "&&") || Match(tok2, "||")) )
|
||||
{
|
||||
tok->next->setstr((strcmp(tok->next->str, "0")!=0) ? "true" : "false");
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1310,7 +1314,7 @@ const TOKEN *Tokenizer::findmatch(const TOKEN *tok, const char pattern[], const
|
|||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
std::string Tokenizer::fileLine( const TOKEN *tok )
|
||||
std::string Tokenizer::fileLine( const TOKEN *tok ) const
|
||||
{
|
||||
std::ostringstream ostr;
|
||||
ostr << "[" << Files.at(tok->FileIndex) << ":" << tok->linenr << "]";
|
||||
|
|
38
tokenize.h
38
tokenize.h
|
@ -27,7 +27,7 @@
|
|||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include "settings.h"
|
||||
#include "settings.h"
|
||||
#include "errorlogger.h"
|
||||
|
||||
class TOKEN
|
||||
|
@ -61,10 +61,10 @@ public:
|
|||
};
|
||||
|
||||
class Tokenizer
|
||||
{
|
||||
private:
|
||||
// Deallocate lists..
|
||||
void DeallocateTokens();
|
||||
{
|
||||
private:
|
||||
// Deallocate lists..
|
||||
void DeallocateTokens();
|
||||
|
||||
public:
|
||||
Tokenizer();
|
||||
|
@ -82,19 +82,19 @@ public:
|
|||
static const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]);
|
||||
static const TOKEN *gettok(const TOKEN *tok, int index);
|
||||
static const char *getstr(const TOKEN *tok, int index);
|
||||
static void deleteTokens(TOKEN *tok);
|
||||
static const char *getParameterName( const TOKEN *ftok, int par );
|
||||
static const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
|
||||
static bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
|
||||
static bool SameFileName( const char fname1[], const char fname2[] );
|
||||
static bool IsName(const char str[]);
|
||||
static bool IsNumber(const char str[]);
|
||||
static bool IsStandardType(const char str[]);
|
||||
|
||||
std::string fileLine( const TOKEN *tok );
|
||||
static void deleteTokens(TOKEN *tok);
|
||||
static const char *getParameterName( const TOKEN *ftok, int par );
|
||||
static const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
|
||||
static bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
|
||||
static bool SameFileName( const char fname1[], const char fname2[] );
|
||||
static bool IsName(const char str[]);
|
||||
static bool IsNumber(const char str[]);
|
||||
static bool IsStandardType(const char str[]);
|
||||
|
||||
std::string fileLine( const TOKEN *tok ) const;
|
||||
|
||||
// Return size.
|
||||
int SizeOfType(const char type[]);
|
||||
int SizeOfType(const char type[]) const;
|
||||
|
||||
void initTokens();
|
||||
|
||||
|
@ -103,7 +103,7 @@ public:
|
|||
void FillFunctionList(const unsigned int file_id);
|
||||
const TOKEN *GetFunctionTokenByName( const char funcname[] ) const;
|
||||
void settings( const Settings &settings );
|
||||
const TOKEN *tokens() const;
|
||||
const TOKEN *tokens() const;
|
||||
|
||||
|
||||
#ifndef UNIT_TESTING
|
||||
|
@ -123,8 +123,8 @@ private:
|
|||
|
||||
void combine_2tokens(TOKEN *tok, const char str1[], const char str2[]);
|
||||
|
||||
void DeleteNextToken(TOKEN *tok);
|
||||
|
||||
void DeleteNextToken(TOKEN *tok);
|
||||
|
||||
bool simplifyConditions();
|
||||
|
||||
TOKEN *_gettok(TOKEN *tok, int index);
|
||||
|
|
Loading…
Reference in New Issue