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
8 changes: 7 additions & 1 deletion lib/checkmemoryleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,14 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari
// This struct member is allocated.. check that it is deallocated
int indentlevel3 = indentlevel2;
for (const Token *tok3 = tok2; tok3; tok3 = tok3->next()) {
if (tok3->str() == "{")
if (tok3->str() == "{") {
if (tok3->scope()->type == ScopeType::eIf && tok3 == tok3->scope()->bodyStart) { // bailout: member checked in if condition
const Token* const condBeg = tok3->scope()->classDef->tokAt(1);
if (Token::findmatch(condBeg, ". %varid%", condBeg->link(), assignToks.first->varId()))
break;
}
++indentlevel3;
}

else if (tok3->str() == "}") {
if (indentlevel3 == 0) {
Expand Down
12 changes: 12 additions & 0 deletions test/testmemleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,18 @@ class TestMemleakStructMember : public TestFixture {
"[test.cpp:10:5]: (error) Resource leak: s.fd [resourceLeak]\n"
"[test.cpp:16:1]: (error) Resource leak: s.fd [resourceLeak]\n",
errout_str());

check("struct S { int fd; };\n" // #13031
"void f() {\n"
" struct S* s = malloc(sizeof(struct S));\n"
" s->fd = open(\"abc\", O_RDWR | O_NOCTTY);\n"
" if (s->fd < 0) {\n"
" free(s);\n"
" return NULL;\n"
" }\n"
" return s;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void failedAllocation() {
Expand Down
Loading