@@ -830,10 +830,41 @@ void goto_convertt::do_java_new_array(
830830 deref,
831831 struct_type.components ()[2 ].get_name (),
832832 struct_type.components ()[2 ].type ());
833- side_effect_exprt data_cpp_new_expr (ID_cpp_new_array, data.type ());
833+
834+ // Allocate a (struct realtype**) instead of a (void**) if possible.
835+ const irept &given_element_type=object_type.find (ID_C_element_type);
836+ typet allocate_data_type;
837+ exprt cast_data_member;
838+ if (given_element_type.is_not_nil ())
839+ {
840+ allocate_data_type=
841+ pointer_typet (static_cast <const typet &>(given_element_type));
842+ }
843+ else
844+ allocate_data_type=data.type ();
845+
846+ side_effect_exprt data_cpp_new_expr (ID_cpp_new_array, allocate_data_type);
834847 data_cpp_new_expr.set (ID_size, rhs.op0 ());
848+
849+ // Must directly assign the new array to a temporary
850+ // because goto-symex will notice `x=side_effect_exprt` but not
851+ // `x=typecast_exprt(side_effect_exprt(...))`
852+ symbol_exprt new_array_data_symbol=
853+ new_tmp_symbol (
854+ data_cpp_new_expr.type (),
855+ " new_array_data" ,
856+ dest,
857+ location)
858+ .symbol_expr ();
859+ goto_programt::targett t_p2=dest.add_instruction (ASSIGN);
860+ t_p2->code =code_assignt (new_array_data_symbol, data_cpp_new_expr);
861+ t_p2->source_location =location;
862+
835863 goto_programt::targett t_p=dest.add_instruction (ASSIGN);
836- t_p->code =code_assignt (data, data_cpp_new_expr);
864+ exprt cast_cpp_new=new_array_data_symbol;
865+ if (cast_cpp_new.type ()!=data.type ())
866+ cast_cpp_new=typecast_exprt (cast_cpp_new, data.type ());
867+ t_p->code =code_assignt (data, cast_cpp_new);
837868 t_p->source_location =location;
838869
839870 // zero-initialize the data
@@ -846,7 +877,7 @@ void goto_convertt::do_java_new_array(
846877 ns,
847878 get_message_handler ());
848879 codet array_set (ID_array_set);
849- array_set.copy_to_operands (data , zero_element);
880+ array_set.copy_to_operands (new_array_data_symbol , zero_element);
850881 goto_programt::targett t_d=dest.add_instruction (OTHER);
851882 t_d->code =array_set;
852883 t_d->source_location =location;
0 commit comments