From 4779cb124e33381e35745225a93db2ac2d9cec5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 7 May 2020 22:25:08 +0200 Subject: [PATCH] Fixed #9649 (False positive: uninitialized struct member) --- lib/checkuninitvar.cpp | 18 ++++++++---------- test/testuninitvar.cpp | 13 +++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 62f81be6c..3b1e6f24f 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -435,20 +435,18 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var const Token *condition = tok->next()->astOperand2(); const Token *lhs = condition->astOperand1(); const Token *rhs = condition->astOperand2(); - const Token *vartok = rhs && rhs->isNumber() ? lhs : rhs; - const Token *numtok = rhs && rhs->isNumber() ? rhs : lhs; + const Token *vartok = (lhs && lhs->hasKnownIntValue()) ? rhs : lhs; + const Token *numtok = (lhs == vartok) ? rhs : lhs; while (Token::simpleMatch(vartok, ".")) vartok = vartok->astOperand2(); - if (vartok && vartok->varId() && numtok) { + if (vartok && vartok->varId() && numtok && numtok->hasKnownIntValue()) { const std::map::const_iterator it = variableValue.find(vartok->varId()); - if (it != variableValue.end() && it->second != MathLib::toLongNumber(numtok->str())) + if (it != variableValue.end() && it->second != numtok->getKnownIntValue()) return true; // this scope is not fully analysed => return true - else { - condVarId = vartok->varId(); - condVarValue = VariableValue(MathLib::toLongNumber(numtok->str())); - if (condition->str() == "!=") - condVarValue = !condVarValue; - } + condVarId = vartok->varId(); + condVarValue = VariableValue(numtok->getKnownIntValue()); + if (condition->str() == "!=") + condVarValue = !condVarValue; } } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 31231719d..a91291a53 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2523,6 +2523,19 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + checkUninitVar("enum t_err { ERR_NONE, ERR_BAD_ARGS };\n" // #9649 + "struct box_t { int value; };\n" + "int init_box(box_t *p, int v);\n" + "\n" + "void foo(int ret) {\n" + " box_t box2;\n" + " if (ret == ERR_NONE)\n" + " ret = init_box(&box2, 20);\n" + " if (ret == ERR_NONE)\n" + " z = x + box2.value;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + checkUninitVar("void f(int x) {\n" " int value;\n" " if (x == 32)\n"