Skip to content

Commit 14cbaeb

Browse files
author
Daniel Marjamäki
committed
Fixed cppcheck-opensource#2428 (false alarm with code containing a throw clause)
1 parent 989e0e7 commit 14cbaeb

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

lib/checkexceptionsafety.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void CheckExceptionSafety::deallocThrow()
106106
bool globalVar = false;
107107
for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next())
108108
{
109-
if (tok->varId() == varid)
109+
if (tok2->varId() == varid)
110110
{
111111
globalVar = true;
112112
break;

test/testexceptionsafety.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class TestExceptionSafety : public TestFixture
3535
void run()
3636
{
3737
TEST_CASE(destructors);
38-
TEST_CASE(deallocThrow);
38+
TEST_CASE(deallocThrow1);
39+
TEST_CASE(deallocThrow2);
3940
}
4041

4142
void check(const std::string &code)
@@ -66,7 +67,7 @@ class TestExceptionSafety : public TestFixture
6667
ASSERT_EQUALS("[test.cpp:3]: (error) Throwing exception in destructor\n", errout.str());
6768
}
6869

69-
void deallocThrow()
70+
void deallocThrow1()
7071
{
7172
check("int * p;\n"
7273
"void f(int x)\n"
@@ -78,6 +79,17 @@ class TestExceptionSafety : public TestFixture
7879
"}\n");
7980
ASSERT_EQUALS("[test.cpp:6]: (error) Throwing exception in invalid state, p points at deallocated memory\n", errout.str());
8081
}
82+
83+
void deallocThrow2()
84+
{
85+
check("void f() {\n"
86+
" int* p = 0;\n"
87+
" delete p;\n"
88+
" throw 1;\n"
89+
" p = new int;\n"
90+
"}\n");
91+
ASSERT_EQUALS("", errout.str());
92+
}
8193
};
8294

8395
REGISTER_TEST(TestExceptionSafety)

0 commit comments

Comments
 (0)