memory leaks: Added checking of deallocate to see that the pointer isn't deallocated already

This commit is contained in:
Daniel Marjamäki 2009-01-15 20:34:39 +00:00
parent 218c107b6f
commit 4cfb8ad1cb
4 changed files with 1569 additions and 1539 deletions

File diff suppressed because it is too large Load Diff

View File

@ -147,6 +147,15 @@ public:
return true;
}
static std::string deallocDealloc(const Tokenizer *tokenizer, const Token *Location)
{
return msg1(tokenizer, Location) + "Deallocating a deallocated pointer";
}
static bool deallocDealloc(const Settings &s)
{
return true;
}
static std::string cstyleCast(const Tokenizer *tokenizer, const Token *Location)
{
return msg1(tokenizer, Location) + "C-style pointer casting";

View File

@ -165,6 +165,9 @@ private:
TEST_CASE(dealloc_use_4);
TEST_CASE(dealloc_use_5);
TEST_CASE(dealloc_use_6);
// free a free'd pointer
TEST_CASE(freefree);
}
@ -1587,6 +1590,18 @@ private:
ASSERT_EQUALS(std::string(""), errout.str());
}
void freefree()
{
check("void foo()\n"
"{\n"
" char *str = malloc(100);\n"
" free(str);\n"
" free(str);\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:5]: Deallocating a deallocated pointer\n"), errout.str());
}
};
REGISTER_TEST(TestMemleak)

View File

@ -72,6 +72,7 @@ int main()
err.push_back(Message("mismatchAllocDealloc", Message::std, "Mismatching allocation and deallocation: %1", "varname"));
err.push_back(Message("memleak", Message::std, "Memory leak: %1", "varname"));
err.push_back(Message("resourceLeak", Message::std, "Resource leak: %1", "varname"));
err.push_back(Message("deallocDealloc", Message::std, "Deallocating a deallocated pointer"));
// checkother.cpp..
err.push_back(Message("cstyleCast", Message::style, "C-style pointer casting"));