Fixed #2434 (FP memleakOnRealloc)

This commit is contained in:
Zachary Blair 2011-01-12 22:33:46 -08:00
parent 09f900ce79
commit 38be7056b0
2 changed files with 28 additions and 4 deletions

View File

@ -2451,14 +2451,25 @@ 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()))
memleakUponReallocFailureError(tok, tok->str());
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());
}
}
}

View File

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