From c581753040b8e5af23ae3ad98a10f59253f51acf Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sun, 23 Aug 2009 22:54:41 +0300 Subject: [PATCH] Fix ticket #613 (array members variables are not checked in constructor) http://sourceforge.net/apps/trac/cppcheck/ticket/613 Patch from micres --- src/checkclass.cpp | 11 +++++++++++ test/testclass.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/checkclass.cpp b/src/checkclass.cpp index 2e57d272f..630840dca 100644 --- a/src/checkclass.cpp +++ b/src/checkclass.cpp @@ -109,6 +109,11 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses) varname = next->strAt(3); } + else if (Token::Match(next, "%type% %var% [")) + { + varname = next->strAt(1); + } + // std::string.. else if (withClasses && Token::Match(next, "std :: string %var% ;")) { @@ -243,6 +248,12 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va initVar(varlist, ftok->str().c_str()); } + // Assignment of array item of member variable? + else if (Token::Match(ftok, "%var% [ %any% ] =")) + { + initVar(varlist, ftok->str().c_str()); + } + // The functions 'clear' and 'Clear' are supposed to initialize variable. if (Token::Match(ftok, "%var% . clear|Clear (")) { diff --git a/test/testclass.cpp b/test/testclass.cpp index edb4e353d..e53c1cca4 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -48,6 +48,7 @@ private: TEST_CASE(uninitVarEnum); TEST_CASE(uninitVarStream); TEST_CASE(uninitVarTypedef); + TEST_CASE(uninitVarArray); TEST_CASE(privateCtor1); // If constructor is private.. TEST_CASE(privateCtor2); // If constructor is private.. TEST_CASE(function); // Function is not variable @@ -392,6 +393,30 @@ private: ASSERT_EQUALS("", errout.str()); } + void uninitVarArray() + { + checkUninitVar("class John\n" + "{\n" + "public:\n" + " John() {}\n" + "\n" + "private:\n" + " char name[255];\n" + "};\n"); + + ASSERT_EQUALS("[test.cpp:4]: (style) Member variable not initialized in the constructor 'John::name'\n", errout.str()); + checkUninitVar("class John\n" + "{\n" + "public:\n" + " John() {John::name[0] = '\0';}\n" + "\n" + "private:\n" + " char name[255];\n" + "};\n"); + + ASSERT_EQUALS("", errout.str()); + } + void uninitVarEnum() { checkUninitVar("class Fred\n"