Partial fix for 12031: False positive: uninitialized variable (#5573)

This commit is contained in:
Paul Fultz II 2023-10-18 16:49:22 -05:00 committed by GitHub
parent 09f426d980
commit e1a120e6b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View File

@ -204,8 +204,10 @@ static bool evaluateCondition(const std::string& op,
if (!condition)
return false;
if (condition->str() == op) {
return evaluateCondition(op, r, condition->astOperand1(), pm, settings) ||
evaluateCondition(op, r, condition->astOperand2(), pm, settings);
if (evaluateCondition(op, r, condition->astOperand1(), pm, settings) ||
evaluateCondition(op, r, condition->astOperand2(), pm, settings)) {
return true;
}
}
MathLib::bigint result = 0;
bool error = false;
@ -309,8 +311,10 @@ void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, const Toke
if (lhs.empty() || rhs.empty()) {
if (frontIs(lhs, !then))
programMemoryParseCondition(pm, tok->astOperand2(), endTok, settings, then);
if (frontIs(rhs, !then))
else if (frontIs(rhs, !then))
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, then);
else
pm.setIntValue(tok, 0, then);
}
} else if (tok->exprId() > 0) {
if (endTok && isExpressionChanged(tok, tok->next(), endTok, settings, true))

View File

@ -5605,6 +5605,22 @@ private:
"}\n";
values = tokenValues(code, "x >", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size());
// #12031
code = "bool g(int *p);\n"
"bool h();\n"
"void f(bool b, int y) {\n"
" int x;\n"
" if (b && y > 0) {\n"
" b = g(&x);\n"
" }\n"
" while (b && y > 0) {\n"
" if (x < 0) {}\n"
" break;\n"
" }\n"
"}\n";
values = tokenValues(code, "x <", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size());
}
void valueFlowConditionExpressions() {