Need to check number of argument of `Struct#new`; fix
MODIFY
mrbgems/mruby-struct/src/struct.c
mrbgems/mruby-struct/src/struct.c
@@ -283,17 +283,21 @@ mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
name = mrb_nil_value();
mrb_get_args(mrb, "*&", &argv, &argc, &b);
if (argc == 0) { /* special case to avoid crash */
- rest = mrb_ary_new(mrb);
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments");
}
else {
- if (argc > 0) name = argv[0];
- pargv = &argv[1];
- argcnt = argc-1;
- if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
- /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
+ pargv = argv;
+ argcnt = argc;
+ if (argc > 0) {
+ name = argv[0];
+ if (mrb_symbol_p(name)) {
+ /* 1stArgument:symbol -> name=nil rest=argv[0..n] */
name = mrb_nil_value();
- pargv = &argv[0];
- argcnt++;
+ }
+ else {
+ pargv++;
+ argcnt--;
+ }
}
rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
for (i=0; i<RARRAY_LEN(rest); i++) {

MODIFY
mrbgems/mruby-struct/test/struct.rb
mrbgems/mruby-struct/test/struct.rb
@@ -11,13 +11,6 @@ assert('Struct.new', '15.2.18.3.1') do
assert_equal [:m1, :m2], c.members
end
-# Check crash bug with Struc.new and no params.
-assert('Struct.new', '15.2.18.3.1') do
- c = Struct.new()
- assert_equal Struct, c.superclass
- assert_equal [], c.members
-end
-
assert('Struct#==', '15.2.18.4.1') do
c = Struct.new(:m1, :m2)
cc1 = c.new(1,2)
@@ -192,7 +185,7 @@ assert("Struct.new generates subclass of Struct") do
begin
original_struct = Struct
Struct = String
- assert_equal original_struct, original_struct.new.superclass
+ assert_equal original_struct, original_struct.new(:foo).superclass
ensure
Struct = original_struct
end

Need to check number of argument of `Struct#new`; fix #3823