Skip to content

Commit

Permalink
Partial fix for 12031: False positive: uninitialized variable (danmar…
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 authored Oct 18, 2023
1 parent 09f426d commit e1a120e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down
16 changes: 16 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5605,6 +5605,22 @@ class TestValueFlow : public TestFixture {
"}\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() {
Expand Down

0 comments on commit e1a120e

Please sign in to comment.