1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Fix memory corruption in Enumerable#reverse_each [ruby-dev:50867] [Bug #16354]

This commit is contained in:
Kazuki Tsujimoto 2019-11-19 09:35:47 -06:00
parent 61131edba7
commit ff41663403
No known key found for this signature in database
GPG key ID: BCEA306C49B81CD7
2 changed files with 22 additions and 3 deletions

12
enum.c
View file

@ -2419,14 +2419,20 @@ static VALUE
enum_reverse_each(int argc, VALUE *argv, VALUE obj)
{
VALUE ary;
long i;
long len;
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
ary = enum_to_a(argc, argv, obj);
for (i = RARRAY_LEN(ary); --i >= 0; ) {
rb_yield(RARRAY_AREF(ary, i));
len = RARRAY_LEN(ary);
while (len--) {
long nlen;
rb_yield(RARRAY_AREF(ary, len));
nlen = RARRAY_LEN(ary);
if (nlen < len) {
len = nlen;
}
}
return obj;

View file

@ -735,6 +735,19 @@ class TestEnumerable < Test::Unit::TestCase
assert_equal([2,1,3,2,1], @obj.reverse_each.to_a)
end
def test_reverse_each_memory_corruption
bug16354 = '[ruby-dev:50867]'
assert_normal_exit %q{
size = 1000
(0...size).reverse_each do |i|
i.inspect
ObjectSpace.each_object(Array) do |a|
a.clear if a.length == size
end
end
}, bug16354
end
def test_chunk
e = [].chunk {|elt| true }
assert_equal([], e.to_a)