From 04cd261ee246cf2944296636135cc7567e2192fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 21 Apr 2014 10:32:29 +0200 Subject: [PATCH] Fixed #5664 (false positive: (error) Uninitialized variable: j (Comma operator in for loop)) --- lib/checkuninitvar.cpp | 4 +++- test/testuninitvar.cpp | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 082030325..b1d70ea4e 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1602,7 +1602,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const bool assign = true; if (tok->strAt(1) == "=") { unsigned int indentlevel = 0; // Handle '(a=1)..' - for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) { + for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { if (tok2->varId() == var.declarationId()) { assign = false; break; @@ -1615,6 +1615,8 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const if (indentlevel <= 1U) break; --indentlevel; + } else if (tok2->str() == ";" || tok2->str() == ",") { + break; } } } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 66505bfae..89d19c5cb 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -3274,6 +3274,14 @@ private: " }\n" "}"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar2("void f(void) {\n" + " int i;\n" + " while (x) {\n" + " for (i=0,y=i;;){}\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void uninitvar2_4494() {