Skip to content

Commit adceda1

Browse files
author
Daniel Kroening
committed
fix exprt::opX accesses in goto-symex
This improves type safety.
1 parent 9643ec2 commit adceda1

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/goto-symex/expr_skeleton.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ expr_skeletont expr_skeletont::remove_op0(exprt e)
2121
{
2222
PRECONDITION(e.id() != ID_symbol);
2323
PRECONDITION(e.operands().size() >= 1);
24-
e.op0().make_nil();
24+
to_multi_ary_expr(e).op0().make_nil();
2525
return expr_skeletont{std::move(e)};
2626
}
2727

@@ -39,7 +39,7 @@ exprt expr_skeletont::apply(exprt expr) const
3939
INVARIANT(
4040
p->operands().size() >= 1,
4141
"expected pointed-to expression to have at least one operand");
42-
p = &p->op0();
42+
p = &to_multi_ary_expr(*p).op0();
4343
}
4444

4545
INVARIANT(p->is_nil(), "expected pointed-to expression to be nil");

src/goto-symex/symex_builtin_functions.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void goto_symext::symex_allocate(
5858

5959
dynamic_counter++;
6060

61-
exprt size=code.op0();
61+
exprt size = to_binary_expr(code).op0();
6262
optionalt<typet> object_type;
6363
auto function_symbol = outer_symbol_table.lookup(state.source.function_id);
6464
INVARIANT(function_symbol, "function associated with allocation not found");
@@ -93,16 +93,19 @@ void goto_symext::symex_allocate(
9393
else if(
9494
!alloc_size.has_value() && tmp_size.id() == ID_mult &&
9595
tmp_size.operands().size() == 2 &&
96-
(tmp_size.op0().is_constant() || tmp_size.op1().is_constant()))
96+
(to_mult_expr(tmp_size).op0().is_constant() ||
97+
to_mult_expr(tmp_size).op1().is_constant()))
9798
{
98-
exprt s=tmp_size.op0();
99+
exprt s = to_mult_expr(tmp_size).op0();
99100
if(s.is_constant())
100101
{
101-
s=tmp_size.op1();
102-
PRECONDITION(*c_sizeof_type_rec(tmp_size.op0()) == *tmp_type);
102+
s = to_mult_expr(tmp_size).op1();
103+
PRECONDITION(
104+
*c_sizeof_type_rec(to_mult_expr(tmp_size).op0()) == *tmp_type);
103105
}
104106
else
105-
PRECONDITION(*c_sizeof_type_rec(tmp_size.op1()) == *tmp_type);
107+
PRECONDITION(
108+
*c_sizeof_type_rec(to_mult_expr(tmp_size).op1()) == *tmp_type);
106109

107110
object_type = array_typet(*tmp_type, s);
108111
}
@@ -167,7 +170,7 @@ void goto_symext::symex_allocate(
167170
state.symbol_table.add(value_symbol);
168171

169172
// to allow constant propagation
170-
exprt zero_init = state.rename(code.op1(), ns).get();
173+
exprt zero_init = state.rename(to_binary_expr(code).op1(), ns).get();
171174
simplify(zero_init, ns);
172175

173176
INVARIANT(
@@ -258,7 +261,7 @@ void goto_symext::symex_va_start(
258261

259262
// create an array holding pointers to the parameters, starting after the
260263
// parameter that the operand points to
261-
const exprt &op = skip_typecast(code.op0());
264+
const exprt &op = skip_typecast(to_unary_expr(code).op());
262265
// this must be the address of a symbol
263266
const irep_idt start_parameter =
264267
to_ssa_expr(to_address_of_expr(op).object()).get_object_name();

src/goto-symex/symex_goto.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ void goto_symext::apply_goto_condition(
6060
// Could use not_exprt + simplify, but let's avoid paying that cost on quite
6161
// a hot path:
6262
if(new_guard.id() == ID_not)
63-
jump_not_taken_state.apply_condition(new_guard.op0(), current_state, ns);
63+
jump_not_taken_state.apply_condition(
64+
to_not_expr(new_guard).op(), current_state, ns);
6465
else if(new_guard.id() == ID_notequal)
6566
{
67+
auto &not_equal_expr = to_notequal_expr(new_guard);
6668
jump_not_taken_state.apply_condition(
67-
equal_exprt(new_guard.op0(), new_guard.op1()), current_state, ns);
69+
equal_exprt(not_equal_expr.lhs(), not_equal_expr.rhs()),
70+
current_state,
71+
ns);
6872
}
6973
}
7074

@@ -180,10 +184,10 @@ static optionalt<renamedt<exprt, L2>> try_evaluate_pointer_comparison(
180184
if(expr.id() != ID_equal && expr.id() != ID_notequal)
181185
return {};
182186

183-
if(!can_cast_type<pointer_typet>(expr.op0().type()))
187+
if(!can_cast_type<pointer_typet>(to_binary_expr(expr).op0().type()))
184188
return {};
185189

186-
exprt lhs = expr.op0(), rhs = expr.op1();
190+
exprt lhs = to_binary_expr(expr).op0(), rhs = to_binary_expr(expr).op1();
187191
if(can_cast_expr<symbol_exprt>(rhs))
188192
std::swap(lhs, rhs);
189193

0 commit comments

Comments
 (0)