From 9fe9be1ea90a01b195cd285e502c44e1e74cf5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 13 Mar 2010 20:24:39 +0100 Subject: [PATCH] Fixed #1465 (false positive: unintialized class member) --- lib/checkclass.cpp | 16 ++++++++++------ test/testclass.cpp | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 3f80505ad..28e7f09f5 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -235,7 +235,7 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va } // Before a new statement there is "[{};)=]" or "else" - if (! Token::Match(ftok, "[{};)=]") && ftok->str() != "else") + if (! Token::Match(ftok, "[{};()=]") && ftok->str() != "else") continue; // Using the operator= function to initialize all variables.. @@ -285,18 +285,22 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va continue; } + else if (ftok->str() == "if") + continue; + // Calling member function? else if (Token::Match(ftok, "%var% (")) { // No recursive calls! if (std::find(callstack.begin(), callstack.end(), ftok->str()) == callstack.end()) { - callstack.push_back(ftok->str()); int i = 0; const Token *ftok2 = _tokenizer->findClassFunction(tok1, classname, ftok->strAt(0), i, isStruct); if (ftok2) { + callstack.push_back(ftok->str()); initializeVarList(tok1, ftok2, varlist, classname, callstack, isStruct); + callstack.pop_back(); } else // there is a called member function, but it is not defined where we can find it, so we assume it initializes everything { @@ -331,16 +335,16 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va // the function is external and it's neither friend nor inherited virtual function. // assume all variables that are passed to it are initialized.. - unsigned int indentlevel = 0; + unsigned int indentlevel2 = 0; for (tok = ftok->tokAt(2); tok; tok = tok->next()) { if (tok->str() == "(") - ++indentlevel; + ++indentlevel2; else if (tok->str() == ")") { - if (indentlevel == 0) + if (indentlevel2 == 0) break; - --indentlevel; + --indentlevel2; } if (tok->isName()) { diff --git a/test/testclass.cpp b/test/testclass.cpp index 668e3b709..812cd0973 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -52,6 +52,7 @@ private: TEST_CASE(uninitVarArray1); TEST_CASE(uninitVarArray2); TEST_CASE(uninitVarArray3); + TEST_CASE(uninitVarArray4); TEST_CASE(uninitVarArray2D); TEST_CASE(uninitMissingFuncDef);// can't expand function in constructor TEST_CASE(privateCtor1); // If constructor is private.. @@ -1185,6 +1186,24 @@ private: ASSERT_EQUALS("", errout.str()); } + void uninitVarArray4() + { + checkUninitVar("class John\n" + "{\n" + "private:\n" + " int a[100];\n" + " int b[100];\n" + "\n" + "public:\n" + " John()\n" + " {\n" + " if (snprintf(a,10,\"a\")) { }\n" + " if (snprintf(b,10,\"b\")) { }\n" + " }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } + void uninitVarArray2D() { checkUninitVar("class John\n"