Skip to content

Commit 724467c

Browse files
authored
Merge pull request #6541 from tautschnig/fix-rmc-708
Simplification of nested pointer arithmetic: do not assume same type
2 parents 868a035 + b4c2ff1 commit 724467c

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

regression/cbmc/Pointer_Arithmetic15/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ int main()
1515

1616
a = p - q;
1717

18+
// mixing types: the C front-end will insert casts
19+
unsigned long long u;
20+
p = (p + a) + u;
21+
1822
assert(0);
1923
}

src/util/simplify_expr_int.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,16 +439,24 @@ simplify_exprt::resultt<> simplify_exprt::simplify_plus(const plus_exprt &expr)
439439
expr.op0().id() == ID_plus && expr.op0().type().id() == ID_pointer &&
440440
expr.op0().operands().size() == 2)
441441
{
442-
plus_exprt op0 = to_plus_expr(expr.op0());
442+
plus_exprt result = to_plus_expr(expr.op0());
443+
if(as_const(result).op0().type().id() != ID_pointer)
444+
result.op0().swap(result.op1());
445+
const exprt &op1 = as_const(result).op1();
443446

444-
if(op0.op1().id() == ID_plus)
445-
to_plus_expr(op0.op1()).add_to_operands(expr.op1());
447+
if(op1.id() == ID_plus)
448+
{
449+
plus_exprt new_op1 = to_plus_expr(op1);
450+
new_op1.add_to_operands(
451+
typecast_exprt::conditional_cast(expr.op1(), new_op1.op0().type()));
452+
result.op1() = simplify_plus(new_op1);
453+
}
446454
else
447-
op0.op1()=plus_exprt(op0.op1(), expr.op1());
448-
449-
auto result = op0;
450-
451-
result.op1() = simplify_plus(to_plus_expr(result.op1()));
455+
{
456+
plus_exprt new_op1{
457+
op1, typecast_exprt::conditional_cast(expr.op1(), op1.type())};
458+
result.op1() = simplify_plus(new_op1);
459+
}
452460

453461
return changed(simplify_plus(result));
454462
}

0 commit comments

Comments
 (0)