diff --git a/src/check.h b/src/check.h index d8424c627..0eef58792 100644 --- a/src/check.h +++ b/src/check.h @@ -72,15 +72,26 @@ protected: /** report an error */ void reportError(const Token *tok, const std::string severity, const std::string id, const std::string msg) { - ErrorLogger::ErrorMessage::FileLocation loc; - loc.line = tok->linenr(); - loc.file = _tokenizer->file(tok); + std::list callstack; + callstack.push_back(tok); + reportError(callstack, severity, id, msg); + } + /** report an error */ + void reportError(const std::list callstack, const std::string severity, const std::string id, const std::string msg) + { std::list locationList; - locationList.push_back(loc); + for (std::list::const_iterator it = callstack.begin(); it != callstack.end(); ++it) + { + ErrorLogger::ErrorMessage::FileLocation loc; + loc.line = (*it)->linenr(); + loc.file = _tokenizer->file(*it); + locationList.push_back(loc); + } _errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList, severity, msg, id)); } + }; #endif diff --git a/src/checkautovariables.cpp b/src/checkautovariables.cpp index a1737f782..5e90e1fe8 100644 --- a/src/checkautovariables.cpp +++ b/src/checkautovariables.cpp @@ -189,23 +189,26 @@ void CheckAutoVariables::autoVariables() else if (bindent > 0 && Token::Match(tok, "%var% = & %var%")) //Critical assignement { if (error_av(tok->tokAt(0), tok->tokAt(3))) - _errorLogger->genericError(_tokenizer, - tok, - "Wrong assignement of an auto-variable to an effective parameter of a function"); + reportError(tok, + "error", + "autoVariables", + "Wrong assignement of an auto-variable to an effective parameter of a function"); } else if (bindent > 0 && Token::Match(tok, "%var% [ %any% ] = & %var%")) //Critical assignement { if (error_av(tok->tokAt(0), tok->tokAt(6))) - _errorLogger->genericError(_tokenizer, - tok, - "Wrong assignement of an auto-variable to an effective parameter of a function"); + reportError(tok, + "error", + "autoVariables", + "Wrong assignement of an auto-variable to an effective parameter of a function"); } else if (bindent > 0 && Token::Match(tok, "return & %var%")) //Critical return { if (is_auto_var(tok->tokAt(2))) - _errorLogger->genericError(_tokenizer, - tok, - "Return of the address of an auto-variable"); + reportError(tok, + "error", + "autoVariables", + "Return of the address of an auto-variable"); } } vd_list.clear(); diff --git a/src/checkbufferoverrun.cpp b/src/checkbufferoverrun.cpp index 50a8bccb0..09b29b177 100644 --- a/src/checkbufferoverrun.cpp +++ b/src/checkbufferoverrun.cpp @@ -47,9 +47,30 @@ CheckBufferOverrunClass instance; void CheckBufferOverrunClass::arrayIndexOutOfBounds(const Token *tok) { _callStack.push_back(tok); - _errorLogger->arrayIndexOutOfBounds(_tokenizer, _callStack); + arrayIndexOutOfBounds(); _callStack.pop_back(); } + +void CheckBufferOverrunClass::arrayIndexOutOfBounds() +{ + reportError(_callStack, "all", "arrayIndexOutOfBounds", "Array index out of bounds"); +} + +void CheckBufferOverrunClass::bufferOverrun(const Token *tok) +{ + reportError(tok, "all", "bufferOverrun", "Buffer overrun"); +} + +void CheckBufferOverrunClass::strncatUsage(const Token *tok) +{ + reportError(tok, "all", "strncatUsage", "Dangerous usage of strncat, possible buffer overrun"); +} + +void CheckBufferOverrunClass::outOfBounds(const Token *tok, const std::string &what) +{ + reportError(tok, "error", "outOfBounds", what + " is out of bounds"); +} + //--------------------------------------------------------------------------- @@ -148,7 +169,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co const char *num = tok->strAt(6); if (std::atoi(num) > total_size) { - _errorLogger->bufferOverrun(_tokenizer, tok); + bufferOverrun(tok); } } } @@ -161,7 +182,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co const char *num = tok->strAt(varc + 6); if (std::atoi(num) > total_size) { - _errorLogger->bufferOverrun(_tokenizer, tok); + bufferOverrun(tok); } } continue; @@ -220,7 +241,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co if (Token::Match(tok2, pattern.str().c_str())) { - _errorLogger->bufferOverrun(_tokenizer, tok2); + bufferOverrun(tok2); break; } @@ -243,7 +264,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co } if (len > 2 && len >= (int)size + 2) { - _errorLogger->bufferOverrun(_tokenizer, tok); + bufferOverrun(tok); } continue; } @@ -254,7 +275,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co { int n = atoi(tok->strAt(6)); if (n == size) - _errorLogger->strncatUsage(_tokenizer, tok); + strncatUsage(tok); } @@ -263,7 +284,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co { int n = atoi(tok->strAt(6)) + atoi(tok->strAt(15)); if (n > size) - _errorLogger->strncatUsage(_tokenizer, tok->tokAt(9)); + strncatUsage(tok->tokAt(9)); } @@ -288,7 +309,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co } if (len > (int)size) { - _errorLogger->bufferOverrun(_tokenizer, tok); + bufferOverrun(tok); } } @@ -297,13 +318,13 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co { int n = std::atoi(tok->strAt(4)); if (n > size) - _errorLogger->outOfBounds(_tokenizer, tok->tokAt(4), "snprintf size"); + outOfBounds(tok->tokAt(4), "snprintf size"); } // cin.. if (varid > 0 && Token::Match(tok, "cin >> %varid% ;", varid)) { - _errorLogger->bufferOverrun(_tokenizer, tok); + bufferOverrun(tok); } // Function call.. @@ -589,6 +610,3 @@ void CheckBufferOverrunClass::bufferOverrun() - - - diff --git a/src/checkbufferoverrun.h b/src/checkbufferoverrun.h index b8cfa648a..5430bffcd 100644 --- a/src/checkbufferoverrun.h +++ b/src/checkbufferoverrun.h @@ -68,6 +68,10 @@ private: std::list _callStack; void arrayIndexOutOfBounds(const Token *tok); + void arrayIndexOutOfBounds(); + void bufferOverrun(const Token *tok); + void strncatUsage(const Token *tok); + void outOfBounds(const Token *tok, const std::string &what); }; //--------------------------------------------------------------------------- diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 939a6fff6..81e1753ef 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -454,7 +454,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list if (alloc != Many && dealloctype != No && dealloctype != Many && dealloctype != alloc) { callstack.push_back(tok); - _errorLogger->mismatchAllocDealloc(_tokenizer, callstack, varname); + mismatchAllocDealloc(callstack, varname); callstack.pop_back(); } @@ -501,7 +501,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list if (dealloc != Many && alloctype != No && alloctype != Many && alloctype != dealloc) { callstack.push_back(tok); - _errorLogger->mismatchAllocDealloc(_tokenizer, callstack, varname); + mismatchAllocDealloc(callstack, varname); callstack.pop_back(); } dealloctype = dealloc; @@ -1499,7 +1499,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const char clas if (alloc != Many && Dealloc != No && Dealloc != Many && Dealloc != alloc) { callstack.push_back(tok); - _errorLogger->mismatchAllocDealloc(_tokenizer, callstack, (std::string(classname) + "::" + varname).c_str()); + mismatchAllocDealloc(callstack, (std::string(classname) + "::" + varname).c_str()); callstack.pop_back(); } @@ -1529,7 +1529,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const char clas if (dealloc != Many && Alloc != No && Alloc != Many && Alloc != dealloc) { callstack.push_back(tok); - _errorLogger->mismatchAllocDealloc(_tokenizer, callstack, (std::string(classname) + "::" + varname).c_str()); + mismatchAllocDealloc(callstack, (std::string(classname) + "::" + varname).c_str()); callstack.pop_back(); } @@ -1648,3 +1648,7 @@ void CheckMemoryLeakClass::mismatchSizeError(const Token *tok, const std::string reportError(tok, "error", "mismatchSize", "The given size " + sz + " is mismatching"); } +void CheckMemoryLeakClass::mismatchAllocDealloc(const std::list &callstack, const std::string &varname) +{ + reportError(callstack, "error", "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname); +} diff --git a/src/checkmemoryleak.h b/src/checkmemoryleak.h index 63fc676a6..3e5340f9b 100644 --- a/src/checkmemoryleak.h +++ b/src/checkmemoryleak.h @@ -131,7 +131,7 @@ private: void deallocDeallocError(const Token *tok, const std::string &varname); void deallocuseError(const Token *tok, const std::string &varname); void mismatchSizeError(const Token *tok, const std::string &sz); - + void mismatchAllocDealloc(const std::list &callstack, const std::string &varname); // Experimental functionality.. protected: diff --git a/src/errorlogger.h b/src/errorlogger.h index 23ed2ac71..092654efb 100644 --- a/src/errorlogger.h +++ b/src/errorlogger.h @@ -95,46 +95,23 @@ public: */ virtual void reportStatus(unsigned int index, unsigned int max) = 0; - void genericError(const Tokenizer *tokenizer, const Token *Location, const std::string &msg) - { - _writemsg(tokenizer, Location, "error", "" + msg + "", "genericError"); - } - static bool genericError() - { - return true; - } - void arrayIndexOutOfBounds(const Tokenizer *tokenizer, const std::list &Location) - { - _writemsg(tokenizer, Location, "all", "Array index out of bounds", "arrayIndexOutOfBounds"); - } static bool arrayIndexOutOfBounds(const Settings &s) { return s._showAll; } - void bufferOverrun(const Tokenizer *tokenizer, const Token *Location) - { - _writemsg(tokenizer, Location, "all", "Buffer overrun", "bufferOverrun"); - } static bool bufferOverrun(const Settings &s) { return s._showAll; } - void strncatUsage(const Tokenizer *tokenizer, const Token *Location) - { - _writemsg(tokenizer, Location, "all", "Dangerous usage of strncat, possible buffer overrun", "strncatUsage"); - } + static bool strncatUsage(const Settings &s) { return s._showAll; } - void outOfBounds(const Tokenizer *tokenizer, const Token *Location, const std::string &what) - { - _writemsg(tokenizer, Location, "error", "" + what + " is out of bounds", "outOfBounds"); - } static bool outOfBounds() { return true; @@ -189,10 +166,7 @@ public: return s._checkCodingStyle || s._showAll; } - void mismatchAllocDealloc(const Tokenizer *tokenizer, const std::list &Location, const std::string &varname) - { - _writemsg(tokenizer, Location, "error", "Mismatching allocation and deallocation: " + varname + "", "mismatchAllocDealloc"); - } + static bool mismatchAllocDealloc() { return true;