CheckBufferOverrun: increased constness of _tokenizer member

This commit is contained in:
Daniel Marjamäki 2008-11-22 19:47:10 +00:00
parent 07b5ebe72b
commit a60dad3562
4 changed files with 127 additions and 123 deletions

View File

@ -25,7 +25,7 @@
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include <list> #include <list>
#include <cstring> #include <cstring>
#include <stdlib.h> // <- strtoul #include <stdlib.h> // <- strtoul
@ -35,16 +35,16 @@
// CallStack used when parsing into subfunctions. // CallStack used when parsing into subfunctions.
CheckBufferOverrunClass::CheckBufferOverrunClass( Tokenizer *tokenizer, ErrorLogger *errorLogger ) CheckBufferOverrunClass::CheckBufferOverrunClass( const Tokenizer *tokenizer, ErrorLogger *errorLogger )
{ {
_tokenizer = tokenizer; _tokenizer = tokenizer;
_errorLogger = errorLogger; _errorLogger = errorLogger;
} }
CheckBufferOverrunClass::~CheckBufferOverrunClass() CheckBufferOverrunClass::~CheckBufferOverrunClass()
{ {
} }
// Modified version of 'ReportError' that also reports the callstack // Modified version of 'ReportError' that also reports the callstack
void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[]) 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); int total_size = arrsize * _tokenizer->SizeOfType(tok2->next->str);
if (total_size == 0) if (total_size == 0)
continue; continue;
// Class member variable => Check functions // Class member variable => Check functions
if ( Tokenizer::Match(tok, "class") ) if ( Tokenizer::Match(tok, "class") )
{ {
std::string func_pattern(structname + std::string(" :: %var% (")); std::string func_pattern(structname + std::string(" :: %var% ("));
const TOKEN *tok3 = Tokenizer::findmatch(_tokenizer->tokens(), func_pattern.c_str()); const TOKEN *tok3 = Tokenizer::findmatch(_tokenizer->tokens(), func_pattern.c_str());
while ( tok3 ) while ( tok3 )
{ {
for ( const TOKEN *tok4 = tok3; tok4; tok4 = tok4->next ) for ( const TOKEN *tok4 = tok3; tok4; tok4 = tok4->next )
{ {
if ( Tokenizer::Match(tok4,"[;{}]") ) if ( Tokenizer::Match(tok4,"[;{}]") )
break; break;
if ( Tokenizer::Match(tok4, ") {") ) if ( Tokenizer::Match(tok4, ") {") )
{ {
const char *names[2] = {varname[1], 0}; const char *names[2] = {varname[1], 0};
CheckBufferOverrun_CheckScope( Tokenizer::gettok(tok4, 2), names, arrsize, total_size ); CheckBufferOverrun_CheckScope( Tokenizer::gettok(tok4, 2), names, arrsize, total_size );
break; break;
} }
} }
tok3 = Tokenizer::findmatch(tok3->next, func_pattern.c_str()); tok3 = Tokenizer::findmatch(tok3->next, func_pattern.c_str());
} }
} }
for ( const TOKEN *tok3 = _tokenizer->tokens(); tok3; tok3 = tok3->next ) for ( const TOKEN *tok3 = _tokenizer->tokens(); tok3; tok3 = tok3->next )
{ {
@ -461,9 +461,9 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
} }
tok3 = tok3->next; tok3 = tok3->next;
} }
if ( ! tok3 ) if ( ! tok3 )
break; break;
if ( ! CheckTok ) if ( ! CheckTok )
@ -476,8 +476,8 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckBufferOverrunClass::CheckBufferOverrun() void CheckBufferOverrunClass::CheckBufferOverrun()
{ {

View File

@ -21,31 +21,31 @@
#ifndef CheckBufferOverrunH #ifndef CheckBufferOverrunH
#define CheckBufferOverrunH #define CheckBufferOverrunH
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h" #include "tokenize.h"
#include "errorlogger.h" #include "errorlogger.h"
class CheckBufferOverrunClass class CheckBufferOverrunClass
{ {
public: public:
CheckBufferOverrunClass( Tokenizer *tokenizer, ErrorLogger *errorLogger ); CheckBufferOverrunClass( const Tokenizer *tokenizer, ErrorLogger *errorLogger );
~CheckBufferOverrunClass(); ~CheckBufferOverrunClass();
// Buffer overrun.. // Buffer overrun..
void CheckBufferOverrun(); void CheckBufferOverrun();
// Dangerous functions that can cause buffer overruns // Dangerous functions that can cause buffer overruns
void WarningDangerousFunctions(); void WarningDangerousFunctions();
private: private:
void CheckBufferOverrun_StructVariable(); void CheckBufferOverrun_StructVariable();
void CheckBufferOverrun_LocalVariable(); void CheckBufferOverrun_LocalVariable();
void CheckBufferOverrun_CheckScope( const TOKEN *tok, const char *varname[], const int size, const int total_size ); void CheckBufferOverrun_CheckScope( const TOKEN *tok, const char *varname[], const int size, const int total_size );
void ReportError(const TOKEN *tok, const char errmsg[]); void ReportError(const TOKEN *tok, const char errmsg[]);
Tokenizer *_tokenizer; const Tokenizer *_tokenizer;
ErrorLogger *_errorLogger; ErrorLogger *_errorLogger;
std::list<const TOKEN *> CallStack; std::list<const TOKEN *> CallStack;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -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) if (!type)
return 0; 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")) while (!Tokenizer::Match(tok->next,"0"))
DeleteNextToken(tok); DeleteNextToken(tok);
} }
} }
for ( bool done = false; !done; done = true) for ( bool done = false; !done; done = true)
{ {
done &= simplifyConditions(); done &= simplifyConditions();
}; };
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool Tokenizer::simplifyConditions() bool Tokenizer::simplifyConditions()
{ {
bool ret = true; bool ret = true;
for ( TOKEN *tok = _tokens; tok; tok = tok->next ) for ( TOKEN *tok = _tokens; tok; tok = tok->next )
{ {
if (Match(tok, "( true &&") || Match(tok, "&& true &&") || Match(tok->next, "&& true )")) if (Match(tok, "( true &&") || Match(tok, "&& true &&") || Match(tok->next, "&& true )"))
{ {
DeleteNextToken( tok ); DeleteNextToken( tok );
DeleteNextToken( tok ); DeleteNextToken( tok );
ret = false; ret = false;
} }
else if (Match(tok, "( false ||") || Match(tok, "|| false ||") || Match(tok->next, "|| false )")) else if (Match(tok, "( false ||") || Match(tok, "|| false ||") || Match(tok->next, "|| false )"))
{ {
DeleteNextToken( tok ); DeleteNextToken( tok );
DeleteNextToken( tok ); DeleteNextToken( tok );
ret = false; ret = false;
} }
// Change numeric constant in condition to "true" or "false" // Change numeric constant in condition to "true" or "false"
const TOKEN *tok2 = gettok(tok, 2); const TOKEN *tok2 = gettok(tok, 2);
if ((Match(tok, "(") || Match(tok, "&&") || Match(tok, "||")) && if ((Match(tok, "(") || Match(tok, "&&") || Match(tok, "||")) &&
Match(tok->next, "%num%") && Match(tok->next, "%num%") &&
(Match(tok2, ")") || Match(tok2, "&&") || Match(tok2, "||")) ) (Match(tok2, ")") || Match(tok2, "&&") || Match(tok2, "||")) )
{ {
tok->next->setstr((strcmp(tok->next->str, "0")!=0) ? "true" : "false"); tok->next->setstr((strcmp(tok->next->str, "0")!=0) ? "true" : "false");
ret = false; ret = false;
} }
} }
return ret; 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; std::ostringstream ostr;
ostr << "[" << Files.at(tok->FileIndex) << ":" << tok->linenr << "]"; ostr << "[" << Files.at(tok->FileIndex) << ":" << tok->linenr << "]";

View File

@ -27,7 +27,7 @@
#include <vector> #include <vector>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include "settings.h" #include "settings.h"
#include "errorlogger.h" #include "errorlogger.h"
class TOKEN class TOKEN
@ -61,10 +61,10 @@ public:
}; };
class Tokenizer class Tokenizer
{ {
private: private:
// Deallocate lists.. // Deallocate lists..
void DeallocateTokens(); void DeallocateTokens();
public: public:
Tokenizer(); Tokenizer();
@ -82,19 +82,19 @@ public:
static const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]); static const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]);
static const TOKEN *gettok(const TOKEN *tok, int index); static const TOKEN *gettok(const TOKEN *tok, int index);
static const char *getstr(const TOKEN *tok, int index); static const char *getstr(const TOKEN *tok, int index);
static void deleteTokens(TOKEN *tok); static void deleteTokens(TOKEN *tok);
static const char *getParameterName( const TOKEN *ftok, int par ); 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 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 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 SameFileName( const char fname1[], const char fname2[] );
static bool IsName(const char str[]); static bool IsName(const char str[]);
static bool IsNumber(const char str[]); static bool IsNumber(const char str[]);
static bool IsStandardType(const char str[]); static bool IsStandardType(const char str[]);
std::string fileLine( const TOKEN *tok ); std::string fileLine( const TOKEN *tok ) const;
// Return size. // Return size.
int SizeOfType(const char type[]); int SizeOfType(const char type[]) const;
void initTokens(); void initTokens();
@ -103,7 +103,7 @@ public:
void FillFunctionList(const unsigned int file_id); void FillFunctionList(const unsigned int file_id);
const TOKEN *GetFunctionTokenByName( const char funcname[] ) const; const TOKEN *GetFunctionTokenByName( const char funcname[] ) const;
void settings( const Settings &settings ); void settings( const Settings &settings );
const TOKEN *tokens() const; const TOKEN *tokens() const;
#ifndef UNIT_TESTING #ifndef UNIT_TESTING
@ -123,8 +123,8 @@ private:
void combine_2tokens(TOKEN *tok, const char str1[], const char str2[]); void combine_2tokens(TOKEN *tok, const char str1[], const char str2[]);
void DeleteNextToken(TOKEN *tok); void DeleteNextToken(TOKEN *tok);
bool simplifyConditions(); bool simplifyConditions();
TOKEN *_gettok(TOKEN *tok, int index); TOKEN *_gettok(TOKEN *tok, int index);