Skip to content

Commit b80460f

Browse files
Fix #11786 False positive: memory leak (#5182)
1 parent fe56b0c commit b80460f

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

lib/checkleakautovar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
609609
// unknown control.. (TODO: handle loops)
610610
else if ((Token::Match(tok, "%type% (") && Token::simpleMatch(tok->linkAt(1), ") {")) || Token::simpleMatch(tok, "do {")) {
611611
varInfo.clear();
612-
break;
612+
return false;
613613
}
614614

615615
// return

test/testleakautovar.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class TestLeakAutoVar : public TestFixture {
178178

179179
// loops
180180
TEST_CASE(loop1);
181+
TEST_CASE(loop2);
181182

182183
// mismatching allocation/deallocation
183184
TEST_CASE(mismatchAllocDealloc);
@@ -2070,6 +2071,17 @@ class TestLeakAutoVar : public TestFixture {
20702071
ASSERT_EQUALS("", errout.str());
20712072
}
20722073

2074+
void loop2() {
2075+
check("void f() {\n" // #11786
2076+
" int* p = (int*)malloc(sizeof(int));\n"
2077+
" if (1) {\n"
2078+
" while (0) {}\n"
2079+
" free(p);\n"
2080+
" }\n"
2081+
"}\n");
2082+
ASSERT_EQUALS("", errout.str());
2083+
}
2084+
20732085
void mismatchAllocDealloc() {
20742086
check("void f() {\n"
20752087
" FILE*f=fopen(fname,a);\n"

0 commit comments

Comments
 (0)