Skip to content

Commit

Permalink
Fix #12953 Wrong error id reported (memleak vs. resourceLeak) (#6631)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Jul 22, 2024
1 parent cdb68bd commit ac5d06c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/checkmemoryleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,8 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari

// Struct member is allocated => check if it is also properly deallocated..
else if ((assignToks = isMemberAssignment(tok2, variable->declarationId())).first && assignToks.first->varId()) {
if (getAllocationType(assignToks.second, assignToks.first->varId()) == AllocType::No)
const AllocType allocType = getAllocationType(assignToks.second, assignToks.first->varId());
if (allocType == AllocType::No)
continue;

if (variable->isArgument() && variable->valueType() && variable->valueType()->type == ValueType::UNKNOWN_TYPE && assignToks.first->astParent()) {
Expand All @@ -823,7 +824,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari

else if (tok3->str() == "}") {
if (indentlevel3 == 0) {
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), allocType);
break;
}
--indentlevel3;
Expand Down Expand Up @@ -854,7 +855,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari
// Deallocating the struct..
else if (Token::Match(tok3, "%name% ( %varid% )", structid) && mSettings->library.getDeallocFuncInfo(tok3)) {
if (indentlevel2 == 0)
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), allocType);
break;
}

Expand Down Expand Up @@ -903,7 +904,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari
!Token::Match(tok3, "return & %varid%", structid) &&
!(Token::Match(tok3, "return %varid% . %var%", structid) && tok3->tokAt(3)->varId() == structmemberid) &&
!(Token::Match(tok3, "return %name% (") && tok3->astOperand1() && deallocInFunction(tok3->astOperand1(), structid))) {
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), allocType);
}
break;
}
Expand Down
12 changes: 6 additions & 6 deletions test/testmemleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1859,9 +1859,9 @@ class TestMemleakStructMember : public TestFixture {
" ((s).b) = open(\"xx.log\", O_RDONLY);\n"
" (&s)->c = open(\"xx.log\", O_RDONLY);\n"
"}\n", false);
ASSERT_EQUALS("[test.c:7]: (error) Memory leak: s.a\n"
"[test.c:7]: (error) Memory leak: s.b\n"
"[test.c:7]: (error) Memory leak: s.c\n",
ASSERT_EQUALS("[test.c:7]: (error) Resource leak: s.a\n"
"[test.c:7]: (error) Resource leak: s.b\n"
"[test.c:7]: (error) Resource leak: s.c\n",
errout_str());

check("struct S { int *p, *q; };\n" // #7705
Expand Down Expand Up @@ -2084,11 +2084,11 @@ class TestMemleakStructMember : public TestFixture {
"}";

check(code1, true);
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: a.f\n"
ASSERT_EQUALS("[test.cpp:12]: (error) Resource leak: a.f\n"
"[test.cpp:12]: (error) Memory leak: a.c\n"
"[test.cpp:12]: (error) Memory leak: a.m\n", errout_str());
check(code1, false);
ASSERT_EQUALS("[test.c:12]: (error) Memory leak: a.f\n"
ASSERT_EQUALS("[test.c:12]: (error) Resource leak: a.f\n"
"[test.c:12]: (error) Memory leak: a.m\n", errout_str());

// Test OK case
Expand Down Expand Up @@ -2120,7 +2120,7 @@ class TestMemleakStructMember : public TestFixture {
check(code3, true);
ASSERT_EQUALS("", errout_str());
check(code3, false);
ASSERT_EQUALS("[test.c:4]: (error) Memory leak: a.f\n", errout_str());
ASSERT_EQUALS("[test.c:4]: (error) Resource leak: a.f\n", errout_str());

// Test struct with destructor
const char code4[] = "struct A {\n"
Expand Down

0 comments on commit ac5d06c

Please sign in to comment.