From 299c1bb208d6a3da78af6d31a5d74b061fe56ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 25 Dec 2012 10:37:21 +0100 Subject: [PATCH] Uninitialized variables: handle unsimplified labels better --- lib/checkuninitvar.cpp | 26 +++++++++++++++++++------- test/testuninitvar.cpp | 13 +++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 0ec0184a7..d3ed14d72 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1097,8 +1097,6 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, if (possibleInit) *possibleInit = false; - bool ret = false; - unsigned int number_of_if = 0; // variable values @@ -1292,10 +1290,24 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, if (Token::Match(tok, "return|break|continue|throw|goto")) { if (noreturn) *noreturn = true; - else - ret = true; - } else if (tok->str() == "goto") - return true; + + while (tok && tok->str() != ";") { + // variable is seen.. + if (tok->varId() == var.varId()) { + // Use variable + if (!suppressErrors && isVariableUsage(scope, tok, var.isPointer())) + uninitvarError(tok, tok->str()); + + else + // assume that variable is assigned + return true; + } + + tok = tok->next(); + } + + return bool(noreturn==NULL); + } // variable is seen.. if (tok->varId() == var.varId()) { @@ -1309,7 +1321,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, } } - return ret; + return false; } bool CheckUninitVar::checkIfForWhileHead(const Scope *scope, const Token *startparanthesis, const Variable& var, bool suppressErrors, bool isuninit) diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index a51a25cdb..c790f5c4f 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2106,6 +2106,19 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + checkUninitVar2("int f(int x) {\n" + " int ret;\n" + " if (!x) {\n" + " ret = -123;\n" + " goto out1;\n" + " }\n" + " return 0;\n" + "out1:\n" + "out2:\n" + " return ret;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + checkUninitVar2("void f() {\n" " int i;\n" " if (x) {\n"