From 47a7c915fa146683e377f77735c92805b699dce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 10 Jan 2009 19:23:21 +0000 Subject: [PATCH] errmsg: Added error messages for "Array index out of bounds" and "Buffer overrun" --- Makefile | 2 +- src/checkbufferoverrun.cpp | 21 +++++++++++---------- src/checkbufferoverrun.h | 2 +- src/cppcheck.cpp | 6 ++++-- src/errormessage.h | 18 ++++++++++++++++++ tools/errmsg.cpp | 4 ++++ 6 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 985855622..53655ae44 100644 --- a/Makefile +++ b/Makefile @@ -86,7 +86,7 @@ install: cppcheck ###### Build -src/checkbufferoverrun.o: src/checkbufferoverrun.cpp src/checkbufferoverrun.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h +src/checkbufferoverrun.o: src/checkbufferoverrun.cpp src/checkbufferoverrun.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/errormessage.h g++ $(CXXFLAGS) -c -o src/checkbufferoverrun.o src/checkbufferoverrun.cpp src/checkclass.o: src/checkclass.cpp src/checkclass.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/errormessage.h diff --git a/src/checkbufferoverrun.cpp b/src/checkbufferoverrun.cpp index 02e22466b..9092026ab 100644 --- a/src/checkbufferoverrun.cpp +++ b/src/checkbufferoverrun.cpp @@ -21,6 +21,7 @@ //--------------------------------------------------------------------------- #include "checkbufferoverrun.h" +#include "errormessage.h" #include #include @@ -48,13 +49,13 @@ CheckBufferOverrunClass::~CheckBufferOverrunClass() } // Modified version of 'ReportError' that also reports the callstack -void CheckBufferOverrunClass::ReportError(const Token *tok, const char errmsg[]) +void CheckBufferOverrunClass::ReportError(const std::string &errmsg) { std::ostringstream ostr; std::list::const_iterator it; for (it = _callStack.begin(); it != _callStack.end(); it++) ostr << _tokenizer->fileLine(*it) << " -> "; - ostr << _tokenizer->fileLine(tok) << ": " << errmsg; + ostr << errmsg; _errorLogger->reportErr(ostr.str()); } //--------------------------------------------------------------------------- @@ -92,7 +93,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co const char *num = tok->strAt(2); if (strtol(num, NULL, 10) >= size) { - ReportError(tok->next(), "Array index out of bounds"); + ReportError(ErrorMessage::arrayIndexOutOfBounds(_tokenizer, tok->next())); } } } @@ -101,7 +102,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co const char *num = tok->strAt(2 + varc); if (strtol(num, NULL, 10) >= size) { - ReportError(tok->next(), "Array index out of bounds"); + ReportError(ErrorMessage::arrayIndexOutOfBounds(_tokenizer, tok->next())); } } @@ -129,7 +130,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co const char *num = tok->strAt(3); if (strtol(num, NULL, 10) >= size) { - ReportError(tok->next(), "Array index out of bounds"); + ReportError(ErrorMessage::arrayIndexOutOfBounds(_tokenizer, tok->next())); } } } @@ -138,7 +139,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co const char *num = tok->next()->strAt(2 + varc); if (strtol(num, NULL, 10) >= size) { - ReportError(tok->next(), "Array index out of bounds"); + ReportError(ErrorMessage::arrayIndexOutOfBounds(_tokenizer, tok->next())); } tok = tok->tokAt(4); continue; @@ -156,7 +157,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co const char *num = tok->strAt(6); if (atoi(num) > total_size) { - ReportError(tok, "Buffer overrun"); + ReportError(ErrorMessage::bufferOverrun(_tokenizer, tok)); } } continue; @@ -170,7 +171,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co const char *num = tok->strAt(varc + 6); if (atoi(num) > total_size) { - ReportError(tok, "Buffer overrun"); + ReportError(ErrorMessage::bufferOverrun(_tokenizer, tok)); } } continue; @@ -229,7 +230,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co if (Token::Match(tok2, pattern.str().c_str())) { - ReportError(tok2, "Buffer overrun"); + ReportError(ErrorMessage::bufferOverrun(_tokenizer, tok2)); break; } @@ -252,7 +253,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co } if (len > 2 && len >= (int)size + 2) { - ReportError(tok, "Buffer overrun"); + ReportError(ErrorMessage::bufferOverrun(_tokenizer, tok)); } continue; } diff --git a/src/checkbufferoverrun.h b/src/checkbufferoverrun.h index 08be6b8d1..21806f8fd 100644 --- a/src/checkbufferoverrun.h +++ b/src/checkbufferoverrun.h @@ -41,7 +41,7 @@ private: void CheckBufferOverrun_StructVariable(); void CheckBufferOverrun_LocalVariable(); void CheckBufferOverrun_CheckScope(const Token *tok, const char *varname[], const int size, const int total_size, unsigned int varid); - void ReportError(const Token *tok, const char errmsg[]); + void ReportError(const std::string &errmsg); const Tokenizer *_tokenizer; const Settings _settings; diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 7d85d227f..c01912800 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -279,10 +279,12 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) if (ErrorMessage::virtualDestructor(_settings)) checkClass.virtualDestructor(); + // Array index out of bounds / Buffer overruns.. + if (ErrorMessage::arrayIndexOutOfBounds(_settings) && ErrorMessage::bufferOverrun(_settings)) + checkBufferOverrun.bufferOverrun(); + if (_settings._showAll) { - // Buffer overruns.. - checkBufferOverrun.bufferOverrun(); // Check for "if (a=b)" checkOther.CheckIfAssignment(); diff --git a/src/errormessage.h b/src/errormessage.h index f883dd5f6..5afd1ff5c 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -30,6 +30,24 @@ private: ErrorMessage() { } static std::string msg1(const Tokenizer *tokenizer, const Token *Location); public: + static std::string arrayIndexOutOfBounds(const Tokenizer *tokenizer, const Token *Location) + { + return msg1(tokenizer, Location) + "Array index out of bounds"; + } + static bool arrayIndexOutOfBounds(const Settings &s) + { + return s._showAll; + } + + static std::string bufferOverrun(const Tokenizer *tokenizer, const Token *Location) + { + return msg1(tokenizer, Location) + "Buffer overrun"; + } + static bool bufferOverrun(const Settings &s) + { + return s._showAll; + } + static std::string noConstructor(const Tokenizer *tokenizer, const Token *Location, const std::string &classname) { return msg1(tokenizer, Location) + "The class '" + classname + "' has no constructor"; diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index c99464591..14dd3f221 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -53,6 +53,10 @@ int main() // Error messages.. std::list err; + // checkbufferoverrun.cpp + err.push_back(Message("arrayIndexOutOfBounds", Message::ALL, "Array index out of bounds")); + err.push_back(Message("bufferOverrun", Message::ALL, "Buffer overrun")); + // checkclass.cpp.. err.push_back(Message("noConstructor", Message::STYLE, "The class '%1' has no constructor", "classname")); err.push_back(Message("uninitVar", 0, "Uninitialized member variable '%1::%2'", "classname", "varname"));