Skip to content

Commit

Permalink
Fix 12681: FP: knownConditionTrueFalse
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 committed May 20, 2024
1 parent 337dfc9 commit 5b7342f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2833,8 +2833,24 @@ 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 < 8) {
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) {
if ((inc && value->bound == ValueFlow::Value::Bound::Lower)
|| (!inc && value->bound == ValueFlow::Value::Bound::Upper))
value->invertBound();
} else if (newvalue < value->intvalue) {
if ((!inc && value->bound == ValueFlow::Value::Bound::Lower)
|| (inc && value->bound == ValueFlow::Value::Bound::Upper))
value->invertBound();
}
}

value->intvalue = newvalue;
}

value->errorPath.emplace_back(tok, tok->str() + " is " + opName + "', new value is " + value->infoString());
}
Expand Down
9 changes: 9 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4783,6 +4783,15 @@ 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());
}

void alwaysTrueInfer() {
Expand Down

0 comments on commit 5b7342f

Please sign in to comment.