2nd attempt to fix crash in CheckUnusedFunctions::check. CheckBufferOverrun::checkBufferAllocatedWithStrlen: Don't check for 'new' in C code.

This commit is contained in:
Alexander Mai 2015-06-28 18:34:09 +02:00
parent 02a3a01eca
commit 0b225fa02f
4 changed files with 7 additions and 9 deletions

View File

@ -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);

View File

@ -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<std::string, FunctionUsage>::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<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger)
{
(void)fileInfo;
check(&errorLogger);
check(&errorLogger, settings);
}

View File

@ -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;

View File

@ -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());
}