Skip to content

Commit 01255db

Browse files
author
Daniel Kroening
committed
avoid access to exprt::opX
This avoids out-of-bounds accesses to the operands() vector.
1 parent 95b1101 commit 01255db

File tree

6 files changed

+54
-54
lines changed

6 files changed

+54
-54
lines changed

src/goto-programs/adjust_float_expressions.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void adjust_float_expressions(exprt &expr, const exprt &rounding_mode)
118118
irep_idt());
119119

120120
expr.operands().resize(3);
121-
expr.op2()=rounding_mode;
121+
to_ieee_float_op_expr(expr).rounding_mode() = rounding_mode;
122122
}
123123
}
124124

@@ -138,7 +138,7 @@ void adjust_float_expressions(exprt &expr, const exprt &rounding_mode)
138138
// the representation.
139139
expr.id(ID_floatbv_typecast);
140140
expr.operands().resize(2);
141-
expr.op1()=rounding_mode;
141+
to_floatbv_typecast_expr(expr).rounding_mode() = rounding_mode;
142142
}
143143
else if(
144144
dest_type.id() == ID_floatbv &&
@@ -148,7 +148,7 @@ void adjust_float_expressions(exprt &expr, const exprt &rounding_mode)
148148
// casts from integer to float-type might round
149149
expr.id(ID_floatbv_typecast);
150150
expr.operands().resize(2);
151-
expr.op1()=rounding_mode;
151+
to_floatbv_typecast_expr(expr).rounding_mode() = rounding_mode;
152152
}
153153
else if(
154154
dest_type.id() == ID_floatbv &&
@@ -174,7 +174,7 @@ void adjust_float_expressions(exprt &expr, const exprt &rounding_mode)
174174
*/
175175
expr.id(ID_floatbv_typecast);
176176
expr.operands().resize(2);
177-
expr.op1()=
177+
to_floatbv_typecast_expr(expr).rounding_mode() =
178178
from_integer(ieee_floatt::ROUND_TO_ZERO, rounding_mode.type());
179179
}
180180
}

src/goto-programs/builtin_functions.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,7 @@ void goto_convertt::cpp_new_initializer(
567567
exprt goto_convertt::get_array_argument(const exprt &src)
568568
{
569569
if(src.id()==ID_typecast)
570-
{
571-
assert(src.operands().size()==1);
572-
return get_array_argument(src.op0());
573-
}
570+
return get_array_argument(to_typecast_expr(src).op());
574571

575572
if(src.id()!=ID_address_of)
576573
{
@@ -579,25 +576,25 @@ exprt goto_convertt::get_array_argument(const exprt &src)
579576
throw 0;
580577
}
581578

582-
assert(src.operands().size()==1);
579+
const auto &address_of_expr = to_address_of_expr(src);
583580

584-
if(src.op0().id()!=ID_index)
581+
if(address_of_expr.object().id() != ID_index)
585582
{
586583
error().source_location=src.find_source_location();
587584
error() << "expected array-element as argument" << eom;
588585
throw 0;
589586
}
590587

591-
assert(src.op0().operands().size()==2);
588+
const auto &index_expr = to_index_expr(address_of_expr.object());
592589

593-
if(src.op0().op0().type().id() != ID_array)
590+
if(index_expr.array().type().id() != ID_array)
594591
{
595592
error().source_location=src.find_source_location();
596593
error() << "expected array as argument" << eom;
597594
throw 0;
598595
}
599596

600-
return src.op0().op0();
597+
return index_expr.array();
601598
}
602599

603600
void goto_convertt::do_array_op(
@@ -633,10 +630,12 @@ exprt make_va_list(const exprt &expr)
633630
return make_va_list(to_typecast_expr(expr).op());
634631

635632
// if it's an address of an lvalue, we take that
636-
if(expr.id()==ID_address_of &&
637-
expr.operands().size()==1 &&
638-
is_lvalue(expr.op0()))
639-
return expr.op0();
633+
if(expr.id() == ID_address_of)
634+
{
635+
const auto &address_of_expr = to_address_of_expr(expr);
636+
if(is_lvalue(address_of_expr.object()))
637+
return address_of_expr.object();
638+
}
640639

641640
return expr;
642641
}

src/goto-programs/goto_clean_expr.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,20 @@ void goto_convertt::clean_expr(
363363
expr.operands().size() == 2,
364364
"side-effect assignment expressions must have two operands");
365365

366-
if(expr.op1().id()==ID_side_effect &&
367-
to_side_effect_expr(expr.op1()).get_statement()==ID_function_call)
366+
auto &side_effect_assign = to_side_effect_expr_assign(expr);
367+
368+
if(
369+
side_effect_assign.rhs().id() == ID_side_effect &&
370+
to_side_effect_expr(side_effect_assign.rhs()).get_statement() ==
371+
ID_function_call)
368372
{
369-
clean_expr(expr.op0(), dest, mode);
370-
exprt lhs=expr.op0();
373+
clean_expr(side_effect_assign.lhs(), dest, mode);
374+
exprt lhs = side_effect_assign.lhs();
371375

372376
// turn into code
373377
code_assignt assignment;
374378
assignment.lhs()=lhs;
375-
assignment.rhs()=expr.op1();
379+
assignment.rhs() = side_effect_assign.rhs();
376380
assignment.add_source_location()=expr.source_location();
377381
convert_assign(assignment, dest, mode);
378382

@@ -423,7 +427,7 @@ void goto_convertt::clean_expr(
423427
// This is simply replaced by the literal
424428
DATA_INVARIANT(
425429
expr.operands().size() == 1, "ID_compound_literal has a single operand");
426-
expr=expr.op0();
430+
expr = to_unary_expr(expr).op();
427431
}
428432
}
429433

@@ -439,8 +443,8 @@ void goto_convertt::clean_expr_address_of(
439443
{
440444
DATA_INVARIANT(
441445
expr.operands().size() == 1, "ID_compound_literal has a single operand");
442-
clean_expr(expr.op0(), dest, mode);
443-
expr = make_compound_literal(expr.op0(), dest, mode);
446+
clean_expr(to_unary_expr(expr).op(), dest, mode);
447+
expr = make_compound_literal(to_unary_expr(expr).op(), dest, mode);
444448
}
445449
else if(expr.id()==ID_string_constant)
446450
{

src/goto-programs/goto_convert_class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class goto_convertt:public messaget
119119
const irep_idt &mode,
120120
bool result_is_used);
121121
void remove_function_call(
122-
side_effect_exprt &expr,
122+
side_effect_expr_function_callt &expr,
123123
goto_programt &dest,
124124
const irep_idt &mode,
125125
bool result_is_used);

src/goto-programs/goto_convert_side_effect.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ void goto_convertt::remove_assignment(
4444

4545
if(statement==ID_assign)
4646
{
47-
exprt new_lhs = skip_typecast(expr.op0());
47+
auto &old_assignment = to_side_effect_expr_assign(expr);
48+
exprt new_lhs = skip_typecast(old_assignment.lhs());
4849
exprt new_rhs =
49-
typecast_exprt::conditional_cast(expr.op1(), new_lhs.type());
50-
code_assignt assign(std::move(new_lhs), std::move(new_rhs));
51-
assign.add_source_location() = expr.source_location();
50+
typecast_exprt::conditional_cast(old_assignment.rhs(), new_lhs.type());
51+
code_assignt new_assignment(std::move(new_lhs), std::move(new_rhs));
52+
new_assignment.add_source_location() = expr.source_location();
5253

53-
convert_assign(assign, dest, mode);
54+
convert_assign(new_assignment, dest, mode);
5455
}
5556
else if(statement==ID_assign_plus ||
5657
statement==ID_assign_minus ||
@@ -285,38 +286,28 @@ void goto_convertt::remove_post(
285286
}
286287

287288
void goto_convertt::remove_function_call(
288-
side_effect_exprt &expr,
289+
side_effect_expr_function_callt &expr,
289290
goto_programt &dest,
290291
const irep_idt &mode,
291292
bool result_is_used)
292293
{
293-
INVARIANT_WITH_DIAGNOSTICS(
294-
expr.operands().size() == 2,
295-
"function_call expects two operands",
296-
expr.find_source_location());
297-
298294
if(!result_is_used)
299295
{
300-
code_function_callt call(expr.op0(), expr.op1().operands());
296+
code_function_callt call(expr.function(), expr.arguments());
301297
call.add_source_location()=expr.source_location();
302298
convert_function_call(call, dest, mode);
303299
expr.make_nil();
304300
return;
305301
}
306302

307303
// get name of function, if available
308-
309-
INVARIANT_WITH_DIAGNOSTICS(
310-
expr.id() == ID_side_effect && expr.get(ID_statement) == ID_function_call,
311-
"expects function call",
312-
expr.find_source_location());
313-
314304
std::string new_base_name = "return_value";
315305
irep_idt new_symbol_mode = mode;
316306

317-
if(expr.op0().id()==ID_symbol)
307+
if(expr.function().id() == ID_symbol)
318308
{
319-
const irep_idt &identifier = to_symbol_expr(expr.op0()).get_identifier();
309+
const irep_idt &identifier =
310+
to_symbol_expr(expr.function()).get_identifier();
320311
const symbolt &symbol = ns.lookup(identifier);
321312

322313
new_base_name+='_';
@@ -341,7 +332,7 @@ void goto_convertt::remove_function_call(
341332
{
342333
goto_programt tmp_program2;
343334
code_function_callt call(
344-
new_symbol.symbol_expr(), expr.op0(), expr.op1().operands());
335+
new_symbol.symbol_expr(), expr.function(), expr.arguments());
345336
call.add_source_location()=new_symbol.location;
346337
convert_function_call(call, dest, mode);
347338
}
@@ -395,7 +386,7 @@ void goto_convertt::remove_cpp_delete(
395386

396387
codet tmp(expr.get_statement());
397388
tmp.add_source_location()=expr.source_location();
398-
tmp.copy_to_operands(expr.op0());
389+
tmp.copy_to_operands(to_unary_expr(expr).op());
399390
tmp.set(ID_destructor, expr.find(ID_destructor));
400391

401392
convert_cpp_delete(tmp, dest);
@@ -451,7 +442,8 @@ void goto_convertt::remove_temporary_object(
451442

452443
if(expr.operands().size()==1)
453444
{
454-
const code_assignt assignment(new_symbol.symbol_expr(), expr.op0());
445+
const code_assignt assignment(
446+
new_symbol.symbol_expr(), to_unary_expr(expr).op());
455447

456448
convert(assignment, dest, mode);
457449
}
@@ -559,7 +551,8 @@ void goto_convertt::remove_side_effect(
559551
const irep_idt &statement=expr.get_statement();
560552

561553
if(statement==ID_function_call)
562-
remove_function_call(expr, dest, mode, result_is_used);
554+
remove_function_call(
555+
to_side_effect_expr_function_call(expr), dest, mode, result_is_used);
563556
else if(statement==ID_assign ||
564557
statement==ID_assign_plus ||
565558
statement==ID_assign_minus ||

src/goto-programs/goto_inline_class.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ void goto_inlinet::replace_return(
165165
{
166166
if(it->is_return())
167167
{
168+
const auto &code_return = it->get_return();
169+
168170
if(lhs.is_not_nil())
169171
{
170-
if(it->code.operands().size()!=1)
172+
if(!code_return.has_return_value())
171173
{
172174
warning().source_location=it->code.find_source_location();
173175
warning() << "return expects one operand!\n"
@@ -178,14 +180,16 @@ void goto_inlinet::replace_return(
178180
// a typecast may be necessary if the declared return type at the call
179181
// site differs from the defined return type
180182
it->code = code_assignt(
181-
lhs, typecast_exprt::conditional_cast(it->code.op0(), lhs.type()));
183+
lhs,
184+
typecast_exprt::conditional_cast(
185+
code_return.return_value(), lhs.type()));
182186
it->type=ASSIGN;
183187

184188
it++;
185189
}
186-
else if(!it->code.operands().empty())
190+
else if(code_return.has_return_value())
187191
{
188-
it->code=code_expressiont(it->code.op0());
192+
it->code = code_expressiont(code_return.return_value());
189193
it->type=OTHER;
190194
it++;
191195
}

0 commit comments

Comments
 (0)