Skip to content

Commit

Permalink
Fix SEGV when splat object
Browse files Browse the repository at this point in the history
Splat operation should return an array.
And raise an error if result of convert by to_a is not array or nil.
  • Loading branch information
ksss committed Sep 8, 2016
1 parent cd5133c commit d265c03
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,15 +891,32 @@ mrb_ary_rindex_m(mrb_state *mrb, mrb_value self)
MRB_API mrb_value
mrb_ary_splat(mrb_state *mrb, mrb_value v)
{
mrb_value a, recv_class;

if (mrb_array_p(v)) {
return v;
}
if (mrb_respond_to(mrb, v, mrb_intern_lit(mrb, "to_a"))) {
return mrb_funcall(mrb, v, "to_a", 0);

if (!mrb_respond_to(mrb, v, mrb_intern_lit(mrb, "to_a"))) {
return mrb_ary_new_from_values(mrb, 1, &v);
}
else {

a = mrb_funcall(mrb, v, "to_a", 0);
if (mrb_array_p(a)) {
return a;
}
else if (mrb_nil_p(a)) {
return mrb_ary_new_from_values(mrb, 1, &v);
}
else {
recv_class = mrb_obj_value(mrb_obj_class(mrb, v));
mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S to Array (%S#to_a gives %S)",
recv_class,
recv_class,
mrb_obj_value(mrb_obj_class(mrb, a))
);
return mrb_undef_value();
}
}

static mrb_value
Expand Down

0 comments on commit d265c03

Please sign in to comment.