diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index c788ae992..dab826771 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -378,6 +378,7 @@ void CheckClass::initializeVarList(const Function &func, std::list else if (Token::simpleMatch(ftok, "operator= (") && ftok->previous()->str() != "::") { + /** @todo check function parameters for overloaded function so we check the right one */ // check if member function exists std::list::const_iterator it; for (it = scope->functionList.begin(); it != scope->functionList.end(); ++it) @@ -392,10 +393,14 @@ void CheckClass::initializeVarList(const Function &func, std::list // member function has implementation if (it->hasBody) { - // initialize variable use list using member function - callstack.push_back(ftok->str()); - initializeVarList(*it, callstack, scope, usage); - callstack.pop_back(); + // check for recursion + if ((&(*it) != &func)) + { + // initialize variable use list using member function + callstack.push_back(ftok->str()); + initializeVarList(*it, callstack, scope, usage); + callstack.pop_back(); + } } // there is a called member function, but it has no implementation, so we assume it initializes everything @@ -445,10 +450,14 @@ void CheckClass::initializeVarList(const Function &func, std::list // member function has implementation if (it->hasBody) { - // initialize variable use list using member function - callstack.push_back(ftok->str()); - initializeVarList(*it, callstack, scope, usage); - callstack.pop_back(); + // check for recursion + if ((&(*it) != &func)) + { + // initialize variable use list using member function + callstack.push_back(ftok->str()); + initializeVarList(*it, callstack, scope, usage); + callstack.pop_back(); + } } // there is a called member function, but it has no implementation, so we assume it initializes everything diff --git a/test/testclass.cpp b/test/testclass.cpp index 81d20bdf3..b736a70e9 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -108,6 +108,7 @@ private: TEST_CASE(operatorEq1); TEST_CASE(operatorEq2); TEST_CASE(operatorEq3); // ticket #3051 + TEST_CASE(operatorEq4); // ticket #3114 TEST_CASE(operatorEqRetRefThis1); TEST_CASE(operatorEqRetRefThis2); // ticket #1323 TEST_CASE(operatorEqRetRefThis3); // ticket #1405 @@ -317,6 +318,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void operatorEq4() // ticket #3114 (infinite loop) + { + checkOpertorEq("struct A {\n" + " A& operator=(A const& a) { return operator=(&a); }\n" + " A& operator=(const A*) { return *this; }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } + // Check that operator Equal returns reference to this void checkOpertorEqRetRefThis(const char code[]) {