diff --git a/lib/checkexceptionsafety.cpp b/lib/checkexceptionsafety.cpp index edee7221a..cdadcc66c 100644 --- a/lib/checkexceptionsafety.cpp +++ b/lib/checkexceptionsafety.cpp @@ -106,7 +106,7 @@ void CheckExceptionSafety::deallocThrow() bool globalVar = false; for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next()) { - if (tok->varId() == varid) + if (tok2->varId() == varid) { globalVar = true; break; diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index 52b93e1b2..9d05870c2 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -35,7 +35,8 @@ private: void run() { TEST_CASE(destructors); - TEST_CASE(deallocThrow); + TEST_CASE(deallocThrow1); + TEST_CASE(deallocThrow2); } void check(const std::string &code) @@ -66,7 +67,7 @@ private: ASSERT_EQUALS("[test.cpp:3]: (error) Throwing exception in destructor\n", errout.str()); } - void deallocThrow() + void deallocThrow1() { check("int * p;\n" "void f(int x)\n" @@ -78,6 +79,17 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:6]: (error) Throwing exception in invalid state, p points at deallocated memory\n", errout.str()); } + + void deallocThrow2() + { + check("void f() {\n" + " int* p = 0;\n" + " delete p;\n" + " throw 1;\n" + " p = new int;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestExceptionSafety)