From ab4187924662424bcccb1c905b0aee8e5380c72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 30 May 2010 09:00:18 +0200 Subject: [PATCH] ExecutionPath: Better handling of 'FOREACH (..) {}' --- lib/executionpath.cpp | 26 +++++++++++++------------- test/testother.cpp | 9 +++++++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/executionpath.cpp b/lib/executionpath.cpp index 2cef71233..c05aed1e5 100644 --- a/lib/executionpath.cpp +++ b/lib/executionpath.cpp @@ -158,24 +158,24 @@ static void checkExecutionPaths_(const Token *tok, std::list &c } // bailout used variables in '; FOREACH ( .. ) { .. }' - else if (Token::Match(tok->previous(), "[;{}] %var% (")) + else if (tok->str() != "if" && Token::Match(tok->previous(), "[;{}] %var% (")) { // goto { - const Token *tok2 = tok->tokAt(2); - if (tok2 && tok2->str() == "(") - tok2 = tok2->link(); + const Token *tok2 = tok->next()->link(); if (tok2 && tok2->str() == ")") - tok2 = tok2->next(); - if (tok2 && tok2->str() == "{") { - // goto "}" - tok2 = tok2->link(); - - // bail out all variables used in "{ .. }" - for (; tok && tok != tok2; tok = tok->next()) + tok2 = tok2->next(); + if (tok2 && tok2->str() == "{") { - if (tok->varId()) - ExecutionPath::bailOutVar(checks, tok->varId()); + // goto "}" + tok2 = tok2->link(); + + // bail out all variables used in "{ .. }" + for (; tok && tok != tok2; tok = tok->next()) + { + if (tok->varId()) + ExecutionPath::bailOutVar(checks, tok->varId()); + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index 1bdee4af7..603f28e21 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1382,6 +1382,15 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: s\n", errout.str()); + checkUninitVar("void a()\n" + "{\n" + " struct S *s1;\n" + " struct S *s2;\n" + " FOREACH(s1) { }\n" + " s2->x = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: s2\n", errout.str()); + // #1533 checkUninitVar("char a()\n" "{\n"