Fixed #1440 (false negative: memory leak (function parameters))
This commit is contained in:
parent
8ccd95a643
commit
e068979d62
|
@ -2108,11 +2108,27 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string
|
|||
// Checks for memory leaks inside function..
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
void CheckMemoryLeakInFunction::parseFunctionScope(const Token *tok, const bool classmember)
|
||||
void CheckMemoryLeakInFunction::parseFunctionScope(const Token *tok, const Token *tok1, const bool classmember)
|
||||
{
|
||||
// Check locking/unlocking of global resources..
|
||||
checkScope(tok->next(), "", 0, classmember, 1);
|
||||
|
||||
// Locate parameters and check their usage..
|
||||
for (const Token *tok2 = tok1; tok2; tok2 = tok2->next())
|
||||
{
|
||||
if (tok2 == tok)
|
||||
break;
|
||||
if (tok2->str() == ")")
|
||||
break;
|
||||
if (Token::Match(tok2, "[(,] %type% * %var% [,)]") && tok2->next()->isStandardType())
|
||||
{
|
||||
const std::string varname(tok2->strAt(3));
|
||||
const unsigned int varid = tok2->tokAt(3)->varId();
|
||||
const unsigned int sz = _tokenizer->sizeOfType(tok->next());
|
||||
checkScope(tok->next(), varname, varid, classmember, sz);
|
||||
}
|
||||
}
|
||||
|
||||
// Locate variable declarations and check their usage..
|
||||
unsigned int indentlevel = 0;
|
||||
do
|
||||
|
@ -2187,9 +2203,10 @@ void CheckMemoryLeakInFunction::check()
|
|||
// Found a function scope
|
||||
if (Token::Match(tok, ") const| {"))
|
||||
{
|
||||
const Token * const tok1 = tok->link();
|
||||
while (tok->str() != "{")
|
||||
tok = tok->next();
|
||||
parseFunctionScope(tok, classmember);
|
||||
parseFunctionScope(tok, tok1, classmember);
|
||||
tok = tok->link();
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -193,10 +193,11 @@ public:
|
|||
|
||||
/**
|
||||
* @brief %Check all variables in function scope
|
||||
* @param tok The first '{' token of the function scope
|
||||
* @param tok The first '{' token of the function body
|
||||
* @param tok1 The '(' token in the function declaration
|
||||
* @param classmember Is this function a class member?
|
||||
*/
|
||||
void parseFunctionScope(const Token *tok, const bool classmember);
|
||||
void parseFunctionScope(const Token *tok, const Token *tok1, const bool classmember);
|
||||
|
||||
/**
|
||||
* @brief %Check if there is a "p = foo(p, .." and foo returns the argument (p)
|
||||
|
|
|
@ -381,6 +381,9 @@ private:
|
|||
TEST_CASE(knownFunctions);
|
||||
|
||||
TEST_CASE(same_function_name);
|
||||
|
||||
// #1440 - Check function parameters also..
|
||||
TEST_CASE(functionParameter);
|
||||
}
|
||||
|
||||
|
||||
|
@ -2688,6 +2691,15 @@ private:
|
|||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void functionParameter()
|
||||
{
|
||||
check("void a(char *p)\n"
|
||||
"{\n"
|
||||
" p = malloc(100);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
|
||||
}
|
||||
};
|
||||
|
||||
static TestMemleakInFunction testMemleakInFunction;
|
||||
|
|
Loading…
Reference in New Issue