From a60dad3562731678566406bfcfe00eb59381b8a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Nov 2008 19:47:10 +0000 Subject: [PATCH] CheckBufferOverrun: increased constness of _tokenizer member --- CheckBufferOverrun.cpp | 80 +++++++++++++++++------------------ CheckBufferOverrun.h | 38 ++++++++--------- tokenize.cpp | 94 ++++++++++++++++++++++-------------------- tokenize.h | 38 ++++++++--------- 4 files changed, 127 insertions(+), 123 deletions(-) diff --git a/CheckBufferOverrun.cpp b/CheckBufferOverrun.cpp index ea5372809..2cc7d1d99 100644 --- a/CheckBufferOverrun.cpp +++ b/CheckBufferOverrun.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include // <- 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() { diff --git a/CheckBufferOverrun.h b/CheckBufferOverrun.h index 2b170e2c2..1c8196100 100644 --- a/CheckBufferOverrun.h +++ b/CheckBufferOverrun.h @@ -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 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 CallStack; }; //--------------------------------------------------------------------------- diff --git a/tokenize.cpp b/tokenize.cpp index 55126b051..112565626 100644 --- a/tokenize.cpp +++ b/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::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 << "]"; diff --git a/tokenize.h b/tokenize.h index 868e5e8d1..4987e75db 100644 --- a/tokenize.h +++ b/tokenize.h @@ -27,7 +27,7 @@ #include #include #include -#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);