From dfedb920f8f9ea7cab648a6a52f67f7f3c584e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 17 Dec 2011 07:56:46 +0100 Subject: [PATCH] Uninitialized variables: Fixed false positive when address of variable is taken inside = { .. }. Ticket: #3369 --- lib/checkuninitvar.cpp | 16 +++++++++++++--- test/testuninitvar.cpp | 9 +++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 9e219cb20..66c0b3e30 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1126,9 +1126,19 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int } } - // skip = { .. } - if (Token::simpleMatch(tok, "= {")) - tok = tok->next()->link(); + // = { .. } + if (Token::simpleMatch(tok, "= {")) { + // end token + const Token *end = tok->next()->link(); + + // If address of variable is taken in the block then bail out + if (Token::findmatch(tok->tokAt(2), "& %varid%", end, varid)) + return true; + + // Skip block + tok = end; + continue; + } // TODO: handle loops, try, etc if (tok->str() == "for" || Token::simpleMatch(tok, ") {") || Token::Match(tok, "%var% {")) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 7c5022388..b921e1cba 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1732,6 +1732,15 @@ private: "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); + // = { .. } + checkUninitVar2("int f() {\n" + " int a;\n" + " int *p[] = { &a };\n" + " *p[0] = 0;\n" + " return a;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // >> => initialization / usage { const char code[] = "void f() {\n"