Skip to content

Commit 9d75641

Browse files
committed
Fixed #3598 (false positive: (error) Memory pointed to by 'a' is freed twice.)
1 parent ff1edbc commit 9d75641

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

lib/checkother.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,7 +2501,7 @@ void CheckOther::checkDoubleFree()
25012501

25022502

25032503
// If a variable is passed to a function, remove it from the set of previously freed variables
2504-
else if (Token::Match(tok, "%var% (") && !Token::Match(tok, "printf|sprintf|snprintf|fprintf|if|while")) {
2504+
else if (Token::Match(tok, "%var% (") && !Token::Match(tok, "printf|sprintf|snprintf|fprintf")) {
25052505

25062506
// If this is a new function definition, clear all variables
25072507
if (Token::simpleMatch(tok->next()->link(), ") {")) {
@@ -2533,7 +2533,7 @@ void CheckOther::checkDoubleFree()
25332533

25342534
// Any control statements in-between delete, free() or closedir() statements
25352535
// makes it unclear whether any subsequent statements would be redundant.
2536-
if (Token::Match(tok, "else|break|continue|goto|return|throw")) {
2536+
if (Token::Match(tok, "if|else|for|while|break|continue|goto|return|throw|switch")) {
25372537
freedVariables.clear();
25382538
closeDirVariables.clear();
25392539
}

test/testother.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4614,6 +4614,22 @@ class TestOther : public TestFixture {
46144614
"}"
46154615
);
46164616
ASSERT_EQUALS("", errout.str());
4617+
4618+
check(
4619+
"int foo()\n"
4620+
"{\n"
4621+
" int* a = new int;\n"
4622+
" bool doDelete = true;\n"
4623+
" if (a != 0)\n"
4624+
" {\n"
4625+
" doDelete = false;\n"
4626+
" delete a;\n"
4627+
" }\n"
4628+
" if(doDelete)\n"
4629+
" delete a;\n"
4630+
" return 0;\n"
4631+
"}"
4632+
);
46174633
}
46184634

46194635
void coutCerrMisusage() {

0 commit comments

Comments
 (0)