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

ruby-changes:26951

From: nobu <ko1@a...>
Date: Fri, 1 Feb 2013 16:40:30 +0900 (JST)
Subject: [ruby-changes:26951] nobu:r39003 (trunk): marshal.c: prohibit_ivar

nobu	2013-02-01 16:35:37 +0900 (Fri, 01 Feb 2013)

  New Revision: 39003

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

  Log:
    marshal.c: prohibit_ivar
    
    * marshal.c (r_object0): prohibit setting instance variables of
      exising class/module.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39002)
+++ ChangeLog	(revision 39003)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Feb  1 16:35:34 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* marshal.c (r_object0): prohibit setting instance variables of
+	  exising class/module.
+
 Fri Feb  1 14:34:29 2013  Shugo Maeda  <shugo@r...>
 
 	* ext/readline/extconf.rb, ext/readline/readline.c: check
Index: marshal.c
===================================================================
--- marshal.c	(revision 39002)
+++ marshal.c	(revision 39003)
@@ -1434,6 +1434,13 @@ append_extmod(VALUE obj, VALUE extmod) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1434
     return obj;
 }
 
+#define prohibit_ivar(type, str) do { \
+	if (!ivp || !*ivp) break; \
+	rb_raise(rb_eTypeError, \
+		 "can't override instance variable of "type" `%"PRIsVALUE"'", \
+		 (str)); \
+    } while (0)
+
 static VALUE
 r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
 {
@@ -1802,6 +1809,7 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1809
 	    VALUE str = r_bytes(arg);
 
 	    v = rb_path_to_class(str);
+	    prohibit_ivar("class/module", str);
 	    v = r_entry(v, arg);
             v = r_leave(v, arg);
 	}
@@ -1812,6 +1820,7 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1820
 	    VALUE str = r_bytes(arg);
 
 	    v = path2class(str);
+	    prohibit_ivar("class", str);
 	    v = r_entry(v, arg);
             v = r_leave(v, arg);
 	}
@@ -1822,6 +1831,7 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1831
 	    VALUE str = r_bytes(arg);
 
 	    v = path2module(str);
+	    prohibit_ivar("module", str);
 	    v = r_entry(v, arg);
             v = r_leave(v, arg);
 	}
Index: test/ruby/test_marshal.rb
===================================================================
--- test/ruby/test_marshal.rb	(revision 39002)
+++ test/ruby/test_marshal.rb	(revision 39003)
@@ -62,8 +62,8 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_marshal.rb#L62
 
   def test_struct_invalid_members
     TestMarshal.const_set :StructInvalidMembers, Struct.new(:a)
-    Marshal.load("\004\bIc&TestMarshal::StructInvalidMembers\006:\020__members__\"\bfoo")
     assert_raise(TypeError, "[ruby-dev:31759]") {
+      Marshal.load("\004\bIc&TestMarshal::StructInvalidMembers\006:\020__members__\"\bfoo")
       TestMarshal::StructInvalidMembers.members
     }
   end
@@ -538,4 +538,16 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_marshal.rb#L538
     assert_equal(obj, loaded, bug7627)
     assert_nil(loaded.foo, bug7627)
   end
+
+  def test_class_ivar
+    assert_raise(TypeError) {Marshal.load("\x04\x08Ic\x1bTestMarshal::TestClass\x06:\x0e@ivar_bug\"\x08bug")}
+    assert_raise(TypeError) {Marshal.load("\x04\x08IM\x1bTestMarshal::TestClass\x06:\x0e@ivar_bug\"\x08bug")}
+    assert_not_operator(TestClass, :instance_variable_defined?, :@bug)
+  end
+
+  def test_module_ivar
+    assert_raise(TypeError) {Marshal.load("\x04\x08Im\x1cTestMarshal::TestModule\x06:\x0e@ivar_bug\"\x08bug")}
+    assert_raise(TypeError) {Marshal.load("\x04\x08IM\x1cTestMarshal::TestModule\x06:\x0e@ivar_bug\"\x08bug")}
+    assert_not_operator(TestModule, :instance_variable_defined?, :@bug)
+  end
 end

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

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