Fix 10468: False positive; uninitialized variable. Loop is always executed at least once (#3462)

This commit is contained in:
Paul Fultz II 2021-09-18 15:23:05 -05:00 committed by GitHub
parent 578d3c3a93
commit 9e9a982c6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 4 deletions

View File

@ -67,7 +67,7 @@ struct ForwardTraversal {
std::vector<int> result = analyzer->evaluate(tok, ctx);
// TODO: We should convert to bool
bool checkThen = std::any_of(result.begin(), result.end(), [](int x) {
return x == 1;
return x != 0;
});
bool checkElse = std::any_of(result.begin(), result.end(), [](int x) {
return x == 0;

View File

@ -705,6 +705,7 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se
}
} else if (!value.isImpossible()) {
// is condition only depending on 1 variable?
// cppcheck-suppress[variableScope] #8541
nonneg int varId = 0;
bool ret = false;
visitAstNodes(parent->astOperand1(),
@ -724,9 +725,6 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se
v.conditional = true;
v.changeKnownToPossible();
if (varId)
v.varId = varId;
setTokenValue(parent, v, settings);
}
}

View File

@ -4895,6 +4895,15 @@ private:
" return a;\n" // <- a has been initialized
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
// #10468
valueFlowUninit("uint32_t foo(uint32_t in) {\n"
" uint32_t out, mask = 0x7F;\n"
" while (mask ^ 0x7FFFFFFF)\n"
" out = in & ~mask;\n"
" return out;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void uninitvar_ipa() {