Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ bool CheckLeakAutoVarImpl::checkScope(const Token * const startToken,
}
}

if (tok == tok->scope()->bodyEnd && tok->scope()->type == ScopeType::eUnconditional)
ret(tok, varInfo, /*isEndOfScope*/ true);

// look for end of statement
const bool isInit = Token::Match(tok->tokAt(-1), "%var% {|(") && tok->tokAt(-1)->variable() && tok->tokAt(-1) == tok->tokAt(-1)->variable()->nameToken();
Expand Down
29 changes: 29 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class TestLeakAutoVar : public TestFixture {
TEST_CASE(inlineFunction); // #3989

TEST_CASE(smartPtrInContainer); // #8262
TEST_CASE(unconditionalScope);

TEST_CASE(functionCallCastConfig); // #9652
TEST_CASE(functionCallLeakIgnoreConfig); // #7923
Expand Down Expand Up @@ -3178,6 +3179,34 @@ class TestLeakAutoVar : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void unconditionalScope() {
check("void f() {\n" // #14945
" {\n"
" int* p = new int;\n"
" *p = 1;\n"
" }\n"
" {\n"
" int* q = new int;\n"
" *q = 2;\n"
" delete q;\n"
" }\n"
" int* r = new int;\n"
" *r = 3;\n"
" delete r;\n"
"}\n", dinit(CheckOptions, $.cpp = true));
ASSERT_EQUALS("[test.cpp:5:5]: (error) Memory leak: p [memleak]\n", errout_str());

check("void f() {\n"
" int* p = new int;\n"
" {\n"
" (void)p;\n"
" }\n"
" *p = 1;\n"
" delete p;\n"
"}\n", dinit(CheckOptions, $.cpp = true));
ASSERT_EQUALS("", errout_str());
}

void functionCallCastConfig() { // #9652
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def format=\"2\">\n"
Expand Down
Loading