array.c: freeze in callback

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41246 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-06-12 03:03:46 +00:00
parent ff7bb4723b
commit da786437a7
3 changed files with 22 additions and 0 deletions

View File

@ -1,3 +1,8 @@
Wed Jun 12 12:03:43 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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@ruby-lang.org>
* array.c (FL_SET_EMBED): shared object is frozen even when get

View File

@ -2321,6 +2321,7 @@ rb_ary_sort_bang(VALUE ary)
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 */

View File

@ -1378,6 +1378,22 @@ class TestArray < Test::Unit::TestCase
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__