diff --git a/lib/checkexceptionsafety.cpp b/lib/checkexceptionsafety.cpp index 0c56e10fb..4048eb928 100644 --- a/lib/checkexceptionsafety.cpp +++ b/lib/checkexceptionsafety.cpp @@ -177,7 +177,7 @@ void CheckExceptionSafety::unsafeNew() localVars.insert(tok->varId()); } - if (Token::Match(tok, "; %var% = new %type%")) + if (Token::Match(tok, "[;{}] %var% = new %type%")) { if (!varname.empty()) { @@ -188,6 +188,19 @@ void CheckExceptionSafety::unsafeNew() varname = tok->strAt(1); } + else if (Token::Match(tok, "[;{}] %var% (")) + { + for (tok = tok->next(); tok && !Token::Match(tok, "[;{}]"); tok = tok->next()) + { + if (tok->str() == varname) + { + varname = ""; + } + } + if (!tok) + break; + } + else if (tok->str() == "delete") { if (Token::simpleMatch(tok->next(), varname.c_str()) || diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index 7197d64a5..100f83a91 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -113,6 +113,15 @@ private: " B *b = new B;\n" "}\n", "A\n"); ASSERT_EQUALS("", errout.str()); + + // passing pointer to unknown function.. the pointer may be added to some list etc.. + check("void f()\n" + "{\n" + " A *a1 = new A;\n" + " add(a1);\n" + " A *a2 = new A;\n" + "}\n", ""); + ASSERT_EQUALS("", errout.str()); } void realloc()