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.