From f4bf94ca4fdea23b115a79d8bfb6b9110f8e85b8 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 25 May 2012 07:28:32 -0700 Subject: [PATCH] Fixed #3845 --- lib/checkuninitvar.cpp | 7 +++++++ test/testuninitvar.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) 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..