Fixed #1889 (false positive: Common realloc mistake)

This commit is contained in:
Zachary Blair 2010-07-26 23:17:27 -07:00
parent 76fbbd7bd0
commit 9f44d9eb62
2 changed files with 19 additions and 1 deletions

View File

@ -2188,6 +2188,8 @@ void CheckMemoryLeakInFunction::checkReallocUsage()
const Token *tok = _tokenizer->tokens();
while ((tok = Token::findmatch(tok, ") const| {")))
{
const Token *startOfFunction = tok;
// Record the varid's of the function parameters
std::set<unsigned int> parameterVarIds;
for (tok = tok->link(); tok && tok->str() != "{"; tok = tok->next())
@ -2213,6 +2215,8 @@ void CheckMemoryLeakInFunction::checkReallocUsage()
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()))
memleakUponReallocFailureError(tok, tok->str());
}
}

View File

@ -330,6 +330,7 @@ private:
TEST_CASE(realloc5);
TEST_CASE(realloc6);
TEST_CASE(realloc7);
TEST_CASE(realloc8);
TEST_CASE(assign);
@ -2028,6 +2029,19 @@ private:
ASSERT_EQUALS("", errout.str());
}
void realloc8()
{
check("void foo()\n"
"{\n"
" char *origBuf = m_buf;\n"
" m_buf = (char *) realloc (m_buf, m_capacity + growBy);\n"
" if (!m_buf) {\n"
" m_buf = origBuf;\n"
" }\n"
"}\n", false);
ASSERT_EQUALS("", errout.str());
}
void assign()
{
check("void foo()\n"