Skip to content

Commit

Permalink
Don't allow array parameter in Struct.new
Browse files Browse the repository at this point in the history
  • Loading branch information
clayton-shopify authored and bouk committed Nov 24, 2016
1 parent a630c4f commit b60d6c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
26 changes: 8 additions & 18 deletions mrbgems/mruby-struct/src/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,25 +273,15 @@ mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
}
else {
if (argc > 0) name = argv[0];
if (argc > 1) rest = argv[1];
if (mrb_array_p(rest)) {
if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
/* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
mrb_ary_unshift(mrb, rest, name);
name = mrb_nil_value();
}
}
else {
pargv = &argv[1];
argcnt = argc-1;
if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
/* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
name = mrb_nil_value();
pargv = &argv[0];
argcnt++;
}
rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
pargv = &argv[1];
argcnt = argc-1;
if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
/* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
name = mrb_nil_value();
pargv = &argv[0];
argcnt++;
}
rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
for (i=0; i<RARRAY_LEN(rest); i++) {
id = mrb_obj_to_sym(mrb, RARRAY_PTR(rest)[i]);
mrb_ary_set(mrb, rest, i, mrb_symbol_value(id));
Expand Down
6 changes: 6 additions & 0 deletions mrbgems/mruby-struct/test/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@
assert_equal 1, a.dig(:purple, :red)
assert_equal 1, a.dig(1, 0)
end

assert("Struct.new does not allow array") do
assert_raise(TypeError) do
Struct.new("Test", [:a])
end
end

0 comments on commit b60d6c4

Please sign in to comment.