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

ruby-changes:31011

From: shugo <ko1@a...>
Date: Mon, 30 Sep 2013 10:49:27 +0900 (JST)
Subject: [ruby-changes:31011] shugo:r43090 (trunk): * vm_method.c (rb_undef): raise a NameError if the original method

shugo	2013-09-30 10:49:21 +0900 (Mon, 30 Sep 2013)

  New Revision: 43090

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

  Log:
    * vm_method.c (rb_undef): raise a NameError if the original method
      of a refined method is not defined.
    
    * vm_insnhelper.c (rb_method_entry_eq): added NULL check to avoid SEGV.
    
    * test/ruby/test_refinement.rb: related test.

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_refinement.rb
    trunk/vm_method.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43089)
+++ ChangeLog	(revision 43090)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Sep 30 10:40:20 2013  Shugo Maeda  <shugo@r...>
+
+	* vm_method.c (rb_undef): raise a NameError if the original method
+	  of a refined method is not defined. 
+
+	* vm_insnhelper.c (rb_method_entry_eq): added NULL check to avoid SEGV.
+
+	* test/ruby/test_refinement.rb: related test.
+
 Sun Sep 29 23:45:42 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* parse.y (rb_id_attrset, intern_str): allow junk attrset ID for
Index: vm_method.c
===================================================================
--- vm_method.c	(revision 43089)
+++ vm_method.c	(revision 43090)
@@ -868,7 +868,9 @@ rb_undef(VALUE klass, ID id) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L868
 
     me = search_method(klass, id, 0);
 
-    if (UNDEFINED_METHOD_ENTRY_P(me)) {
+    if (UNDEFINED_METHOD_ENTRY_P(me) ||
+	(me->def->type == VM_METHOD_TYPE_REFINED &&
+	 UNDEFINED_METHOD_ENTRY_P(me->def->body.orig_me))) {
 	const char *s0 = " class";
 	VALUE c = klass;
 
@@ -1118,9 +1120,9 @@ rb_method_entry_eq(const rb_method_entry https://github.com/ruby/ruby/blob/trunk/vm_method.c#L1120
 static int
 rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2)
 {
-    if (d1 && d1->type == VM_METHOD_TYPE_REFINED)
+    if (d1 && d1->type == VM_METHOD_TYPE_REFINED && d1->body.orig_me)
 	d1 = d1->body.orig_me->def;
-    if (d2 && d2->type == VM_METHOD_TYPE_REFINED)
+    if (d2 && d2->type == VM_METHOD_TYPE_REFINED && d2->body.orig_me)
 	d2 = d2->body.orig_me->def;
     if (d1 == d2) return 1;
     if (!d1 || !d2) return 0;
Index: test/ruby/test_refinement.rb
===================================================================
--- test/ruby/test_refinement.rb	(revision 43089)
+++ test/ruby/test_refinement.rb	(revision 43090)
@@ -1016,6 +1016,50 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L1016
     assert_not_send([FooSub, :method_defined?, :z])
   end
 
+  def test_undef_refined_method
+    bug8966 = '[ruby-core:57466] [Bug #8966]'
+
+    assert_in_out_err([], <<-INPUT, ["NameError"], [], bug8966)
+      module Foo
+        refine Object do
+          def foo
+            puts "foo"
+          end
+        end
+      end
+
+      using Foo
+
+      class Object
+        begin
+          undef foo
+        rescue Exception => e
+          p e.class
+        end
+      end
+    INPUT
+
+    assert_in_out_err([], <<-INPUT, ["NameError"], [], bug8966)
+      module Foo
+        refine Object do
+          def foo
+            puts "foo"
+          end
+        end
+      end
+
+      # without `using Foo'
+
+      class Object
+        begin
+          undef foo
+        rescue Exception => e
+          p e.class
+        end
+      end
+    INPUT
+  end
+
   private
 
   def eval_using(mod, s)

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

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