memory leaks: Added checking of deallocate to see that the pointer isn't deallocated already
This commit is contained in:
parent
218c107b6f
commit
4cfb8ad1cb
|
@ -1014,8 +1014,8 @@ void CheckMemoryLeakClass::simplifycode(Token *tok)
|
|||
done = false;
|
||||
}
|
||||
|
||||
// Reduce "[;{}] alloc ; dealloc ;" => "[;{}]"
|
||||
if (Token::Match(tok2, "[;{}] alloc ; dealloc ;"))
|
||||
// Reduce "[;{}] alloc ; dealloc ; !!dealloc" => "[;{}]"
|
||||
if (Token::Match(tok2, "[;{}] alloc ; dealloc ; !!dealloc"))
|
||||
{
|
||||
erase(tok2, tok2->tokAt(5));
|
||||
done = false;
|
||||
|
@ -1177,7 +1177,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope(const Token *Tok1, const c
|
|||
}
|
||||
|
||||
simplifycode(tok);
|
||||
//tok->printOut( "simplifycode result" );
|
||||
tok->printOut("simplifycode result");
|
||||
|
||||
// If the variable is not allocated at all => no memory leak
|
||||
if (Token::findmatch(tok, "alloc") == 0)
|
||||
|
@ -1214,6 +1214,11 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope(const Token *Tok1, const c
|
|||
MemoryLeak(result->tokAt(2), varname, alloctype);
|
||||
}
|
||||
|
||||
else if ((result = Token::findmatch(tok, "dealloc ; dealloc ;")) != NULL)
|
||||
{
|
||||
_errorLogger->reportErr(ErrorMessage::deallocDealloc(_tokenizer, result->tokAt(2)));
|
||||
}
|
||||
|
||||
else if (! Token::findmatch(tok, "dealloc") &&
|
||||
! Token::findmatch(tok, "use") &&
|
||||
! Token::findmatch(tok, "return use ;"))
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"));
|
||||
|
|
Loading…
Reference in New Issue