Fixed #2434 (FP memleakOnRealloc)
This commit is contained in:
parent
09f900ce79
commit
38be7056b0
|
@ -2451,13 +2451,24 @@ void CheckMemoryLeakInFunction::checkReallocUsage()
|
|||
}
|
||||
|
||||
if (tok->varId() > 0 &&
|
||||
Token::Match(tok, "%var% = realloc|g_try_realloc ( %var% ,") &&
|
||||
Token::Match(tok, "%var% = realloc|g_try_realloc ( %var% , %any% ) ;|}") &&
|
||||
tok->varId() == tok->tokAt(4)->varId() &&
|
||||
parameterVarIds.find(tok->varId()) == parameterVarIds.end())
|
||||
{
|
||||
// Check that another copy of the pointer wasn't saved earlier in the function
|
||||
if (!Token::findmatch(startOfFunction, "%var% = %varid% ;", tok->varId()) &&
|
||||
!Token::findmatch(startOfFunction, "[{};] %varid% = %var% [;=]", tok->varId()))
|
||||
if (Token::findmatch(startOfFunction, "%var% = %varid% ;", tok->varId()) ||
|
||||
Token::findmatch(startOfFunction, "[{};] %varid% = %var% [;=]", tok->varId()))
|
||||
continue;
|
||||
|
||||
// Check that the allocation isn't followed immediately by an 'if (!var) { error(); }' that might handle failure
|
||||
if (Token::Match(tok->tokAt(9), "if ( ! %varid% ) {", tok->varId()))
|
||||
{
|
||||
const Token* tokEndBrace = tok->tokAt(14)->link();
|
||||
if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") &&
|
||||
Token::Match(tokEndBrace->tokAt(-2)->link()->tokAt(-2), "{|}|; %var% ("))
|
||||
continue;
|
||||
}
|
||||
|
||||
memleakUponReallocFailureError(tok, tok->str());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -253,6 +253,7 @@ private:
|
|||
TEST_CASE(realloc8);
|
||||
TEST_CASE(realloc9);
|
||||
TEST_CASE(realloc10);
|
||||
TEST_CASE(realloc11);
|
||||
|
||||
TEST_CASE(assign);
|
||||
|
||||
|
@ -2150,6 +2151,18 @@ private:
|
|||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void realloc11()
|
||||
{
|
||||
check("void foo() {\n"
|
||||
" char *p;\n"
|
||||
" p = realloc(p, size);\n"
|
||||
" if (!p)\n"
|
||||
" error();\n"
|
||||
" usep(p);\n"
|
||||
"}\n", false);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void assign()
|
||||
{
|
||||
check("void foo()\n"
|
||||
|
|
Loading…
Reference in New Issue