Skip to content

Commit 7f0234e

Browse files
authored
Fix 12032: False positive: uninitialized variable, flags with same value (#5754)
1 parent 77157a6 commit 7f0234e

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

lib/programmemory.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,14 @@ namespace {
16401640
return *it;
16411641
}
16421642

1643+
static bool updateValue(ValueFlow::Value& v, ValueFlow::Value x)
1644+
{
1645+
const bool returnValue = !x.isUninitValue() && !x.isImpossible();
1646+
if (v.isUninitValue() || returnValue)
1647+
v = std::move(x);
1648+
return returnValue;
1649+
}
1650+
16431651
ValueFlow::Value execute(const Token* expr)
16441652
{
16451653
depth--;
@@ -1648,13 +1656,29 @@ namespace {
16481656
}};
16491657
if (depth < 0)
16501658
return unknown();
1651-
ValueFlow::Value v = executeImpl(expr);
1652-
if (!v.isUninitValue())
1659+
ValueFlow::Value v = unknown();
1660+
if (updateValue(v, executeImpl(expr)))
16531661
return v;
16541662
if (!expr)
16551663
return v;
1656-
if (expr->exprId() > 0 && pm->hasValue(expr->exprId()))
1657-
return pm->at(expr->exprId());
1664+
if (expr->exprId() > 0 && pm->hasValue(expr->exprId())) {
1665+
if (updateValue(v, pm->at(expr->exprId())))
1666+
return v;
1667+
}
1668+
// Find symbolic values
1669+
for (const ValueFlow::Value& value : expr->values()) {
1670+
if (!value.isSymbolicValue())
1671+
continue;
1672+
if (!value.isKnown())
1673+
continue;
1674+
if (value.tokvalue->exprId() > 0 && !pm->hasValue(value.tokvalue->exprId()))
1675+
continue;
1676+
ValueFlow::Value v2 = pm->at(value.tokvalue->exprId());
1677+
if (!v2.isIntValue() && value.intvalue != 0)
1678+
continue;
1679+
v2.intvalue += value.intvalue;
1680+
return v2;
1681+
}
16581682
if (const ValueFlow::Value* value = getImpossibleValue(expr))
16591683
return *value;
16601684
return v;

test/testvalueflow.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5645,6 +5645,22 @@ class TestValueFlow : public TestFixture {
56455645
values = tokenValues(code, "x <", ValueFlow::Value::ValueType::UNINIT);
56465646
ASSERT_EQUALS(0, values.size());
56475647

5648+
code = "void getX(int *p);\n"
5649+
"bool do_something();\n"
5650+
"void foo() {\n"
5651+
" int x;\n"
5652+
" bool flag;\n"
5653+
" bool success;\n"
5654+
" success = do_something();\n"
5655+
" flag = success;\n"
5656+
" if (success == true) {\n"
5657+
" getX(&x);\n"
5658+
" }\n"
5659+
" for (int i = 0; (flag == true) && (i < x); ++i) {}\n"
5660+
"}\n";
5661+
values = tokenValues(code, "x ) ; ++ i", ValueFlow::Value::ValueType::UNINIT);
5662+
ASSERT_EQUALS(0, values.size());
5663+
56485664
code = "void g(bool *result, size_t *buflen) {\n" // #12091
56495665
" if (*result && *buflen >= 5) {}\n" // <- *buflen might not be initialized
56505666
"}\n"

0 commit comments

Comments
 (0)