mismatching allocation / deallocation: moved to --all

This commit is contained in:
Daniel Marjamäki 2009-01-31 17:54:31 +00:00
parent f6f72fc022
commit 81174a4817
5 changed files with 23 additions and 11 deletions

View File

@ -293,6 +293,8 @@ const char * CheckMemoryLeakClass::call_func(const Token *tok, std::list<const T
void CheckMemoryLeakClass::MismatchError(const Token *Tok1, const std::list<const Token *> &callstack, const char varname[])
{
if (! ErrorMessage::mismatchAllocDealloc(_settings))
return;
std::ostringstream errmsg;
for (std::list<const Token *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok)
errmsg << _tokenizer->fileLine(*tok) << " -> ";

View File

@ -367,7 +367,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// Memory leak
CheckMemoryLeakClass checkMemoryLeak(&_tokenizer, _settings, this);
if (ErrorMessage::memleak() || ErrorMessage::mismatchAllocDealloc())
if (ErrorMessage::memleak() || ErrorMessage::mismatchAllocDealloc(_settings))
checkMemoryLeak.CheckMemoryLeak();
// Check that all class constructors are ok.

View File

@ -132,11 +132,11 @@ public:
static std::string mismatchAllocDealloc(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{
return msg1(tokenizer, Location) + std::string("(always) ") + "Mismatching allocation and deallocation: " + varname + "";
return msg1(tokenizer, Location) + std::string("(all) ") + "Mismatching allocation and deallocation: " + varname + "";
}
static bool mismatchAllocDealloc()
static bool mismatchAllocDealloc(const Settings &s)
{
return true;
return s._showAll;
}
static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
@ -175,6 +175,15 @@ public:
return true;
}
static std::string deallocuse(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{
return msg1(tokenizer, Location) + std::string("(always) ") + "Using '" + varname + "' after it is deallocated / released";
}
static bool deallocuse()
{
return true;
}
static std::string cstyleCast(const Tokenizer *tokenizer, const Token *Location)
{
return msg1(tokenizer, Location) + std::string("(style) ") + "C-style pointer casting";

View File

@ -951,8 +951,8 @@ private:
"{\n"
" int *a = new int[10];\n"
" free(a);\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Mismatching allocation and deallocation: a\n"), errout.str());
"}\n", true);
ASSERT_EQUALS(std::string("[test.cpp:4]: (all) Mismatching allocation and deallocation: a\n"), errout.str());
}
void mismatch2()
@ -966,7 +966,7 @@ private:
"\n"
" fp = popen();\n"
" pclose(fp);\n"
"}\n");
"}\n", false);
ASSERT_EQUALS(std::string(""), errout.str());
}
@ -1041,9 +1041,9 @@ private:
"{\n"
" char *p = new char[100];\n"
" foo(p);\n"
"}\n");
"}\n", true);
std::string err(errout.str());
ASSERT_EQUALS(std::string("[test.cpp:9] -> [test.cpp:3]: (always) Mismatching allocation and deallocation: str\n"), err);
ASSERT_EQUALS(std::string("[test.cpp:9] -> [test.cpp:3]: (all) Mismatching allocation and deallocation: str\n"), err);
}
@ -1248,7 +1248,7 @@ private:
" free(str1);\n"
"}\n", true);
ASSERT_EQUALS(std::string("[test.cpp:17]: (always) Mismatching allocation and deallocation: Fred::str1\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:17]: (all) Mismatching allocation and deallocation: Fred::str1\n"), errout.str());
}
void class3()

View File

@ -75,11 +75,12 @@ int main()
err.push_back(Message("unusedFunction", Message::style_all, "[%1]: The function '%2' is never used", "filename", "funcname"));
// checkmemoryleak.cpp..
err.push_back(Message("mismatchAllocDealloc", Message::always, "Mismatching allocation and deallocation: %1", "varname"));
err.push_back(Message("mismatchAllocDealloc", Message::all, "Mismatching allocation and deallocation: %1", "varname"));
err.push_back(Message("memleak", Message::always, "Memory leak: %1", "varname"));
err.push_back(Message("memleakall", Message::all, "Memory leak: %1", "varname"));
err.push_back(Message("resourceLeak", Message::always, "Resource leak: %1", "varname"));
err.push_back(Message("deallocDealloc", Message::always, "Deallocating a deallocated pointer: %1", "varname"));
err.push_back(Message("deallocuse", Message::always, "Using '%1' after it is deallocated / released", "varname"));
// checkother.cpp..
err.push_back(Message("cstyleCast", Message::style, "C-style pointer casting"));