commit
813a193bb6
|
@ -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.");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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)";
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue