From 3b4160600de7ecd79d266698d5561bb5f9fc1a93 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 27 Nov 2015 11:18:40 +0100 Subject: [PATCH] Fixed crash in CheckMemoryLeak::functionReturnType() for unary operator:: (#7172) --- lib/checkmemoryleak.cpp | 2 +- test/testmemleak.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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)