From 21dff96e7b5700bcbcacf2b960fa193452ea845d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Mar 2009 17:35:53 +0100 Subject: [PATCH] refactoring checkbufferoverrun --- src/checkbufferoverrun.cpp | 16 +--------------- src/checkbufferoverrun.h | 23 ++++++++++++++++------- src/cppcheck.cpp | 8 -------- test/testbufferoverrun.cpp | 2 +- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/checkbufferoverrun.cpp b/src/checkbufferoverrun.cpp index 3dfa76f42..4aef4686c 100644 --- a/src/checkbufferoverrun.cpp +++ b/src/checkbufferoverrun.cpp @@ -36,20 +36,6 @@ //--------------------------------------------------------------------------- -// _callStack used when parsing into subfunctions. - - -CheckBufferOverrunClass::CheckBufferOverrunClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger) - : _settings(settings) -{ - _tokenizer = tokenizer; - _errorLogger = errorLogger; -} - -CheckBufferOverrunClass::~CheckBufferOverrunClass() -{ - -} void CheckBufferOverrunClass::arrayIndexOutOfBounds(const Token *tok) { @@ -323,7 +309,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co continue; // Only perform this checking if showAll setting is enabled.. - if (!_settings._showAll) + if (!_settings->_showAll) continue; unsigned int parlevel = 0, par = 0; diff --git a/src/checkbufferoverrun.h b/src/checkbufferoverrun.h index 23213cb64..09261ff8e 100644 --- a/src/checkbufferoverrun.h +++ b/src/checkbufferoverrun.h @@ -23,6 +23,7 @@ #define CheckBufferOverrunH //--------------------------------------------------------------------------- +#include "check.h" #include "settings.h" #include @@ -30,11 +31,23 @@ class ErrorLogger; class Token; class Tokenizer; -class CheckBufferOverrunClass +class CheckBufferOverrunClass : public Check { public: - CheckBufferOverrunClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger); - ~CheckBufferOverrunClass(); + + CheckBufferOverrunClass() : Check() + { } + + CheckBufferOverrunClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) + : Check(tokenizer, settings, errorLogger) + { } + + void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) + { + CheckBufferOverrunClass checkBufferOverrunClass(tokenizer, settings, errorLogger); + if (settings->_showAll) + checkBufferOverrunClass.bufferOverrun(); + } /** Check for buffer overruns */ void bufferOverrun(); @@ -49,10 +62,6 @@ private: /** Check for buffer overruns - this is the function that performs the actual checking */ void CheckBufferOverrun_CheckScope(const Token *tok, const char *varname[], const int size, const int total_size, unsigned int varid); - const Tokenizer *_tokenizer; - const Settings _settings; - ErrorLogger *_errorLogger; - /** callstack - used during intra-function checking */ std::list _callStack; diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 8efb07ad9..d1d84946f 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -22,7 +22,6 @@ #include "tokenize.h" // <- Tokenizer #include "checkmemoryleak.h" -#include "checkbufferoverrun.h" #include "checkdangerousfunctions.h" #include "checkheaders.h" #include "checkother.h" @@ -402,9 +401,6 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) if (_settings._unusedFunctions) _checkFunctionUsage.parseTokens(_tokenizer); - // Class for detecting buffer overruns and related problems - CheckBufferOverrunClass checkBufferOverrun(&_tokenizer, _settings, this); - // Class for checking functions that should not be used CheckDangerousFunctionsClass checkDangerousFunctions(&_tokenizer, _settings, this); @@ -413,10 +409,6 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) if (ErrorLogger::memleak() || ErrorLogger::mismatchAllocDealloc()) checkMemoryLeak.CheckMemoryLeak(); - // Array index out of bounds / Buffer overruns.. - if (ErrorLogger::arrayIndexOutOfBounds(_settings) || ErrorLogger::bufferOverrun(_settings)) - checkBufferOverrun.bufferOverrun(); - // Warning upon c-style pointer casts if (ErrorLogger::cstyleCast(_settings)) { diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index a5f3e31bd..14be167b7 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -56,7 +56,7 @@ private: // Check for buffer overruns.. Settings settings; settings._showAll = true; - CheckBufferOverrunClass checkBufferOverrun(&tokenizer, settings, this); + CheckBufferOverrunClass checkBufferOverrun(&tokenizer, &settings, this); checkBufferOverrun.bufferOverrun(); }