From a80101f27734dd2ea6569185e2e299b2af8e2a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 5 Jan 2015 13:23:38 +0100 Subject: [PATCH] CheckMemoryLeak: Fix FP for allocation functions that register memory before returning it --- lib/checkmemoryleak.cpp | 7 +++++-- test/testmemleak.cpp | 14 ++++++++++++++ test/testother.cpp | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 8944744ab..33c360018 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -449,8 +449,11 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Function* f if (Token::Match(tok, "[(,] %varid% [,)]", varid)) { return No; } - if (tok->str() == "return") - return allocType; + if (Token::Match(tok, "[(,] & %varid% [.,)]", varid)) { + return No; + } + if (allocType == No && tok->str() == "return") + return No; } return allocType; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 514ffd411..4a08a6f5c 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -253,6 +253,7 @@ private: TEST_CASE(allocfunc11); TEST_CASE(allocfunc12); // #3660: allocating and returning non-local pointer => not allocfunc TEST_CASE(allocfunc13); // Ticket #4494 and #4540 - class function + TEST_CASE(allocfunc14); // Use pointer before returning it TEST_CASE(throw1); TEST_CASE(throw2); @@ -2827,6 +2828,19 @@ private: ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: a\n", errout.str()); } + void allocfunc14() { // use pointer before returning it + check("static struct ABC * newabc() {\n" + " struct ABC *abc = malloc(sizeof(struct ABC));\n" + " init_abc(&abc->a);\n" // <- might take address + " return abc;\n" + "}\n" + "\n" + "static void f() {\n" + " struct ABC *abc = newabc();\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void throw1() { check("void foo()\n" "{\n" diff --git a/test/testother.cpp b/test/testother.cpp index 01630ff2e..7609f644f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5490,7 +5490,7 @@ private: " std::cout << 3 << -1 ;\n" "}"); ASSERT_EQUALS("", errout.str()); - + check("x = y ? z << $-1 : 0;\n"); ASSERT_EQUALS("", errout.str()); }