Prohibit Class#allocate in a different way

The method introduced by #5979 causes a fault by swapping classes.

```console
% bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
zsh: segmentation fault (core dumped)  bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
```

After applying this patch, a `TypeError` exception will be raised.

```console
% bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
trace (most recent call last):
        [1] -e:1
-e:1:in method: allocation failure of Proc (TypeError)
```

However, if the `mrb_vtype` is the same object, the same care must still be taken as before.

```console
% bin/mruby -e 'Method = Binding; p method(:puts).eval("12345")'
trace (most recent call last):
        [1] -e:1
-e:1:in eval: wrong argument type nil (expected Proc) (TypeError)
```
This commit is contained in:
dearblue
2023-12-22 21:59:34 +09:00
parent fa75865a33
commit 8ecfacefca
11 changed files with 28 additions and 17 deletions
+4 -2
View File
@@ -2227,7 +2227,8 @@ mrb_init_numeric(mrb_state *mrb)
/* Integer Class */
mrb->integer_class = integer = mrb_define_class_id(mrb, MRB_SYM(Integer), numeric); /* 15.2.8 */
MRB_SET_INSTANCE_TT(integer, MRB_TT_UNDEF);
MRB_SET_INSTANCE_TT(integer, MRB_TT_INTEGER);
MRB_UNDEF_ALLOCATOR(integer);
mrb_undef_class_method_id(mrb, integer, MRB_SYM(new));
mrb_define_method_id(mrb, integer, MRB_OPSYM(pow), int_pow, MRB_ARGS_REQ(1));
mrb_define_method_id(mrb, integer, MRB_OPSYM(cmp), num_cmp, MRB_ARGS_REQ(1)); /* 15.2.8.3.1 */
@@ -2272,7 +2273,8 @@ mrb_init_numeric(mrb_state *mrb)
#ifndef MRB_NO_FLOAT
/* Float Class */
mrb->float_class = fl = mrb_define_class_id(mrb, MRB_SYM(Float), numeric); /* 15.2.9 */
MRB_SET_INSTANCE_TT(fl, MRB_TT_UNDEF);
MRB_SET_INSTANCE_TT(fl, MRB_TT_FLOAT);
MRB_UNDEF_ALLOCATOR(fl);
mrb_undef_class_method(mrb, fl, "new");
mrb_define_method_id(mrb, fl, MRB_OPSYM(pow), flo_pow, MRB_ARGS_REQ(1));
mrb_define_method_id(mrb, fl, MRB_OPSYM(div), flo_div, MRB_ARGS_REQ(1)); /* 15.2.9.3.6 */