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

ruby-changes:44821

From: nobu <ko1@a...>
Date: Thu, 24 Nov 2016 20:07:17 +0900 (JST)
Subject: [ruby-changes:44821] nobu:r56894 (trunk): marshal.c: fix infinite recursion

nobu	2016-11-24 20:07:12 +0900 (Thu, 24 Nov 2016)

  New Revision: 56894

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56894

  Log:
    marshal.c: fix infinite recursion
    
    * marshal.c (check_userdump_arg): marshal_dump should not return
      an instance of the same class, otherwise it causes infinite
      recursion.  [ruby-core:78289] [Bug #12974]

  Modified files:
    trunk/marshal.c
    trunk/test/ruby/test_marshal.rb
Index: marshal.c
===================================================================
--- marshal.c	(revision 56893)
+++ marshal.c	(revision 56894)
@@ -178,8 +178,22 @@ check_dump_arg(VALUE ret, struct dump_ar https://github.com/ruby/ruby/blob/trunk/marshal.c#L178
     }
     return ret;
 }
+
+static VALUE
+check_userdump_arg(VALUE obj, ID sym, int argc, const VALUE *argv,
+		   struct dump_arg *arg, const char *name)
+{
+    VALUE ret = rb_funcallv(obj, sym, argc, argv);
+    VALUE klass = CLASS_OF(obj);
+    if (CLASS_OF(ret) == klass) {
+        rb_raise(rb_eRuntimeError, "%"PRIsVALUE"#%s returned same class instance",
+		 klass, name);
+    }
+    return check_dump_arg(ret, arg, name);
+}
+
 #define dump_funcall(arg, obj, sym, argc, argv) \
-    check_dump_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym)
+    check_userdump_arg(obj, sym, argc, argv, arg, name_##sym)
 #define dump_check_funcall(arg, obj, sym, argc, argv) \
     check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym)
 
Index: test/ruby/test_marshal.rb
===================================================================
--- test/ruby/test_marshal.rb	(revision 56893)
+++ test/ruby/test_marshal.rb	(revision 56894)
@@ -757,4 +757,16 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_marshal.rb#L757
     end
     assert_equal(obj, Marshal.load(dump))
   end
+
+  class Bug12974
+    def marshal_dump
+      dup
+    end
+  end
+
+  def test_marshal_dump_recursion
+    assert_raise_with_message(RuntimeError, /same class instance/) do
+      Marshal.dump(Bug12974.new)
+    end
+  end
 end

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

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