diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 278653877..c24421cfa 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -587,12 +587,18 @@ void CheckMemoryLeakInFunction::parse_noreturn() if (indentlevel == 0) break; } - if (Token::Match(tok2, "[;{}] exit (")) + if (Token::Match(tok2->previous(), "[;{}] exit (")) { noreturn.insert(info->className); break; } } + + // This function is not a noreturn function + if (indentlevel == 0) + { + notnoreturn.insert(info->className); + } } } @@ -733,7 +739,17 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list it is not a noreturn function + if (tok->strAt(-1) == "=") + return NULL; + + // Function is not noreturn + if (notnoreturn.find(funcname) != notnoreturn.end()) + return NULL; + return (tok->previous()->str() != "=") ? "callfunc" : NULL; + } unsigned int par = 1; unsigned int parlevel = 0; diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index 75abc8bab..ecce46cb2 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -349,6 +349,9 @@ public: /** Function names for functions that are "noreturn" */ std::set noreturn; + /** Function names for functions that are not "noreturn" */ + std::set notnoreturn; + SymbolDatabase *symbolDatabase; }; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index ef900c103..2c7503fd2 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2619,6 +2619,16 @@ private: " fatal_error();\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void fatal_error()\n" // #2440 + "{ }\n" + "\n" + "void f()\n" + "{\n" + " char *p = malloc(100);\n" + " fatal_error();\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p\n", errout.str()); }