Merge pull request #84 from HeisSpiter/master

Fix for bug #3439
This commit is contained in:
Daniel Marjamäki 2012-03-15 11:37:42 -07:00
commit 813a193bb6
3 changed files with 38 additions and 2 deletions

View File

@ -2879,6 +2879,11 @@ void CheckMemoryLeakNoVar::check()
}
}
}
// Handle the case were the user is calling a function returning something which is leaking
// and never assigns the returned value to a variable, which will lead to a leak.
else if (Token::Match(tok2, "[;{}] %var% (") && getAllocationType(tok2->next(), 0) != No)
missingAssignementLeak(tok2, tok2->next()->str());
}
}
}
@ -2888,4 +2893,9 @@ void CheckMemoryLeakNoVar::functionCallLeak(const Token *loc, const std::string
reportError(loc, Severity::error, "leakNoVarFunctionCall", "Allocation with " + alloc + ", " + functionCall + " doesn't release it.");
}
void CheckMemoryLeakNoVar::missingAssignementLeak(const Token *loc, const std::string &alloc)
{
reportError(loc, Severity::error, "leakNoVar", "Allocation with " + alloc + " never assigned.");
}

View File

@ -456,8 +456,14 @@ private:
void functionCallLeak(const Token *loc, const std::string &alloc, const std::string &functionCall);
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/) const
{ }
void missingAssignementLeak(const Token *loc, const std::string &alloc);
void getErrorMessages(ErrorLogger *e, const Settings *settings) const {
CheckMemoryLeakNoVar c(0, settings, e);
c.functionCallLeak(0, "funcName", "funcName");
c.missingAssignementLeak(0, "funcName");
}
std::string myName() const {
return "Memory leaks (address not taken)";

View File

@ -5198,6 +5198,8 @@ private:
void run() {
// pass allocated memory to function..
TEST_CASE(functionParameter);
// never use leakable resource
TEST_CASE(missingAssignement);
}
void functionParameter() {
@ -5235,5 +5237,23 @@ private:
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Allocation with strdup, mkstemp doesn't release it.\n", "", errout.str());
}
void missingAssignement() {
check("void x()\n"
"{\n"
" malloc(10);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Allocation with malloc never assigned.\n", errout.str());
check("void *f()\n"
"{\n"
" return malloc(10);\n"
"}\n"
"void x()\n"
"{\n"
" f();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Allocation with f never assigned.\n", errout.str());
}
};
static TestMemleakNoVar testMemleakNoVar;