Skip to content

Commit 9e9a982

Browse files
authored
Fix 10468: False positive; uninitialized variable. Loop is always executed at least once (#3462)
1 parent 578d3c3 commit 9e9a982

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

lib/forwardanalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct ForwardTraversal {
6767
std::vector<int> result = analyzer->evaluate(tok, ctx);
6868
// TODO: We should convert to bool
6969
bool checkThen = std::any_of(result.begin(), result.end(), [](int x) {
70-
return x == 1;
70+
return x != 0;
7171
});
7272
bool checkElse = std::any_of(result.begin(), result.end(), [](int x) {
7373
return x == 0;

lib/valueflow.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se
705705
}
706706
} else if (!value.isImpossible()) {
707707
// is condition only depending on 1 variable?
708+
// cppcheck-suppress[variableScope] #8541
708709
nonneg int varId = 0;
709710
bool ret = false;
710711
visitAstNodes(parent->astOperand1(),
@@ -724,9 +725,6 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se
724725
v.conditional = true;
725726
v.changeKnownToPossible();
726727

727-
if (varId)
728-
v.varId = varId;
729-
730728
setTokenValue(parent, v, settings);
731729
}
732730
}

test/testuninitvar.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4895,6 +4895,15 @@ class TestUninitVar : public TestFixture {
48954895
" return a;\n" // <- a has been initialized
48964896
"}");
48974897
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
4898+
4899+
// #10468
4900+
valueFlowUninit("uint32_t foo(uint32_t in) {\n"
4901+
" uint32_t out, mask = 0x7F;\n"
4902+
" while (mask ^ 0x7FFFFFFF)\n"
4903+
" out = in & ~mask;\n"
4904+
" return out;\n"
4905+
"}\n");
4906+
ASSERT_EQUALS("", errout.str());
48984907
}
48994908

49004909
void uninitvar_ipa() {

0 commit comments

Comments
 (0)