From a768b0e8b2bc235c7189504d18e0150c8f48b04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 17 Jul 2012 16:28:34 +0200 Subject: [PATCH] Fixed #3954 (Pointer reference memory leak false positive) --- lib/checkleakautovar.cpp | 4 ++++ lib/checkmemoryleak.cpp | 2 +- test/testleakautovar.cpp | 9 +++++++++ test/testmemleak.cpp | 8 ++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 4df223d69..c8e93a3b2 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -311,6 +311,10 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, continue; } + // Don't check reference variables + if (var && var->isReference()) + continue; + // allocation? if (Token::Match(tok->tokAt(2), "%type% (")) { const std::map::const_iterator it = allocFunctions.find(tok->strAt(2)); diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index f1e81cbf3..f951b876b 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2237,7 +2237,7 @@ void CheckMemoryLeakInFunction::check() if (!var || (!var->isLocal() && !var->isArgument()) || var->isStatic() || !var->scope()) continue; - if (var->isArgument() && var->isReference()) + if (var->isReference()) continue; if (!var->isPointer() && var->typeStartToken()->str() != "int") diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index b45c38720..14436c5c4 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -85,6 +85,7 @@ private: // General tests: variable type, allocation type, etc TEST_CASE(test1); TEST_CASE(test2); + TEST_CASE(test3); // #3954 - reference pointer // Possible leak => Further configuration is needed for complete analysis TEST_CASE(configuration1); @@ -471,6 +472,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void test3() { // 3954 - reference pointer + check("void f() {\n" + " char *&p = x();\n" + " p = malloc(10);\n" + "};"); + ASSERT_EQUALS("", errout.str()); + } + void configuration1() { // Possible leak => configuration is required for complete analysis // The user should be able to "white list" and "black list" functions. diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 1390a7bc0..bf4bbb2a0 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -163,6 +163,7 @@ private: TEST_CASE(staticvar); TEST_CASE(externvar); + TEST_CASE(referencevar); // 3954 - false positive for reference pointer TEST_CASE(alloc_alloc_1); @@ -1005,6 +1006,13 @@ private: ASSERT_EQUALS("", errout.str()); } + void referencevar() { // 3954 - false positive for reference pointer + check("void f() {\n" + " char *&x = get();\n" + " x = malloc(100);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } void alloc_alloc_1() {