From 0b225fa02f51583aa439501910dd16f601090690 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Sun, 28 Jun 2015 18:34:09 +0200 Subject: [PATCH] 2nd attempt to fix crash in CheckUnusedFunctions::check. CheckBufferOverrun::checkBufferAllocatedWithStrlen: Don't check for 'new' in C code. --- lib/checkbufferoverrun.cpp | 2 +- lib/checkunusedfunctions.cpp | 6 +++--- lib/checkunusedfunctions.h | 2 +- test/testunusedfunctions.cpp | 6 ++---- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index cdc297bca..79c694ded 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1520,7 +1520,7 @@ void CheckBufferOverrun::checkBufferAllocatedWithStrlen() dstVarId = tok->varId(); srcVarId = tok->tokAt(6)->varId(); tok = tok->tokAt(8); - } else if (Token::Match(tok, "%var% = new char [ strlen ( %name% ) ]")) { + } else if (_tokenizer->isCPP() && Token::Match(tok, "%var% = new char [ strlen ( %name% ) ]")) { dstVarId = tok->varId(); srcVarId = tok->tokAt(7)->varId(); tok = tok->tokAt(9); diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 4033a0af4..ccf3ca87c 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -209,14 +209,14 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi -void CheckUnusedFunctions::check(ErrorLogger * const errorLogger) +void CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings& settings) { for (std::map::const_iterator it = _functions.begin(); it != _functions.end(); ++it) { const FunctionUsage &func = it->second; if (func.usedOtherFile || func.filename.empty()) continue; if (it->first == "main" || - (_settings->isWindowsPlatform() && (it->first == "WinMain" || it->first == "_tmain")) || + (settings.isWindowsPlatform() && (it->first == "WinMain" || it->first == "_tmain")) || it->first == "if" || (it->first.compare(0, 8, "operator") == 0 && it->first.size() > 8 && !std::isalnum(it->first[8]))) continue; @@ -268,5 +268,5 @@ Check::FileInfo *CheckUnusedFunctions::getFileInfo(const Tokenizer *tokenizer, c void CheckUnusedFunctions::analyseWholeProgram(const std::list &fileInfo, const Settings& settings, ErrorLogger &errorLogger) { (void)fileInfo; - check(&errorLogger); + check(&errorLogger, settings); } diff --git a/lib/checkunusedfunctions.h b/lib/checkunusedfunctions.h index 7ad791399..1493de95a 100644 --- a/lib/checkunusedfunctions.h +++ b/lib/checkunusedfunctions.h @@ -45,7 +45,7 @@ public: // * What functions are declared void parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings); - void check(ErrorLogger * const errorLogger); + void check(ErrorLogger * const errorLogger, const Settings& settings); /** @brief Parse current TU and extract file info */ Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const; diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index e12e8482e..c7d606016 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -72,7 +72,7 @@ private: // Check for unused functions.. CheckUnusedFunctions checkUnusedFunctions(&tokenizer, &settings, this); checkUnusedFunctions.parseTokens(tokenizer, "someFile.c", &settings); - checkUnusedFunctions.check(this); + checkUnusedFunctions.check(this, settings); } void incondition() { @@ -349,8 +349,6 @@ private: // Clear the error buffer.. errout.str(""); - Settings settings; - Tokenizer tokenizer2(&settings, this); std::istringstream istr(code); tokenizer2.tokenize(istr, fname.str().c_str()); @@ -359,7 +357,7 @@ private: } // Check for unused functions.. - c.check(this); + c.check(this, settings); ASSERT_EQUALS("[test1.cpp:1]: (style) The function 'f' is never used.\n", errout.str()); }