From 41a54fceba86fa54ae1e5e1ba3d984c056dbe26b Mon Sep 17 00:00:00 2001 From: Frank Zingsheim Date: Thu, 16 Oct 2014 21:57:43 +0200 Subject: [PATCH] Fixed false positives about uninitialized member variables if variable has a default value (#5500) --- lib/checkuninitvar.cpp | 2 +- lib/symboldatabase.cpp | 2 +- test/testconstructors.cpp | 16 ++++++++++++++++ test/testuninitvar.cpp | 9 +++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index cc259e7e3..4a9ca7e03 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1128,7 +1128,7 @@ void CheckUninitVar::checkStruct(const Scope* scope, const Token *tok, const Var if (scope2->className == structname && scope2->numConstructors == 0U) { for (std::list::const_iterator it = scope2->varlist.begin(); it != scope2->varlist.end(); ++it) { const Variable &var = *it; - if (!var.isArray()) { + if (!var.hasDefault() && !var.isArray()) { // is the variable declared in a inner union? bool innerunion = false; for (std::list::const_iterator it2 = symbolDatabase->scopeList.begin(); it2 != symbolDatabase->scopeList.end(); ++it2) { diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 62ce21beb..82867ae56 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -912,7 +912,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti else if (var->type()->needInitialization == Type::Unknown) unknown = true; } - } else + } else if (!var->hasDefault()) needInitialization = true; } diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index f286756f7..fc63b4fb0 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -2427,6 +2427,22 @@ private: " { }\n" "};"); ASSERT_EQUALS("", errout.str()); + + // non static data-member initialization + check("struct POINT\n" + "{\n" + " int x=0;\n" + " int y=0;\n" + "};\n" + "class Fred\n" + "{\n" + "private:\n" + " POINT p;\n" + "public:\n" + " Fred()\n" + " { }\n" + "};"); + ASSERT_EQUALS("", errout.str()); } void uninitVarUnion1() { // ticket #3196 diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 0493ab59d..a23dd5a8d 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -3203,6 +3203,15 @@ private: " int *p = ab.a;\n" "}"); ASSERT_EQUALS("", errout.str()); + + // non static data-member initialization + checkUninitVar2("struct AB { int a=1; int b; };\n" + "void f(void) {\n" + " struct AB ab;\n" + " int a = ab.a;\n" + " int b = ab.b;\n" + "}"); + ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.b\n", errout.str()); } void uninitvar2_while() {