From 4ac5648656ba4281e5f0c166c9d88cd58106fb64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 5 Feb 2013 17:01:46 +0100 Subject: [PATCH] Uninitialized struct: Fixed false positive in for loop --- lib/checkuninitvar.cpp | 8 ++++++-- test/testuninitvar.cpp | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 3659d84f9..1fa8d4d6e 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1407,8 +1407,12 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va for (const Token *tok = startparentheses->next(); tok && tok != endpar; tok = tok->next()) { if (tok->varId() == var.varId()) { if (Token::Match(tok, "%var% . %var%")) { - if (tok->strAt(2) == membervar) - uninitStructMemberError(tok, tok->str() + "." + membervar); + if (tok->strAt(2) == membervar) { + if (tok->strAt(3) == "=") + return true; + else + uninitStructMemberError(tok, tok->str() + "." + membervar); + } continue; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 4cd0e95d6..6802c2d6f 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2662,6 +2662,13 @@ private: " if (fred.b == 0) { }\n" "}\n", "test.c", true); ASSERT_EQUALS("[test.c:9]: (error) Uninitialized struct member: fred.b\n", errout.str()); + + checkUninitVar2("struct S { int n; int m; };\n" + "void f(void) {\n" + " struct S s;\n" + " for (s.n = 0; s.n <= 10; s.n++) { }\n" + "}", "test.c"); + ASSERT_EQUALS("", errout.str()); } void uninitvar2_while() {