Skip to content

Commit 8745a78

Browse files
Fix #14945 FN memleak with nested scopes. (#8761)
1 parent a156364 commit 8745a78

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

lib/checkleakautovar.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ bool CheckLeakAutoVarImpl::checkScope(const Token * const startToken,
342342
}
343343
}
344344

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

346348
// look for end of statement
347349
const bool isInit = Token::Match(tok->tokAt(-1), "%var% {|(") && tok->tokAt(-1)->variable() && tok->tokAt(-1) == tok->tokAt(-1)->variable()->nameToken();

test/testleakautovar.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ class TestLeakAutoVar : public TestFixture {
211211
TEST_CASE(inlineFunction); // #3989
212212

213213
TEST_CASE(smartPtrInContainer); // #8262
214+
TEST_CASE(unconditionalScope);
214215

215216
TEST_CASE(functionCallCastConfig); // #9652
216217
TEST_CASE(functionCallLeakIgnoreConfig); // #7923
@@ -3178,6 +3179,34 @@ class TestLeakAutoVar : public TestFixture {
31783179
ASSERT_EQUALS("", errout_str());
31793180
}
31803181

3182+
void unconditionalScope() {
3183+
check("void f() {\n" // #14945
3184+
" {\n"
3185+
" int* p = new int;\n"
3186+
" *p = 1;\n"
3187+
" }\n"
3188+
" {\n"
3189+
" int* q = new int;\n"
3190+
" *q = 2;\n"
3191+
" delete q;\n"
3192+
" }\n"
3193+
" int* r = new int;\n"
3194+
" *r = 3;\n"
3195+
" delete r;\n"
3196+
"}\n", dinit(CheckOptions, $.cpp = true));
3197+
ASSERT_EQUALS("[test.cpp:5:5]: (error) Memory leak: p [memleak]\n", errout_str());
3198+
3199+
check("void f() {\n"
3200+
" int* p = new int;\n"
3201+
" {\n"
3202+
" (void)p;\n"
3203+
" }\n"
3204+
" *p = 1;\n"
3205+
" delete p;\n"
3206+
"}\n", dinit(CheckOptions, $.cpp = true));
3207+
ASSERT_EQUALS("", errout_str());
3208+
}
3209+
31813210
void functionCallCastConfig() { // #9652
31823211
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
31833212
"<def format=\"2\">\n"

0 commit comments

Comments
 (0)