Skip to content

Commit 374e85d

Browse files
Fix 12681: FP: knownConditionTrueFalse (#6416)
While truncating integer values for upper/lower bounds, underflows/overflows must be handled correctly by inverting the bound. --------- Signed-off-by: Francois Berder <fberder@outlook.fr>
1 parent 7419e53 commit 374e85d

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

lib/valueflow.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,8 +2837,17 @@ struct ValueFlowAnalyzer : Analyzer {
28372837
const ValueType *dst = tok->valueType();
28382838
if (dst) {
28392839
const size_t sz = ValueFlow::getSizeOf(*dst, settings);
2840-
if (sz > 0 && sz < 8)
2841-
value->intvalue = truncateIntValue(value->intvalue, sz, dst->sign);
2840+
if (sz > 0 && sz < sizeof(MathLib::biguint)) {
2841+
long long newvalue = truncateIntValue(value->intvalue, sz, dst->sign);
2842+
2843+
/* Handle overflow/underflow for value bounds */
2844+
if (value->bound != ValueFlow::Value::Bound::Point) {
2845+
if ((newvalue > value->intvalue && !inc) || (newvalue < value->intvalue && inc))
2846+
value->invertBound();
2847+
}
2848+
2849+
value->intvalue = newvalue;
2850+
}
28422851

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

6057-
if (value.isIntValue() && sz > 0 && sz < 8)
6066+
if (value.isIntValue() && sz > 0 && sz < sizeof(MathLib::biguint))
60586067
value.intvalue = truncateIntValue(value.intvalue, sz, dst->sign);
60596068
}
60606069
return values;

test/testcondition.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4783,6 +4783,23 @@ class TestCondition : public TestFixture {
47834783
" }\n"
47844784
"}\n");
47854785
ASSERT_EQUALS("", errout_str());
4786+
4787+
// #12681
4788+
check("void f(unsigned u) {\n"
4789+
" if (u > 0) {\n"
4790+
" u--;\n"
4791+
" if (u == 0) {}\n"
4792+
" }\n"
4793+
"}\n");
4794+
ASSERT_EQUALS("", errout_str());
4795+
4796+
check("void f(unsigned u) {\n"
4797+
" if (u < 0xFFFFFFFF) {\n"
4798+
" u++;\n"
4799+
" if (u == 0xFFFFFFFF) {}\n"
4800+
" }\n"
4801+
"}\n");
4802+
ASSERT_EQUALS("", errout_str());
47864803
}
47874804

47884805
void alwaysTrueInfer() {

0 commit comments

Comments
 (0)