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

ruby-changes:29194

From: nobu <ko1@a...>
Date: Wed, 12 Jun 2013 12:03:59 +0900 (JST)
Subject: [ruby-changes:29194] nobu:r41246 (trunk): array.c: freeze in callback

nobu	2013-06-12 12:03:46 +0900 (Wed, 12 Jun 2013)

  New Revision: 41246

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

  Log:
    array.c: freeze in callback
    
    * array.c (rb_ary_sort_bang): must not be modified once frozen even in
      a callback method.

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/test/ruby/test_array.rb

Index: array.c
===================================================================
--- array.c	(revision 41245)
+++ array.c	(revision 41246)
@@ -2321,6 +2321,7 @@ rb_ary_sort_bang(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L2321
 	ruby_qsort(RARRAY_PTR(tmp), len, sizeof(VALUE),
 		   rb_block_given_p()?sort_1:sort_2, &data);
 
+	rb_ary_modify(ary);
         if (ARY_EMBED_P(tmp)) {
             assert(ARY_EMBED_P(tmp));
             if (ARY_SHARED_P(ary)) { /* ary might be destructively operated in the given block */
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41245)
+++ ChangeLog	(revision 41246)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jun 12 12:03:43 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* array.c (rb_ary_sort_bang): must not be modified once frozen even in
+	  a callback method.
+
 Wed Jun 12 12:00:15 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* array.c (FL_SET_EMBED): shared object is frozen even when get
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb	(revision 41245)
+++ test/ruby/test_array.rb	(revision 41246)
@@ -1378,6 +1378,22 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1378
     end
   end
 
+  def test_sort_bang_with_freeze
+    ary = []
+    o1 = Object.new
+    o1.singleton_class.class_eval {
+      define_method(:<=>) {|v|
+        ary.freeze
+        1
+      }
+    }
+    o2 = o1.dup
+    ary << o1 << o2
+    orig = ary.dup
+    assert_raise(RuntimeError, "frozen during comparison") {ary.sort!}
+    assert_equal(orig, ary, "must not be modified once frozen")
+  end
+
   def test_to_a
     a = @cls[ 1, 2, 3 ]
     a_id = a.__id__

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

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