diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-31 10:58:49 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-31 10:58:49 +0000 |
commit | c7dfe6077fbc61ee396e67f89bf566298dbdc3e7 (patch) | |
tree | e634475097d7666e527ddd6cc892de279fc6393b | |
parent | 6d7cab3bd6db434f8299c1ef52aa62a92129f41b (diff) | |
download | ruby-c7dfe6077fbc61ee396e67f89bf566298dbdc3e7.tar.gz |
merge revision(s) 66756: [Backport #15479]
Mark array as "going to be modified" in `Array#reject!`
Before this patch, if `reject!` is called on a shared array it can
mutate the shared array rather than a copy. This patch marks the array
as "going to be modified" so that the shared source array isn't
mutated.
[Bug #15479] [ruby-core:90781]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@66966 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | array.c | 1 | ||||
-rw-r--r-- | test/ruby/test_array.rb | 59 | ||||
-rw-r--r-- | version.h | 2 |
3 files changed, 61 insertions, 1 deletions
@@ -3218,6 +3218,7 @@ static VALUE rb_ary_reject_bang(VALUE ary) { RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length); + rb_ary_modify(ary); return ary_reject_bang(ary); } diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 2297ab01bc..61b59e74f2 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1250,6 +1250,65 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[7, 8, 9, 10], a, bug2545) end + def test_shared_array_reject! + c = [] + b = [1, 2, 3, 4] + 3.times do + a = b.dup + c << a.dup + + begin + a.reject! do |x| + case x + when 2 then true + when 3 then raise StandardError, 'Oops' + else false + end + end + rescue StandardError + end + + c << a.dup + end + + bug90781 = '[ruby-core:90781]' + assert_equal [[1, 2, 3, 4], + [1, 3, 4], + [1, 2, 3, 4], + [1, 3, 4], + [1, 2, 3, 4], + [1, 3, 4]], c, bug90781 + end + + def test_iseq_shared_array_reject! + c = [] + 3.times do + a = [1, 2, 3, 4] + c << a.dup + + begin + a.reject! do |x| + case x + when 2 then true + when 3 then raise StandardError, 'Oops' + else false + end + end + rescue StandardError + end + + c << a.dup + end + + bug90781 = '[ruby-core:90781]' + assert_equal [[1, 2, 3, 4], + [1, 3, 4], + [1, 2, 3, 4], + [1, 3, 4], + [1, 2, 3, 4], + [1, 3, 4]], c, bug90781 + end + def test_replace a = @cls[ 1, 2, 3] a_id = a.__id__ @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.4.6" #define RUBY_RELEASE_DATE "2019-01-31" -#define RUBY_PATCHLEVEL 341 +#define RUBY_PATCHLEVEL 342 #define RUBY_RELEASE_YEAR 2019 #define RUBY_RELEASE_MONTH 1 |