From d34924ba6d68d6be22df36cbc4480641fbe4abed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 17 Jul 2012 07:03:40 +0200 Subject: [PATCH] Uninitialized variables: Fixed false positive when there is assignment in condition --- lib/checkuninitvar.cpp | 12 +++++++++++- test/testuninitvar.cpp | 6 ++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index cd1b74809..14d9cf18d 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -33,6 +33,16 @@ namespace { //--------------------------------------------------------------------------- + +// Skip [ .. ] +static const Token * skipBrackets(const Token *tok) +{ + while (tok && tok->str() == "[") + tok = tok->link()->next(); + return tok; +} + + /// @addtogroup Checks /// @{ @@ -860,7 +870,7 @@ private: if (tok.varId() && Token::Match(&tok, "%var% <|<=|==|!=|)")) use(checks, &tok); - else if (Token::Match(&tok, "!| %var% [")) + else if (Token::Match(&tok, "!| %var% [") && !Token::simpleMatch(skipBrackets(tok.next()), "=")) use_array_or_pointer_data(checks, tok.str() == "!" ? tok.next() : &tok); else if (Token::Match(&tok, "!| %var% (")) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 6a6678378..e56fe9527 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -688,6 +688,12 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + checkUninitVar("void f() {\n" + " char a[10];\n" + " if (a[0] = x){}\n" + "}"); + ASSERT_EQUALS("", errout.str()); + checkUninitVar("int foo(int x)\n" "{\n" " int i;\n"