Skip to content

Commit 3b41606

Browse files
committed
Fixed crash in CheckMemoryLeak::functionReturnType() for unary operator:: (cppcheck-opensource#7172)
1 parent 0ba3d25 commit 3b41606

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/checkmemoryleak.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Function* f
343343
return No;
344344
const Token* tok = tok2->astOperand1();
345345
if (Token::Match(tok, ".|::"))
346-
tok = tok->astOperand2();
346+
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
347347
varid = tok->varId();
348348
break;
349349
}

test/testmemleak.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ class TestMemleakInFunction : public TestFixture {
362362

363363
TEST_CASE(gnucfg);
364364
TEST_CASE(trac3991);
365+
TEST_CASE(crash);
365366
}
366367

367368
std::string getcode(const char code[], const char varname[], bool classfunc=false) {
@@ -3893,6 +3894,24 @@ class TestMemleakInFunction : public TestFixture {
38933894
"}", true);
38943895
ASSERT_EQUALS("", errout.str());
38953896
}
3897+
3898+
void crash() {
3899+
check("class ComponentDC {\n"
3900+
" ::Component * getComponent();\n"
3901+
"};\n"
3902+
"::Component * ComponentDC::getComponent() {\n"
3903+
" return ((::Component *)myComponent);\n"
3904+
"}\n"
3905+
"class MultiComponentDC : public ComponentDC {\n"
3906+
" virtual void addChild(InterfaceNode *);\n"
3907+
"};\n"
3908+
"void MultiComponentDC::addChild(InterfaceNode *childNode) {\n"
3909+
" ComponentDC *cdc = dynamic_cast<ComponentDC *>(childNode);\n"
3910+
" if (cdc)\n"
3911+
" ::Component *c = cdc->getComponent();\n"
3912+
"}");
3913+
ASSERT_EQUALS("", errout.str());
3914+
}
38963915
};
38973916

38983917
REGISTER_TEST(TestMemleakInFunction)

0 commit comments

Comments
 (0)