Issue #5274 has been reported by Kenta Murata.

----------------------------------------
Bug #5274: [ruby-dev:44460] Class ??? clone ??? Class ??????????腟???水?????????????????????
http://redmine.ruby-lang.org/issues/5274

Author: Kenta Murata
Status: Assigned
Priority: High
Assignee: Yukihiro Matsumoto
Category: core
Target version: 1.9.3
ruby -v: ruby 1.9.3dev (2011-09-04 revision 33179)


[ruby-dev:44460] ????????宴?????????????障?????
----

??????潟????障?????

罨<??泣?潟?????潟?若?????絎?茵?????????? segv ???????????障?????
腆肴???????? Ruby ????膤祉?? ruby 1.9.3dev (2011-09-04 revision 33179)
[i686-linux] ??с?????

#??泣?潟?????潟?若??(#
StrClone = String.clone
Class.new(StrClone)
Marshal.dump(StrClone.new("a"))



segv ??????????????????StrClone.new("a") ??у??????????????吾?с???(??ゃ?潟?鴻?帥?潟?劫??
??違??違???????????????????c?????????吾?с???)???????????????????????????с?????
Class.new(StrClone) ???絎?茵????????????鴻?? StrClone ???合?違????(篁ラ??
<StrClone> ???吾????障??)???紊???眼??????????<?純?????????膣∝????????紊?????c?????障?c???
??障??????????????????StrClone ??ゃ?潟?鴻?帥?潟?鴻??≪??宴?若?帥?? T_STRING ????????
??????T_OBJECT ?????????????StrClone.new ??? T_OBJECT ???????吾?с????????蚊??
綵??????????障?????

????????????StrClone ???<?純??????????若?????????眼????????????????????????蚊??綵????????
?????吾?с?????絲????????絖???????ゃ????激?c??ゃ?吟????若?喝?冴???????障?????????????????с??
T_OBJECT ???????吾?с?????絲???????絖?????????????????????莎違?c?????障?????紕???????
??????吾?с??????篏?????????障???????泣?潟?????潟?若????с??????????紕?????????????吾?с??????
Marshal.dump ??у?????????????????????????ゃ?潟?鴻?帥?潟?劫????違?????潟??????????????????
????segv ???????????障?????

class ??????吾?с?????? clone ????????合?違???鴻???????????????х??茹c???????????
?????障???????????篁???????翫?????StrClone ???合?違???鴻??紊???眼???????????障?????????
?????????障?????StrClone ???合?違?<?純????????紊???眼???????????障?????????????????篁ヤ??
???????????make_metaclass(class.c) ?????????障?????



#define ENSURE_EIGENCLASS(klass) \
 (rb_ivar_get(METACLASS_OF(klass), id_attached) == (klass) ?
METACLASS_OF(klass) : make_metaclass(klass))

static inline VALUE
make_metaclass(VALUE klass)
{
   ....

   super = RCLASS_SUPER(klass);
   while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
   RCLASS_SUPER(metaclass) = super ? ENSURE_EIGENCLASS(super) : rb_cClass;

   ....

   return metaclass;
}



????????? ENSURE_EIGENCLASS ????????????????klass ??? StrClone ?????c????翫?????
rb_ivar_get(METACLASS_OF(StrClone), id_attached) == (StrClone)
???ゅ???????純???????make_metaclass(StrClone) ???絎?茵?????????障?????
?????? make_metaclass(StrClone) ??с??StrClone ???合?違???鴻??紊???眼???????障?????

?????????篏????莎激???????????????????<StrClone> ??? "__attached__" ?????????ゃ?潟?鴻??
??潟?劫????違?????StrClone ?????????????????????????с?????<StrClone> ???
"__attached__" ????rb_singleton_class_clone(class.c)??篁ヤ?????????????
??????????荳???????????????障????????????????絨??????????????<StrClone> ???
"__attached__" ??? StrClone ????????????????????????違??StrClone ???合?違???鴻??紊?
??眼???????????障???????????鴻??ゃ?????茹f浦?с????障?????



VALUE
rb_singleton_class_clone(VALUE obj)
{
   VALUE klass = RBASIC(obj)->klass;

   ...

       VALUE clone = class_alloc((RBASIC(klass)->flags & ~(FL_MARK)), 0);

       if (BUILTIN_TYPE(obj) == T_CLASS) {
           RBASIC(clone)->klass = (VALUE)clone; /* <= ??潟?潟????????(1) */
       }
       else {
           RBASIC(clone)->klass = rb_singleton_class_clone(klass);
       }

       rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
       /* (1) ????????c????翫?????RBASIC(clone)->klass = clone */
       /* clone ??? "__attached__" ??? clone ??ェ霄ォ????????? */

   ...
}



???<?若???絨????rb_singleton_class_clone ????? "__attached__" ?????
??絲??????????????????羞私???????障???????合?違???鴻??絽吾?? "__attached__" ????????????????
?????????????綽?荀??????????????????class.c ??絲??????????????????∴????????????????????

??合?違???鴻?? "__attached__" ?????????荳????????????????????障???????宴?若?鴻????????
?????????????????????????????с???????????????????????鴻?????????????????????障????????????????
??医????с?泣?潟?????潟?若???????>??絎?茵???????test-all ????????????????????腆肴???с?????
?????????

????????????????????????????医晃?????с?????
??????????????蕁???????????????障?????



# ????????? #
Index: object.c
===================================================================
--- object.c    (revision 33182)
+++ object.c    (working copy)
@@ -272,12 +272,17 @@
 rb_obj_clone(VALUE obj)
 {
    VALUE clone;
+    VALUE singleton;

    if (rb_special_const_p(obj)) {
        rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
    }
    clone = rb_obj_alloc(rb_obj_class(obj));
-    RBASIC(clone)->klass = rb_singleton_class_clone(obj);
+    singleton = rb_singleton_class_clone(obj);
+    RBASIC(clone)->klass = singleton;
+    if (FL_TEST(singleton, FL_SINGLETON)) {
+      rb_singleton_class_attached(singleton, clone);
+    }
    RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone,
FL_TAINT) | FL_TEST(clone, FL_UNTRUSTED)) &
~(FL_FREEZE|FL_FINALIZE|FL_MARK);
    init_copy(clone, obj);
    rb_funcall(clone, id_init_clone, 1, obj);
Index: class.c
===================================================================
--- class.c     (revision 33182)
+++ class.c     (working copy)
@@ -230,12 +230,15 @@
       struct clone_method_data data;
       /* copy singleton(unnamed) class */
       VALUE clone = class_alloc((RBASIC(klass)->flags & ~(FL_MARK)), 0);
+       int attach;

       if (BUILTIN_TYPE(obj) == T_CLASS) {
           RBASIC(clone)->klass = (VALUE)clone;
+           attach = 0;
       }
       else {
           RBASIC(clone)->klass = rb_singleton_class_clone(klass);
+           attach = 1;
       }

       RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
@@ -251,7 +254,9 @@
       data.klass = (VALUE)clone;
       st_foreach(RCLASS_M_TBL(klass), clone_method,
                  (st_data_t)&data);
-       rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
+       if (attach) {
+         rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
+       }
       FL_SET(clone, FL_SINGLETON);
       return (VALUE)clone;
    }



-- 
http://redmine.ruby-lang.org