Skip to content

Commit 9815a7b

Browse files
committed
Correct widening overflow detection for unsigned types
1 parent 5cd42eb commit 9815a7b

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/analyses/variable-sensitivity/widened_range.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ static bool has_underflowed(const exprt &value)
1414
return constant_interval_exprt::greater_than(
1515
value, from_integer(0, value.type()));
1616
}
17-
static bool has_overflowed(const exprt &value)
17+
static bool has_overflowed(const exprt &value, const exprt &initial_value)
1818
{
19-
return constant_interval_exprt::less_than(
20-
value, from_integer(0, value.type()));
19+
return constant_interval_exprt::less_than(value, initial_value);
2120
}
2221

2322
exprt widened_ranget::widen_lower_bound() const
@@ -44,7 +43,7 @@ exprt widened_ranget::widen_upper_bound() const
4443

4544
if(
4645
constant_interval_exprt::contains_extreme(new_upper_bound) ||
47-
has_overflowed(new_upper_bound))
46+
has_overflowed(new_upper_bound, upper_bound))
4847
return max_exprt(upper_bound.type());
4948

5049
return new_upper_bound;

unit/analyses/variable-sensitivity/interval_abstract_value/widening_merge.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,43 @@ SCENARIO(
332332
EXPECT_MODIFIED(merged, val0, valMax);
333333
}
334334
}
335+
WHEN("merging [0, 1] with [1, unsigned_very_large]")
336+
{
337+
auto utype = unsignedbv_typet(32);
338+
auto uval0 = from_integer(0, utype);
339+
auto uval1 = from_integer(1, utype);
340+
auto uVeryLarge = from_integer(2 << 30, utype);
341+
342+
auto op1 = make_interval(uval0, uval1, environment, ns);
343+
auto op2 = make_interval(uval1, uVeryLarge, environment, ns);
344+
345+
auto merged = widening_merge(op1, op2);
346+
347+
THEN("result is [0, MAX]")
348+
{
349+
auto uvalMax = max_exprt(utype);
350+
EXPECT_MODIFIED(merged, uval0, uvalMax);
351+
}
352+
}
353+
WHEN("merging unsigned [7, 9] with [3, 6]")
354+
{
355+
auto utype = unsignedbv_typet(32);
356+
auto uval0 = from_integer(0, utype);
357+
auto uval3 = from_integer(3, utype);
358+
auto uval6 = from_integer(6, utype);
359+
auto uval7 = from_integer(7, utype);
360+
auto uval9 = from_integer(9, utype);
361+
362+
auto op1 = make_interval(uval7, uval9, environment, ns);
363+
auto op2 = make_interval(uval3, uval6, environment, ns);
364+
365+
auto merged = widening_merge(op1, op2);
366+
367+
THEN("result is [0, 9]")
368+
{
369+
EXPECT_MODIFIED(merged, uval0, uval9);
370+
}
371+
}
335372

336373
WHEN("merging [1, 10] with [MIN, 1]")
337374
{

0 commit comments

Comments
 (0)