diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index dd069a9de..c5141c729 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -343,7 +343,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Function* f return No; const Token* tok = tok2->astOperand1(); if (Token::Match(tok, ".|::")) - tok = tok->astOperand2(); + tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1(); varid = tok->varId(); break; } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index ba8abbb14..6fcc82507 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -362,6 +362,7 @@ private: TEST_CASE(gnucfg); TEST_CASE(trac3991); + TEST_CASE(crash); } std::string getcode(const char code[], const char varname[], bool classfunc=false) { @@ -3893,6 +3894,24 @@ private: "}", true); ASSERT_EQUALS("", errout.str()); } + + void crash() { + check("class ComponentDC {\n" + " ::Component * getComponent();\n" + "};\n" + "::Component * ComponentDC::getComponent() {\n" + " return ((::Component *)myComponent);\n" + "}\n" + "class MultiComponentDC : public ComponentDC {\n" + " virtual void addChild(InterfaceNode *);\n" + "};\n" + "void MultiComponentDC::addChild(InterfaceNode *childNode) {\n" + " ComponentDC *cdc = dynamic_cast(childNode);\n" + " if (cdc)\n" + " ::Component *c = cdc->getComponent();\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestMemleakInFunction)