Skip to content

Commit

Permalink
Fix #11786 False positive: memory leak (#5182)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jun 22, 2023
1 parent fe56b0c commit b80460f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
// unknown control.. (TODO: handle loops)
else if ((Token::Match(tok, "%type% (") && Token::simpleMatch(tok->linkAt(1), ") {")) || Token::simpleMatch(tok, "do {")) {
varInfo.clear();
break;
return false;
}

// return
Expand Down
12 changes: 12 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class TestLeakAutoVar : public TestFixture {

// loops
TEST_CASE(loop1);
TEST_CASE(loop2);

// mismatching allocation/deallocation
TEST_CASE(mismatchAllocDealloc);
Expand Down Expand Up @@ -2070,6 +2071,17 @@ class TestLeakAutoVar : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void loop2() {
check("void f() {\n" // #11786
" int* p = (int*)malloc(sizeof(int));\n"
" if (1) {\n"
" while (0) {}\n"
" free(p);\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void mismatchAllocDealloc() {
check("void f() {\n"
" FILE*f=fopen(fname,a);\n"
Expand Down

0 comments on commit b80460f

Please sign in to comment.