Skip to content

Commit ce53931

Browse files
committed
Fixed #9251 (False positive: unininitialized variable (multi variables))
1 parent 2443141 commit ce53931

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,25 +1908,22 @@ static void valueFlowAST(Token *tok, nonneg int varid, const ValueFlow::Value &v
19081908
}
19091909

19101910
/** if known variable is changed in loop body, change it to a possible value */
1911-
static void handleKnownValuesInLoop(const Token *startToken,
1911+
static bool handleKnownValuesInLoop(const Token *startToken,
19121912
const Token *endToken,
19131913
std::list<ValueFlow::Value> *values,
19141914
nonneg int varid,
19151915
bool globalvar,
19161916
const Settings *settings)
19171917
{
1918-
bool isChanged = false;
1918+
const bool isChanged = isVariableChanged(startToken, endToken, varid, globalvar, settings, true);
1919+
if (!isChanged)
1920+
return false;
19191921
for (std::list<ValueFlow::Value>::iterator it = values->begin(); it != values->end(); ++it) {
19201922
if (it->isKnown()) {
1921-
if (!isChanged) {
1922-
if (!isVariableChanged(startToken, endToken, varid, globalvar, settings, true))
1923-
break;
1924-
isChanged = true;
1925-
}
1926-
19271923
it->setPossible();
19281924
}
19291925
}
1926+
return isChanged;
19301927
}
19311928

19321929
static bool evalAssignment(ValueFlow::Value &lhsValue, const std::string &assign, const ValueFlow::Value &rhsValue)
@@ -2113,8 +2110,10 @@ static bool valueFlowForward(Token * const startToken,
21132110
}
21142111

21152112
// if known variable is changed in loop body, change it to a possible value..
2116-
if (Token::Match(tok2, "for|while"))
2117-
handleKnownValuesInLoop(tok2, tok2->linkAt(1)->linkAt(1), &values, varid, var->isGlobal(), settings);
2113+
if (Token::Match(tok2, "for|while")) {
2114+
if (handleKnownValuesInLoop(tok2, tok2->linkAt(1)->linkAt(1), &values, varid, var->isGlobal(), settings))
2115+
number_of_if++;
2116+
}
21182117

21192118
// Set values in condition
21202119
for (Token* tok3 = tok2->tokAt(2); tok3 != tok2->next()->link(); tok3 = tok3->next()) {

‎test/testvalueflow.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,21 @@ class TestValueFlow : public TestFixture {
19121912
"}\n";
19131913
ASSERT_EQUALS(true, testValueOfX(code, 9U, 0)); // x can be 0 at line 9
19141914

1915+
code = "void f(const int *buf) {\n"
1916+
" int x = 111;\n"
1917+
" bool found = false;\n"
1918+
" for (int i = 0; i < 10; i++) {\n"
1919+
" if (buf[i] == 123) {\n"
1920+
" x = i;\n"
1921+
" found = true;\n"
1922+
" break;\n"
1923+
" }\n"
1924+
" }\n"
1925+
" if (found)\n"
1926+
" a = x;\n" // <- x can't be 111
1927+
"}\n";
1928+
ASSERT_EQUALS(false, testValueOfX(code, 12U, 111)); // x can not be 111 at line 9
1929+
19151930
code = "void f(const int *buf) {\n"
19161931
" int x = 0;\n"
19171932
" for (int i = 0; i < 10; i++) {\n"

0 commit comments

Comments
 (0)