Skip to content

Commit e64454d

Browse files
Fix #10152 memleak with switch/break (#8437)
1 parent b6c70b4 commit e64454d

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

lib/checkmemoryleak.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari
947947
}
948948

949949
// Returning from function..
950-
else if ((tok3->scope()->type != ScopeType::eLambda || tok3->scope() == variable->scope()) && tok3->str() == "return") {
950+
else if (((!isWithinScope(tok3, variable, ScopeType::eLambda) && !isWithinScope(tok3, variable, ScopeType::eSwitch)) || tok3->scope() == variable->scope()) && tok3->str() == "return") {
951951
// Returning from function without deallocating struct member?
952952
if (!Token::Match(tok3, "return %varid% ;", structid) &&
953953
!Token::Match(tok3, "return & %varid%", structid) &&

test/testmemleak.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,22 @@ class TestMemleakStructMember : public TestFixture {
20642064
" }\n"
20652065
" return g(&a);\n"
20662066
"}\n");
2067-
TODO_ASSERT_EQUALS("", "[test.cpp:9:9]: (error) Memory leak: a.str [memleak]\n", errout_str());
2067+
ASSERT_EQUALS("", errout_str());
2068+
2069+
check("struct S { int *p; };\n"
2070+
"S f(int i) {\n"
2071+
" S s;\n"
2072+
" switch(i) {\n"
2073+
" case 1:\n"
2074+
" s.p = new int;\n"
2075+
" break;\n"
2076+
" default: {\n"
2077+
" return {};\n"
2078+
" }\n"
2079+
" }\n"
2080+
" return s;\n"
2081+
"}\n");
2082+
ASSERT_EQUALS("", errout_str());
20682083
}
20692084

20702085
void ifelse() {

0 commit comments

Comments
 (0)