diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 040caa396..b2adaf479 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -389,6 +389,13 @@ private: if (tok.varId() && Token::Match(&tok, "%var% [[;]")) { const Variable* var2 = symbolDatabase->getVariableFromVarId(tok.varId()); if (var2 && var2->nameToken() == &tok && !var2->isStatic() && !var2->isConst()) { + if (tok.linkAt(1)) { // array + const Token* end = tok.next(); + while (end->link()) + end = end->link()->next(); + if (end->str() != ";") + return &tok; + } const Scope* parent = var2->scope()->nestedIn; while (parent) { for (std::list::const_iterator j = parent->varlist.begin(); j != parent->varlist.end(); ++j) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 305716954..bed711447 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1182,6 +1182,31 @@ private: " free(buffer);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: buffer\n", errout.str()); + + // #3845 + checkUninitVar("int foo() {\n" + " int a[1] = {5};\n" + " return a[0];\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + checkUninitVar("int foo() {\n" + " int a[2][2] = {{3,4}, {5,6}};\n" + " return a[0][1];\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + checkUninitVar("int foo() {\n" + " int a[1];\n" + " return a[0];\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str()); + + checkUninitVar("int foo() {\n" + " int a[2][2];\n" + " return a[0][1];\n" + "}"); + TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", "", errout.str()); } // alloc..