Skip to content
Merged
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: 1 addition & 1 deletion lib/checkmemoryleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari
}

// Returning from function..
else if ((tok3->scope()->type != ScopeType::eLambda || tok3->scope() == variable->scope()) && tok3->str() == "return") {
else if (((!isWithinScope(tok3, variable, ScopeType::eLambda) && !isWithinScope(tok3, variable, ScopeType::eSwitch)) || tok3->scope() == variable->scope()) && tok3->str() == "return") {
// Returning from function without deallocating struct member?
if (!Token::Match(tok3, "return %varid% ;", structid) &&
!Token::Match(tok3, "return & %varid%", structid) &&
Expand Down
17 changes: 16 additions & 1 deletion test/testmemleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,22 @@ class TestMemleakStructMember : public TestFixture {
" }\n"
" return g(&a);\n"
"}\n");
TODO_ASSERT_EQUALS("", "[test.cpp:9:9]: (error) Memory leak: a.str [memleak]\n", errout_str());
ASSERT_EQUALS("", errout_str());

check("struct S { int *p; };\n"
"S f(int i) {\n"
" S s;\n"
" switch(i) {\n"
" case 1:\n"
" s.p = new int;\n"
" break;\n"
" default: {\n"
" return {};\n"
" }\n"
" }\n"
" return s;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void ifelse() {
Expand Down
Loading