10000 Mark array as "going to be modified" in `Array#reject!` · github/ruby@79c150f · GitHub
[go: up one dir, main page]

Skip to content

Commit 79c150f

Browse files
committed
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/trunk@66756 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent e9dc6f6 commit 79c150f

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

array.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3540,6 +3540,7 @@ static VALUE
35403540
rb_ary_reject_bang(VALUE ary)
35413541
{
35423542
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
3543+
rb_ary_modify(ary);
35433544
return ary_reject_bang(ary);
35443545
}
35453546

test/ruby/test_array.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,65 @@ def test_reject!
13081308
assert_equal(@cls[7, 8, 9, 10], a, bug2545)
13091309
end
13101310

1311+
def test_shared_array_reject!
1312+
c = []
1313+
b = [1, 2, 3, 4]
1314+
3.times do
1315+
a = b.dup
1316+
c << a.dup
1317+
1318+
begin
1319+
a.reject! do |x|
1320+
case x
1321+
when 2 then true
1322+
when 3 then raise StandardError, 'Oops'
1323+
else false
1324+
end
1325+
end
1326+
rescue StandardError
1327+
end
1328+
1329+
c << a.dup
1330+
end
1331+
1332+
bug90781 = '[ruby-core:90781]'
1333+
assert_equal [[1, 2, 3, 4],
1334+
[1, 3, 4],
1335+
[1, 2, 3, 4],
1336+
[1, 3, 4],
1337+
[1, 2, 3, 4],
1338+
[1, 3, 4]], c, bug90781
1339+
end
1340+
1341+
def test_iseq_shared_array_reject!
1342+
c = []
1343+
3.times do
1344+
a = [1, 2, 3, 4]
1345+
c << a.dup
1346+
1347+
begin
1348+
a.reject! do |x|
1349+
case x
1350+
when 2 then true
1351+
when 3 then raise StandardError, 'Oops'
1352+
else false
1353+
end
1354+
end
1355+
rescue StandardError
1356+
end
1357+
1358+
c << a.dup
1359+
end
1360+
1361+
bug90781 = '[ruby-core:90781]'
1362+
assert_equal [[1, 2, 3, 4],
1363+
[1, 3, 4],
1364+
[1, 2, 3, 4],
1365+
[1, 3, 4],
1366+
[1, 2, 3, 4],
1367+
[1, 3, 4]], c, bug90781
1368+
end
1369+
13111370
def test_replace
13121371
a = @cls[ 1, 2, 3]
13131372
a_id = a.__id__

0 commit comments

Comments
 (0)
0