Skip to content

Commit

Permalink
merge revision(s) 66756: [Backport #15479]
Browse files Browse the repository at this point in the history
	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_5@66784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nagachika committed Jan 10, 2019
1 parent 4f0899d commit 5b23824
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions array.c
Expand Up @@ -3241,6 +3241,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);
}

Expand Down
59 changes: 59 additions & 0 deletions test/ruby/test_array.rb
Expand Up @@ -1291,6 +1291,65 @@ def test_reject!
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__
Expand Down
2 changes: 1 addition & 1 deletion version.h
@@ -1,6 +1,6 @@
#define RUBY_VERSION "2.5.4"
#define RUBY_RELEASE_DATE "2019-01-10"
#define RUBY_PATCHLEVEL 127
#define RUBY_PATCHLEVEL 128

#define RUBY_RELEASE_YEAR 2019
#define RUBY_RELEASE_MONTH 1
Expand Down

0 comments on commit 5b23824

Please sign in to comment.