Updated error message about not taking return value from function that returns allocated memory

This commit is contained in:
Pierre Schweitzer 2012-03-18 13:45:50 +01:00 committed by Daniel Marjamäki
parent 1e125dc017
commit 544a1f714e
2 changed files with 8 additions and 4 deletions

View File

@ -2878,7 +2878,7 @@ void CheckMemoryLeakNoVar::check()
} }
} }
// Handle the case were the user is calling a function returning something which is leaking // Handle the case where the user is calling an allocation function
// and never assigns the returned value to a variable, which will lead to a leak. // 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) else if (Token::Match(tok2, "[;{}] %var% (") && getAllocationType(tok2->next(), 0) != No)
missingAssignementLeak(tok2, tok2->next()->str()); missingAssignementLeak(tok2, tok2->next()->str());
@ -2893,7 +2893,11 @@ void CheckMemoryLeakNoVar::functionCallLeak(const Token *loc, const std::string
void CheckMemoryLeakNoVar::missingAssignementLeak(const Token *loc, const std::string &alloc) void CheckMemoryLeakNoVar::missingAssignementLeak(const Token *loc, const std::string &alloc)
{ {
reportError(loc, Severity::error, "leakNoVar", "Allocation with " + alloc + " never assigned."); reportError(loc, Severity::error, "leakNoVar",
"Return value of allocation function " + alloc + " is not used.\n"
"Return value of allocation function " + alloc + " is not used and this can lead to a memory leak. "
"When the allocation succeeds the function returns a pointer to the allocated memory which "
"needs to be freed.");
} }

View File

@ -5250,7 +5250,7 @@ private:
"{\n" "{\n"
" malloc(10);\n" " malloc(10);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Allocation with malloc never assigned.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (error) Return value of allocation function malloc is not used.\n", errout.str());
check("void *f()\n" check("void *f()\n"
"{\n" "{\n"
@ -5260,7 +5260,7 @@ private:
"{\n" "{\n"
" f();\n" " f();\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Allocation with f never assigned.\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Return value of allocation function f is not used.\n", errout.str());
} }
}; };
static TestMemleakNoVar testMemleakNoVar; static TestMemleakNoVar testMemleakNoVar;