Skip to content

Commit

Permalink
Fix 12681: FP: knownConditionTrueFalse (#6416)
Browse files Browse the repository at this point in the history
While truncating integer values for upper/lower bounds,
underflows/overflows must be handled correctly by
inverting the bound.

---------

Signed-off-by: Francois Berder <[email protected]>
  • Loading branch information
francois-berder authored May 21, 2024
1 parent 7419e53 commit 374e85d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2837,8 +2837,17 @@ struct ValueFlowAnalyzer : Analyzer {
const ValueType *dst = tok->valueType();
if (dst) {
const size_t sz = ValueFlow::getSizeOf(*dst, settings);
if (sz > 0 && sz < 8)
value->intvalue = truncateIntValue(value->intvalue, sz, dst->sign);
if (sz > 0 && sz < sizeof(MathLib::biguint)) {
long long newvalue = truncateIntValue(value->intvalue, sz, dst->sign);

/* Handle overflow/underflow for value bounds */
if (value->bound != ValueFlow::Value::Bound::Point) {
if ((newvalue > value->intvalue && !inc) || (newvalue < value->intvalue && inc))
value->invertBound();
}

value->intvalue = newvalue;
}

value->errorPath.emplace_back(tok, tok->str() + " is " + opName + "', new value is " + value->infoString());
}
Expand Down Expand Up @@ -6054,7 +6063,7 @@ static std::list<ValueFlow::Value> truncateValues(std::list<ValueFlow::Value> va
value.valueType = ValueFlow::Value::ValueType::INT;
}

if (value.isIntValue() && sz > 0 && sz < 8)
if (value.isIntValue() && sz > 0 && sz < sizeof(MathLib::biguint))
value.intvalue = truncateIntValue(value.intvalue, sz, dst->sign);
}
return values;
Expand Down
17 changes: 17 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4783,6 +4783,23 @@ class TestCondition : public TestFixture {
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());

// #12681
check("void f(unsigned u) {\n"
" if (u > 0) {\n"
" u--;\n"
" if (u == 0) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("void f(unsigned u) {\n"
" if (u < 0xFFFFFFFF) {\n"
" u++;\n"
" if (u == 0xFFFFFFFF) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void alwaysTrueInfer() {
Expand Down

0 comments on commit 374e85d

Please sign in to comment.