diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index bc3100957..60ddde8c6 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -118,21 +118,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, return No; if (!Token::Match(tok2, "%name% ::|. %type%")) { - // Does tok2 point on "malloc", "strdup" or "kmalloc".. - static const char * const mallocfunc[] = { - "malloc", - "calloc", - "strdup", - "strndup", - "kmalloc", - "kzalloc", - "kcalloc" - }; - for (unsigned int i = 0; i < sizeof(mallocfunc)/sizeof(*mallocfunc); i++) { - if (tok2->str() == mallocfunc[i]) - return Malloc; - } - // Using realloc.. if (varid && Token::Match(tok2, "realloc ( %any% ,") && tok2->tokAt(2)->varId() != varid) return Malloc; @@ -143,9 +128,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, return New; } - if (Token::Match(tok2, "fopen|tmpfile|g_fopen (")) - return File; - if (settings1->standards.posix) { if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp|socket (")) { // simple sanity check of function parameters.. @@ -164,7 +146,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, return Pipe; } - // Does tok2 point on "g_malloc", "g_strdup", .. + // Does tok2 point on a Library allocation function? const int alloctype = settings1->library.alloc(tok2); if (alloctype > 0) { if (alloctype == settings1->library.dealloc("free")) @@ -243,13 +225,9 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok vartok = vartok->tokAt(2); if (Token::Match(vartok, "%varid% )|,|-", varid)) { - if (Token::Match(tok, "free|kfree") || - (tok->str() == "realloc" && Token::simpleMatch(vartok->next(), ", 0 )"))) + if (tok->str() == "realloc" && Token::simpleMatch(vartok->next(), ", 0 )")) return Malloc; - if (tok->str() == "fclose") - return File; - if (settings1->standards.posix) { if (tok->str() == "close") return Fd; @@ -257,10 +235,15 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok return Pipe; } - // Does tok2 point on "g_free", etc .. + // Does tok point on a Library deallocation function? const int dealloctype = settings1->library.dealloc(tok); - if (dealloctype > 0) + if (dealloctype > 0) { + if (dealloctype == settings1->library.dealloc("free")) + return Malloc; + if (dealloctype == settings1->library.dealloc("fclose")) + return File; return Library::ismemory(dealloctype) ? OtherMem : OtherRes; + } } } @@ -2225,23 +2208,13 @@ void CheckMemoryLeakInClass::check() if (!var->isStatic() && var->isPointer()) { // allocation but no deallocation of private variables in public function.. const Token *tok = var->typeStartToken(); - if (tok->isStandardType()) { + // Either it is of standard type or a non-derived type + if (tok->isStandardType() || (var->type() && var->type()->derivedFrom.empty())) { if (var->isPrivate()) checkPublicFunctions(scope, var->nameToken()); variable(scope, var->nameToken()); } - - // known class? - else if (var->type()) { - // not derived? - if (var->type()->derivedFrom.empty()) { - if (var->isPrivate()) - checkPublicFunctions(scope, var->nameToken()); - - variable(scope, var->nameToken()); - } - } } } } @@ -2378,10 +2351,6 @@ void CheckMemoryLeakInClass::checkPublicFunctions(const Scope *scope, const Toke return; const unsigned int varid = classtok->varId(); - if (varid == 0) { - _tokenizer->getSymbolDatabase()->debugMessage(classtok, "CheckMemoryInClass::checkPublicFunctions found variable \'" + classtok->str() + "\' with varid 0"); - return; - } // Parse public functions.. // If they allocate member variables, they should also deallocate diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 2e0312fc6..71ecd140b 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -146,6 +146,7 @@ private: void run() { LOAD_LIB_2(settings1.library, "std.cfg"); LOAD_LIB_2(settings1.library, "gtk.cfg"); + LOAD_LIB_2(settings1.library, "posix.cfg"); LOAD_LIB_2(settings2.library, "std.cfg"); // Check that getcode works correctly.. @@ -4209,7 +4210,7 @@ private: check("void f() {\n" " char *p;\n" " char **pp = &p;\n" - " *pp = calloc(10);\n" + " *pp = calloc(10, 1);\n" "}"); ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str()); } @@ -4295,6 +4296,8 @@ private: settings.addEnabled("warning"); settings.addEnabled("style"); + LOAD_LIB_2(settings.library, "std.cfg"); + TEST_CASE(class1); TEST_CASE(class2); TEST_CASE(class3); @@ -5550,6 +5553,9 @@ private: } void run() { + LOAD_LIB_2(settings.library, "std.cfg"); + LOAD_LIB_2(settings.library, "posix.cfg"); + // testing that errors are detected TEST_CASE(err); @@ -5985,6 +5991,7 @@ private: settings.addEnabled("warning"); LOAD_LIB_2(settings.library, "std.cfg"); + LOAD_LIB_2(settings.library, "posix.cfg"); LOAD_LIB_2(settings.library, "gtk.cfg"); // pass allocated memory to function.. @@ -6095,7 +6102,7 @@ private: check("void x()\n" "{\n" - " calloc(10);\n" + " calloc(10, 1);\n" "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function 'calloc' is not stored.\n", errout.str()); diff --git a/test/testthreadexecutor.cpp b/test/testthreadexecutor.cpp index 1385f30f8..57842a07b 100644 --- a/test/testthreadexecutor.cpp +++ b/test/testthreadexecutor.cpp @@ -62,6 +62,8 @@ private: } void run() { + LOAD_LIB_2(settings.library, "std.cfg"); + TEST_CASE(deadlock_with_many_errors); TEST_CASE(many_threads); TEST_CASE(no_errors_more_files);