Fixed false positives in CheckUninitVar::checkStruct()

This commit is contained in:
PKEuS 2015-01-21 13:10:38 +01:00
parent bd7765e008
commit 346532d312
2 changed files with 65 additions and 18 deletions

View File

@ -1126,7 +1126,10 @@ void CheckUninitVar::checkStruct(const Scope* scope, const Token *tok, const Var
if (scope2->className == structname && scope2->numConstructors == 0U) {
for (std::list<Variable>::const_iterator it = scope2->varlist.begin(); it != scope2->varlist.end(); ++it) {
const Variable &var = *it;
if (!var.hasDefault() && !var.isArray()) {
if (var.hasDefault() || var.isArray() || (!_tokenizer->isC() && var.isClass() && (!var.type() || var.type()->needInitialization != Type::True)))
continue;
// is the variable declared in a inner union?
bool innerunion = false;
for (std::list<Scope>::const_iterator it2 = symbolDatabase->scopeList.begin(); it2 != symbolDatabase->scopeList.end(); ++it2) {
@ -1152,7 +1155,6 @@ void CheckUninitVar::checkStruct(const Scope* scope, const Token *tok, const Var
}
}
}
}
static void conditionAlwaysTrueOrFalse(const Token *tok, const std::map<unsigned int, int> &variableValue, bool *alwaysTrue, bool *alwaysFalse)
{

View File

@ -3301,6 +3301,51 @@ private:
" int b = ab.b;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.b\n", errout.str());
// STL class member
checkUninitVar2("struct A {\n"
" std::map<int, int> m;\n"
" int i;\n"
"};\n"
"void foo() {\n"
" A a;\n"
" x = a.m;\n"
"}");
ASSERT_EQUALS("", errout.str());
// Unknown type (C++)
checkUninitVar2("struct A {\n"
" C m;\n"
" int i;\n"
"};\n"
"void foo() {\n"
" A a;\n"
" x = a.m;\n"
"}", "test.cpp");
ASSERT_EQUALS("", errout.str());
// Unknown type (C)
checkUninitVar2("struct A {\n"
" C m;\n"
" int i;\n"
"};\n"
"void foo() {\n"
" A a;\n"
" x = a.m;\n"
"}", "test.c");
ASSERT_EQUALS("[test.c:7]: (error) Uninitialized struct member: a.m\n", errout.str());
// Type with constructor
checkUninitVar2("class C { C(); }\n"
"struct A {\n"
" C m;\n"
" int i;\n"
"};\n"
"void foo() {\n"
" A a;\n"
" x = a.m;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void uninitvar2_while() {