Skip to content

Commit ac5d06c

Browse files
Fix #12953 Wrong error id reported (memleak vs. resourceLeak) (#6631)
1 parent cdb68bd commit ac5d06c

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lib/checkmemoryleak.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,8 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari
801801

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

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

824825
else if (tok3->str() == "}") {
825826
if (indentlevel3 == 0) {
826-
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
827+
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), allocType);
827828
break;
828829
}
829830
--indentlevel3;
@@ -854,7 +855,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari
854855
// Deallocating the struct..
855856
else if (Token::Match(tok3, "%name% ( %varid% )", structid) && mSettings->library.getDeallocFuncInfo(tok3)) {
856857
if (indentlevel2 == 0)
857-
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
858+
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), allocType);
858859
break;
859860
}
860861

@@ -903,7 +904,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari
903904
!Token::Match(tok3, "return & %varid%", structid) &&
904905
!(Token::Match(tok3, "return %varid% . %var%", structid) && tok3->tokAt(3)->varId() == structmemberid) &&
905906
!(Token::Match(tok3, "return %name% (") && tok3->astOperand1() && deallocInFunction(tok3->astOperand1(), structid))) {
906-
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
907+
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), allocType);
907908
}
908909
break;
909910
}

test/testmemleak.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,9 +1859,9 @@ class TestMemleakStructMember : public TestFixture {
18591859
" ((s).b) = open(\"xx.log\", O_RDONLY);\n"
18601860
" (&s)->c = open(\"xx.log\", O_RDONLY);\n"
18611861
"}\n", false);
1862-
ASSERT_EQUALS("[test.c:7]: (error) Memory leak: s.a\n"
1863-
"[test.c:7]: (error) Memory leak: s.b\n"
1864-
"[test.c:7]: (error) Memory leak: s.c\n",
1862+
ASSERT_EQUALS("[test.c:7]: (error) Resource leak: s.a\n"
1863+
"[test.c:7]: (error) Resource leak: s.b\n"
1864+
"[test.c:7]: (error) Resource leak: s.c\n",
18651865
errout_str());
18661866

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

20862086
check(code1, true);
2087-
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: a.f\n"
2087+
ASSERT_EQUALS("[test.cpp:12]: (error) Resource leak: a.f\n"
20882088
"[test.cpp:12]: (error) Memory leak: a.c\n"
20892089
"[test.cpp:12]: (error) Memory leak: a.m\n", errout_str());
20902090
check(code1, false);
2091-
ASSERT_EQUALS("[test.c:12]: (error) Memory leak: a.f\n"
2091+
ASSERT_EQUALS("[test.c:12]: (error) Resource leak: a.f\n"
20922092
"[test.c:12]: (error) Memory leak: a.m\n", errout_str());
20932093

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

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

0 commit comments

Comments
 (0)