Fixed #131 (Regression: leak reported by 1.27 is not reported by 1.29)

The leak will be reported as a definite bug again. Not just a possible bug.
cppcheck don't know if the execution path is possible, but it knows that if the execution path is taken the memory leak will be certain.
This commit is contained in:
Daniel Marjamäki 2009-06-16 19:05:05 +02:00
parent a81b6487bf
commit ed98f1b367
2 changed files with 25 additions and 10 deletions

View File

@ -298,7 +298,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok)
continue; continue;
} }
if (varname.empty() && Token::Match(tok,"%var% = ")) if (varname.empty() && Token::Match(tok, "%var% = "))
{ {
varname = tok->str(); varname = tok->str();
allocType = GetAllocationType(tok->tokAt(2)); allocType = GetAllocationType(tok->tokAt(2));
@ -1025,8 +1025,14 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
done = false; done = false;
} }
// Reduce "if if" => "if"
else if (Token::Match(tok2, "if if|callfunc"))
{
Token::eraseTokens(tok2, tok2->tokAt(2));
done = false;
}
if (Token::simpleMatch(tok2->next(), "if")) else if (Token::simpleMatch(tok2->next(), "if"))
{ {
// Delete empty if that is not followed by an else // Delete empty if that is not followed by an else
if (Token::Match(tok2->next(), "if ; !!else")) if (Token::Match(tok2->next(), "if ; !!else"))
@ -1075,13 +1081,6 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
done = false; done = false;
} }
// Reduce "if if" => "if"
else if (Token::simpleMatch(tok2, "if if"))
{
Token::eraseTokens(tok2, tok2->tokAt(2));
done = false;
}
// Reduce "if return ; alloc ;" => "alloc ;" // Reduce "if return ; alloc ;" => "alloc ;"
else if (Token::Match(tok2, "[;{}] if return ; alloc ;")) else if (Token::Match(tok2, "[;{}] if return ; alloc ;"))
{ {
@ -1487,7 +1486,7 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const char varname
} }
simplifycode(tok, all); simplifycode(tok, all);
// tok->printOut("simplifycode result"); //tok->printOut("simplifycode result");
// If the variable is not allocated at all => no memory leak // If the variable is not allocated at all => no memory leak
if (Token::findmatch(tok, "alloc") == 0) if (Token::findmatch(tok, "alloc") == 0)

View File

@ -203,6 +203,7 @@ private:
TEST_CASE(func12); TEST_CASE(func12);
TEST_CASE(func13); TEST_CASE(func13);
TEST_CASE(func14); TEST_CASE(func14);
TEST_CASE(func15);
TEST_CASE(allocfunc1); TEST_CASE(allocfunc1);
@ -1426,6 +1427,21 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void func15()
{
check("static void a()\n"
"{ return true; }\n"
"\n"
"static void b()\n"
"{\n"
" char *p = malloc(100);\n"
" if (a()) return;\n" // <- memory leak
" free(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: p\n", errout.str());
}
void allocfunc1() void allocfunc1()
{ {