Skip to content

Commit 5cb8cc7

Browse files
committed
use calculateArithmetic for prefix ++/-- in setTokenValue and in solveExprValue
1 parent 89183e7 commit 5cb8cc7

4 files changed

Lines changed: 40 additions & 11 deletions

File tree

‎lib/infer.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,12 @@ namespace {
175175
friend Interval operator-(const Interval& lhs, const Interval& rhs)
176176
{
177177
Interval result;
178-
result.minvalue = Interval::apply(lhs.minvalue, rhs.maxvalue, std::minus<MathLib::bigint>{});
179-
result.maxvalue = Interval::apply(lhs.maxvalue, rhs.minvalue, std::minus<MathLib::bigint>{});
178+
result.minvalue = Interval::apply(lhs.minvalue, rhs.maxvalue, [](MathLib::bigint x, MathLib::bigint y) {
179+
return calculateArithmetic('-', x, y);
180+
});
181+
result.maxvalue = Interval::apply(lhs.maxvalue, rhs.minvalue, [](MathLib::bigint x, MathLib::bigint y) {
182+
return calculateArithmetic('-', x, y);
183+
});
180184
if (!result.minvalue.empty())
181185
result.minRef = merge(lhs.minRef, rhs.maxRef);
182186
if (!result.maxvalue.empty())

‎lib/valueflow.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6284,16 +6284,16 @@ const Token* ValueFlow::solveExprValue(const Token* expr,
62846284
if (binaryTok && expr->str().size() == 1) {
62856285
switch (expr->str()[0]) {
62866286
case '+': {
6287-
value.intvalue -= intval;
6287+
value.intvalue = calculateArithmetic<MathLib::bigint>('-', value.intvalue, intval);
62886288
return ValueFlow::solveExprValue(binaryTok, eval, value);
62896289
}
62906290
case '-': {
62916291
if (rhs) {
6292-
value.intvalue = intval - value.intvalue;
6292+
value.intvalue = calculateArithmetic<MathLib::bigint>('-', intval, value.intvalue);
62936293
// c - x >= a <=> x <= c - a
62946294
value.invertBound();
62956295
} else
6296-
value.intvalue += intval;
6296+
value.intvalue = calculateArithmetic<MathLib::bigint>('+', value.intvalue, intval);
62976297
return ValueFlow::solveExprValue(binaryTok, eval, value);
62986298
}
62996299
case '*': {

‎lib/vf_settokenvalue.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -682,16 +682,16 @@ namespace ValueFlow
682682
const ValueType *dst = tok->valueType();
683683
if (dst) {
684684
const size_t sz = dst->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
685-
MathLib::bigint newvalue = ValueFlow::truncateIntValue(v.intvalue + 1, sz, dst, settings.platform);
685+
MathLib::bigint newvalue = ValueFlow::truncateIntValue(calculateArithmetic<MathLib::bigint>('+', v.intvalue, 1), sz, dst, settings.platform);
686686
if (v.bound != ValueFlow::Value::Bound::Point) {
687687
if (newvalue < v.intvalue) {
688688
v.invertBound();
689-
newvalue -= 2;
689+
newvalue = calculateArithmetic<MathLib::bigint>('-', newvalue, 2);
690690
}
691691
}
692692
v.intvalue = newvalue;
693693
} else {
694-
v.intvalue = v.intvalue + 1;
694+
v.intvalue = calculateArithmetic<MathLib::bigint>('+', v.intvalue, 1);
695695
}
696696
}
697697
else
@@ -712,16 +712,16 @@ namespace ValueFlow
712712
const ValueType *dst = tok->valueType();
713713
if (dst) {
714714
const size_t sz = dst->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
715-
MathLib::bigint newvalue = ValueFlow::truncateIntValue(v.intvalue - 1, sz, dst, settings.platform);
715+
MathLib::bigint newvalue = ValueFlow::truncateIntValue(calculateArithmetic<MathLib::bigint>('-', v.intvalue, 1), sz, dst, settings.platform);
716716
if (v.bound != ValueFlow::Value::Bound::Point) {
717717
if (newvalue > v.intvalue) {
718718
v.invertBound();
719-
newvalue += 2;
719+
newvalue = calculateArithmetic<MathLib::bigint>('+', newvalue, 2);
720720
}
721721
}
722722
v.intvalue = newvalue;
723723
} else {
724-
v.intvalue = v.intvalue - 1;
724+
v.intvalue = calculateArithmetic<MathLib::bigint>('-', v.intvalue, 1);
725725
}
726726
}
727727
else

‎test/testvalueflow.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,31 @@ class TestValueFlow : public TestFixture {
24872487
"}\n";
24882488
ASSERT_EQUALS(true, testValueOfX(code, 4U, LLONG_MIN));
24892489

2490+
// prefix increment/decrement in an expression wraps without signed integer overflow
2491+
code = "long long f() {\n"
2492+
" long long m = 0x7fffffffffffffff;\n"
2493+
" long long x = ++m;\n"
2494+
" return x;\n"
2495+
"}\n";
2496+
ASSERT_EQUALS(true, testValueOfX(code, 4U, LLONG_MIN));
2497+
2498+
code = "long long f() {\n"
2499+
" long long m = -0x7fffffffffffffff - 1;\n"
2500+
" long long x = --m;\n"
2501+
" return x;\n"
2502+
"}\n";
2503+
ASSERT_EQUALS(true, testValueOfX(code, 4U, LLONG_MAX));
2504+
2505+
// solving chained subtractions of large constants wraps without signed integer overflow
2506+
code = "void f(long long a) {\n"
2507+
" long long y = a - 0x7fffffffffffffff;\n"
2508+
" long long x = y - 0x7fffffffffffffff;\n"
2509+
" long long w = x - 0x7fffffffffffffff;\n"
2510+
" if (w == 0) {}\n"
2511+
" if (a == 0) {}\n"
2512+
"}\n";
2513+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 2));
2514+
24902515
code = "void f() {\n"
24912516
" int x = 123;\n"
24922517
" a = sizeof(x);\n"

0 commit comments

Comments
 (0)