diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index aba91179d..c5e7bd436 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -661,7 +661,23 @@ bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname) const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list callstack, const unsigned int varid, AllocType &alloctype, AllocType &dealloctype, bool &allocpar, unsigned int sz) { if (test_white_list(tok->str())) - return 0; + { + if (tok->str() == "asprintf" || + tok->str() == "delete" || + tok->str() == "fclose" || + tok->str() == "for" || + tok->str() == "free" || + tok->str() == "if" || + tok->str() == "realloc" || + tok->str() == "return" || + tok->str() == "switch" || + tok->str() == "while") + { + return 0; + } + + return "use_"; + } if (noreturn.find(tok->str()) != noreturn.end()) return "exit"; @@ -1366,6 +1382,8 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listprevious(), "[;{}=(,+-*/] %varid% [", varid)) { + // warning is written for "dealloc ; use_ ;". + // but this use doesn't affect the leak-checking addtoken(&rettail, tok, "use_"); } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 295a33b42..0834dff15 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2273,6 +2273,13 @@ private: " str[10] = 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5]: (error) Dereferencing 'str' after it is deallocated / released\n", errout.str()); + + check("void foo() {\n" + " char *str = malloc(10);\n" + " free(str);\n" + " strcpy(str, p);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Dereferencing 'str' after it is deallocated / released\n", errout.str()); }