Skip to content

Commit 4ab7663

Browse files
committed
equal_exprt now checks types of the operands
This commit adds a precondition to the constructor of equal_exprt, which enforces that the two operands have the same type.
1 parent 73d258c commit 4ab7663

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

src/cpp/cpp_typecheck_code.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ void cpp_typecheckt::typecheck_code(codet &code)
7373
shl_exprt shl{from_integer(1, array.type()),
7474
to_index_expr(binary_expr.op0()).index()};
7575
exprt rhs = if_exprt{
76-
equal_exprt{binary_expr.op1(), from_integer(0, array.type())},
76+
equal_exprt{
77+
binary_expr.op1(), from_integer(0, binary_expr.op1().type())},
7778
bitand_exprt{array, bitnot_exprt{shl}},
7879
bitor_exprt{array, shl}};
7980
binary_expr.op0() = to_index_expr(binary_expr.op0()).array();

src/goto-symex/symex_other.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,13 @@ void goto_symext::symex_other(
224224
process_array_expr(state, array1);
225225
process_array_expr(state, array2);
226226

227-
exprt rhs = equal_exprt(array1, array2);
227+
exprt rhs;
228228

229-
// check for size (or type) mismatch
229+
// Types don't match? Make it 'false'.
230230
if(array1.type() != array2.type())
231231
rhs = false_exprt();
232+
else
233+
rhs = equal_exprt(array1, array2);
232234

233235
symex_assign(state, code.op2(), rhs);
234236
}

src/linking/linking.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,17 @@ bool linkingt::adjust_object_type_rec(
949949
}
950950
else
951951
{
952+
if(new_size.type() != old_size.type())
953+
{
954+
link_error(
955+
info.old_symbol,
956+
info.new_symbol,
957+
"conflicting array sizes for variable");
958+
959+
// error logged, continue typechecking other symbols
960+
return true;
961+
}
962+
952963
equal_exprt eq(old_size, new_size);
953964

954965
if(!simplify_expr(eq, ns).is_true())

src/util/std_expr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,7 @@ class equal_exprt:public binary_relation_exprt
13081308
equal_exprt(exprt _lhs, exprt _rhs)
13091309
: binary_relation_exprt(std::move(_lhs), ID_equal, std::move(_rhs))
13101310
{
1311+
PRECONDITION(lhs().type() == rhs().type());
13111312
}
13121313

13131314
static void check(

unit/goto-symex/try_evaluate_pointer_comparisons.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,14 @@ SCENARIO(
8080
}
8181
}
8282

83-
WHEN("Evaluating ptr1 == (long*)(short*)&value1")
83+
WHEN("Evaluating ptr1 == (int*)(short*)&value1")
8484
{
8585
const signedbv_typet long_type{64};
8686
const signedbv_typet short_type{16};
87-
const pointer_typet ptr_long_type = pointer_type(long_type);
8887
const pointer_typet ptr_short_type = pointer_type(short_type);
8988
const equal_exprt comparison{
9089
ptr1,
91-
typecast_exprt{typecast_exprt{address1, ptr_short_type},
92-
ptr_long_type}};
90+
typecast_exprt{typecast_exprt{address1, ptr_short_type}, ptr_type}};
9391
const renamedt<exprt, L2> renamed_comparison =
9492
state.rename(comparison, ns);
9593
auto result = try_evaluate_pointer_comparisons(

0 commit comments

Comments
 (0)