diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 62f9e94d0..25325a229 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -414,6 +414,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) { if (indentlevel <= 1) return No; + --indentlevel; } if (Token::Match(tok2, "return %var% ;")) { @@ -542,7 +543,9 @@ static int countParameters(const Token *tok) } else if (parlevel == 1 && tok->str() == ",") + { ++numpar; + } } return -1; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index ff4482bf2..0ef24e783 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -292,6 +292,7 @@ private: TEST_CASE(func15); TEST_CASE(func16); TEST_CASE(func17); + TEST_CASE(func18); TEST_CASE(allocfunc1); TEST_CASE(allocfunc2); @@ -1587,6 +1588,36 @@ private: } + void func18() + { + // No false positive + // The "free_pointers" will deallocate all pointers + check("static void free_pointers(int arg_count, ...)\n" + "{\n" + " va_list a;\n" + " va_start(a, arg_count);\n" + " for (int i = 0; i < arg_count; i++)\n" + " {\n" + " free(va_arg(a, void *));\n" + " }\n" + " va_end(a);\n" + "}\n" + "\n" + "static char* foo()\n" + "{\n" + " return strdup("");\n" + "}\n" + "\n" + "static void bar()\n" + "{\n" + " int *p = malloc(16);\n" + " free_pointers(1, p);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + + + void allocfunc1() {