using deallocated pointer: detect first problem reported in ticket #2090

This commit is contained in:
Daniel Marjamäki 2010-10-25 17:36:46 +02:00
parent 26afb04dc5
commit 41a06a21d9
2 changed files with 26 additions and 1 deletions

View File

@ -661,7 +661,23 @@ bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname)
const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<const Token *> callstack, const unsigned int varid, AllocType &alloctype, AllocType &dealloctype, bool &allocpar, unsigned int sz)
{
if (test_white_list(tok->str()))
{
if (tok->str() == "asprintf" ||
tok->str() == "delete" ||
tok->str() == "fclose" ||
tok->str() == "for" ||
tok->str() == "free" ||
tok->str() == "if" ||
tok->str() == "realloc" ||
tok->str() == "return" ||
tok->str() == "switch" ||
tok->str() == "while")
{
return 0;
}
return "use_";
}
if (noreturn.find(tok->str()) != noreturn.end())
return "exit";
@ -1366,6 +1382,8 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
}
else if (Token::Match(tok->previous(), "[;{}=(,+-*/] %varid% [", varid))
{
// warning is written for "dealloc ; use_ ;".
// but this use doesn't affect the leak-checking
addtoken(&rettail, tok, "use_");
}
}

View File

@ -2273,6 +2273,13 @@ private:
" str[10] = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Dereferencing 'str' after it is deallocated / released\n", errout.str());
check("void foo() {\n"
" char *str = malloc(10);\n"
" free(str);\n"
" strcpy(str, p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Dereferencing 'str' after it is deallocated / released\n", errout.str());
}