Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2833,8 +2833,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 @@ -6049,7 +6058,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