quick fix for #3114 (infinite recursion when operator= is overloaded)

This commit is contained in:
Robert Reif 2011-09-14 19:04:06 -04:00
parent 288efd832c
commit 2bc7da2c64
2 changed files with 27 additions and 8 deletions

View File

@ -378,6 +378,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
else if (Token::simpleMatch(ftok, "operator= (") && else if (Token::simpleMatch(ftok, "operator= (") &&
ftok->previous()->str() != "::") ftok->previous()->str() != "::")
{ {
/** @todo check function parameters for overloaded function so we check the right one */
// check if member function exists // check if member function exists
std::list<Function>::const_iterator it; std::list<Function>::const_iterator it;
for (it = scope->functionList.begin(); it != scope->functionList.end(); ++it) for (it = scope->functionList.begin(); it != scope->functionList.end(); ++it)
@ -391,12 +392,16 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
{ {
// member function has implementation // member function has implementation
if (it->hasBody) if (it->hasBody)
{
// check for recursion
if ((&(*it) != &func))
{ {
// initialize variable use list using member function // initialize variable use list using member function
callstack.push_back(ftok->str()); callstack.push_back(ftok->str());
initializeVarList(*it, callstack, scope, usage); initializeVarList(*it, callstack, scope, usage);
callstack.pop_back(); callstack.pop_back();
} }
}
// there is a called member function, but it has no implementation, so we assume it initializes everything // there is a called member function, but it has no implementation, so we assume it initializes everything
else else
@ -444,12 +449,16 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
{ {
// member function has implementation // member function has implementation
if (it->hasBody) if (it->hasBody)
{
// check for recursion
if ((&(*it) != &func))
{ {
// initialize variable use list using member function // initialize variable use list using member function
callstack.push_back(ftok->str()); callstack.push_back(ftok->str());
initializeVarList(*it, callstack, scope, usage); initializeVarList(*it, callstack, scope, usage);
callstack.pop_back(); callstack.pop_back();
} }
}
// there is a called member function, but it has no implementation, so we assume it initializes everything // there is a called member function, but it has no implementation, so we assume it initializes everything
else else

View File

@ -108,6 +108,7 @@ private:
TEST_CASE(operatorEq1); TEST_CASE(operatorEq1);
TEST_CASE(operatorEq2); TEST_CASE(operatorEq2);
TEST_CASE(operatorEq3); // ticket #3051 TEST_CASE(operatorEq3); // ticket #3051
TEST_CASE(operatorEq4); // ticket #3114
TEST_CASE(operatorEqRetRefThis1); TEST_CASE(operatorEqRetRefThis1);
TEST_CASE(operatorEqRetRefThis2); // ticket #1323 TEST_CASE(operatorEqRetRefThis2); // ticket #1323
TEST_CASE(operatorEqRetRefThis3); // ticket #1405 TEST_CASE(operatorEqRetRefThis3); // ticket #1405
@ -317,6 +318,15 @@ private:
ASSERT_EQUALS("", errout.str()); 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 // Check that operator Equal returns reference to this
void checkOpertorEqRetRefThis(const char code[]) void checkOpertorEqRetRefThis(const char code[])
{ {