From d3e990b1e59dba3c93bdeb7c66d852f54d83c024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Sep 2012 10:37:27 +0200 Subject: [PATCH] Fixed #3987 (False positive: Memory leak reported when throwing/catching) --- lib/checkleakautovar.cpp | 8 +++++++- test/testleakautovar.cpp | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 7b77ad0b7..e5018d735 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -462,10 +462,16 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, } // return - else if (tok->str() == "return" || tok->str() == "throw") { + else if (tok->str() == "return") { ret(tok, *varInfo); varInfo->clear(); } + + // throw + // TODO: if the execution leave the function then treat it as return + else if (tok->str() == "throw") { + varInfo->clear(); + } } } diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index b1160b192..10d3031a5 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -90,6 +90,9 @@ private: TEST_CASE(test2); TEST_CASE(test3); // #3954 - reference pointer + // Execution reaches a 'throw' + TEST_CASE(throw1); + // Possible leak => Further configuration is needed for complete analysis TEST_CASE(configuration1); TEST_CASE(configuration2); @@ -509,6 +512,24 @@ private: ASSERT_EQUALS("", errout.str()); } + void throw1() { // 3987 - Execution reach a 'throw' + check("void f() {\n" + " char *p = malloc(10);\n" + " throw 123;\n" + "}"); + TODO_ASSERT_EQUALS("error", "", errout.str()); + + check("void f() {\n" + " char *p;\n" + " try {\n" + " p = malloc(10);\n" + " throw 123;\n" + " } catch (...) { }\n" + " free(p);\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.