Fixed #1783 (false positive: uninitalized variable in constructor/operator=, when calling overloaded functions)

This commit is contained in:
Daniel Marjamäki 2010-06-13 10:23:59 +02:00
parent 70bfa890a8
commit 5de124280a
2 changed files with 82 additions and 54 deletions

View File

@ -339,9 +339,15 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va
if (ftok->str() == "if")
continue;
// No recursive calls!
if (std::find(callstack.begin(), callstack.end(), ftok->str()) == callstack.end())
// recursive call / calling overloaded function
// assume that all variables are initialized
if (std::find(callstack.begin(), callstack.end(), ftok->str()) != callstack.end())
{
for (Var *var = varlist; var; var = var->next)
var->init = true;
return;
}
int i = 0;
const Token *ftok2 = _tokenizer->findClassFunction(tok1, classname, ftok->strAt(0), i, isStruct);
if (ftok2)
@ -402,7 +408,6 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va
continue;
}
}
}
// Assignment of member variable?
else if (Token::Match(ftok, "%var% ="))

View File

@ -73,6 +73,7 @@ private:
TEST_CASE(uninitFunction1); // No FP when initialized in function
TEST_CASE(uninitFunction2); // No FP when initialized in function
TEST_CASE(uninitSameClassName); // No FP when two classes have the same name
TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions
TEST_CASE(noConstructor1);
TEST_CASE(noConstructor2);
@ -2035,6 +2036,28 @@ private:
ASSERT_EQUALS("", errout.str());
}
void uninitFunctionOverload()
{
// Ticket #1783 - overloaded "init" functions
checkUninitVar("class A\n"
"{\n"
"private:\n"
" int i;\n"
"\n"
"public:\n"
" A()\n"
" {\n"
" init();\n"
" }\n"
"\n"
" void init() { init(0); }\n"
"\n"
" void init(int value)\n"
" { i = value; }\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void checkNoConstructor(const char code[])
{