From 83d5a72659f43784b296ac0c909e682bfeef9bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 21 Dec 2009 20:04:55 +0100 Subject: [PATCH] Fixed #1126 (False positive: upon exception there is memory leak (calling unknown function)) --- lib/checkexceptionsafety.cpp | 15 ++++++++++++++- test/testexceptionsafety.cpp | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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()