[前][次][番号順一覧][スレッド一覧]

ruby-changes:28507

From: nobu <ko1@a...>
Date: Thu, 2 May 2013 17:32:58 +0900 (JST)
Subject: [ruby-changes:28507] nobu:r40559 (trunk): marshal.c: no overwriting ivars

nobu	2013-05-02 17:32:48 +0900 (Thu, 02 May 2013)

  New Revision: 40559

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40559

  Log:
    marshal.c: no overwriting ivars
    
    * marshal.c (copy_ivar_i): get rid of overwriting already copied
      instance variales.  c.f. [Bug #8276]

  Modified files:
    trunk/ChangeLog
    trunk/marshal.c
    trunk/test/ruby/test_marshal.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40558)
+++ ChangeLog	(revision 40559)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu May  2 17:32:45 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* marshal.c (copy_ivar_i): get rid of overwriting already copied
+	  instance variales.  c.f. [Bug #8276]
+
 Thu May  2 16:55:43 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* thread.c (id_locals): use cached ID.
Index: marshal.c
===================================================================
--- marshal.c	(revision 40558)
+++ marshal.c	(revision 40559)
@@ -1373,7 +1373,11 @@ r_leave(VALUE v, struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1373
 static int
 copy_ivar_i(st_data_t key, st_data_t val, st_data_t arg)
 {
-    rb_ivar_set((VALUE)arg, (ID)key, (VALUE)val);
+    VALUE obj = (VALUE)arg, value = (VALUE)val;
+    ID vid = (ID)key;
+
+    if (!rb_ivar_defined(obj, vid))
+	rb_ivar_set(obj, vid, value);
     return ST_CONTINUE;
 }
 
Index: test/ruby/test_marshal.rb
===================================================================
--- test/ruby/test_marshal.rb	(revision 40558)
+++ test/ruby/test_marshal.rb	(revision 40559)
@@ -555,6 +555,16 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_marshal.rb#L555
     alias marshal_load initialize
   end
 
+  class FrozenData < LoadData
+    def marshal_load(data)
+      super
+      data.instance_variables.each do |iv|
+        instance_variable_set(iv, data.instance_variable_get(iv))
+      end
+      freeze
+    end
+  end
+
   def test_marshal_dump_excess_encoding
     bug8276 = '[ruby-core:54334] [Bug #8276]'
     t = Bug8276.new(bug8276)
@@ -574,18 +584,20 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_marshal.rb#L584
   def test_marshal_load_ivar
     s = "data with ivar"
     s.instance_variable_set(:@t, 42)
-    t = LoadData.new(s)
-    s = Marshal.dump(t)
     hook = ->(v) {
       if LoadData === v
-        assert_send([v, :instance_variable_defined?, :@t])
-        assert_equal(42, v.instance_variable_get(:@t))
+        assert_send([v, :instance_variable_defined?, :@t], v.class.name)
+        assert_equal(42, v.instance_variable_get(:@t), v.class.name)
       end
       v
     }
-    v = Marshal.load(s, hook)
-    assert_send([v, :instance_variable_defined?, :@t])
-    assert_equal(42, v.instance_variable_get(:@t))
+    [LoadData, FrozenData].each do |klass|
+      t = klass.new(s)
+      d = Marshal.dump(t)
+      v = assert_nothing_raised(RuntimeError) {break Marshal.load(d, hook)}
+      assert_send([v, :instance_variable_defined?, :@t], klass.name)
+      assert_equal(42, v.instance_variable_get(:@t), klass.name)
+    end
   end
 
   def test_class_ivar

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]