Several improvements to CheckMemoryLeakNoVar::checkForUnusedReturnValue():

- Support user defined functions (solves TODO tests)
- Print message if return value is not stored properly (adapted message text, #6458)
This commit is contained in:
PKEuS 2015-01-31 17:27:43 +01:00
parent 73113c67e7
commit ae4b86c231
2 changed files with 16 additions and 11 deletions

View File

@ -2745,10 +2745,9 @@ void CheckMemoryLeakNoVar::check()
void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope) void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope)
{ {
for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "{|}|; %name% (") && tok->strAt(-1) != "=") { if (Token::Match(tok, "%name% (") && (!tok->next()->astParent() || tok->next()->astParent()->str() == "!" || tok->next()->astParent()->isComparisonOp())) {
tok = tok->next(); const AllocType allocType = getAllocationType(tok, 0);
const int allocationId = _settings->library.alloc(tok); if (allocType != No)
if (allocationId > 0)
returnValueNotUsedError(tok, tok->str()); returnValueNotUsedError(tok, tok->str());
} }
} }
@ -2807,7 +2806,7 @@ void CheckMemoryLeakNoVar::functionCallLeak(const Token *loc, const std::string
void CheckMemoryLeakNoVar::returnValueNotUsedError(const Token *tok, const std::string &alloc) void CheckMemoryLeakNoVar::returnValueNotUsedError(const Token *tok, const std::string &alloc)
{ {
reportError(tok, Severity::error, "leakReturnValNotUsed", "Return value of allocation function " + alloc + " is not used."); reportError(tok, Severity::error, "leakReturnValNotUsed", "Return value of allocation function " + alloc + " is not stored.");
} }
void CheckMemoryLeakNoVar::unsafeArgAllocError(const Token *tok, const std::string &funcName, const std::string &ptrType, const std::string& objType) void CheckMemoryLeakNoVar::unsafeArgAllocError(const Token *tok, const std::string &funcName, const std::string &ptrType, const std::string& objType)

View File

@ -6448,25 +6448,25 @@ private:
"{\n" "{\n"
" malloc(10);\n" " malloc(10);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not stored.\n", errout.str());
check("void x()\n" check("void x()\n"
"{\n" "{\n"
" calloc(10);\n" " calloc(10);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function calloc is not used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function calloc is not stored.\n", errout.str());
check("void x()\n" check("void x()\n"
"{\n" "{\n"
" strdup(\"Test\");\n" " strdup(\"Test\");\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function strdup is not used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function strdup is not stored.\n", errout.str());
check("void x()\n" check("void x()\n"
"{\n" "{\n"
" (char*) malloc(10);\n" " (char*) malloc(10);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not stored.\n", errout.str());
check("void x()\n" check("void x()\n"
"{\n" "{\n"
@ -6486,7 +6486,7 @@ private:
"{\n" "{\n"
" 42,malloc(42);\n" " 42,malloc(42);\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not used.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not stored.\n", errout.str());
check("void *f()\n" check("void *f()\n"
"{\n" "{\n"
@ -6496,7 +6496,13 @@ private:
"{\n" "{\n"
" f();\n" " f();\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Return value of allocation function f is not used.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (error) Return value of allocation function f is not stored.\n", errout.str());
check("void x()\n"
"{\n"
" if(!malloc(5)) fail();\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not stored.\n", errout.str());
} }
void smartPointerFunctionParam() { void smartPointerFunctionParam() {