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:
parent
61131edba7
commit
ff41663403
2 changed files with 22 additions and 3 deletions
12
enum.c
12
enum.c
|
@ -2419,14 +2419,20 @@ static VALUE
|
||||||
enum_reverse_each(int argc, VALUE *argv, VALUE obj)
|
enum_reverse_each(int argc, VALUE *argv, VALUE obj)
|
||||||
{
|
{
|
||||||
VALUE ary;
|
VALUE ary;
|
||||||
long i;
|
long len;
|
||||||
|
|
||||||
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
|
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
|
||||||
|
|
||||||
ary = enum_to_a(argc, argv, obj);
|
ary = enum_to_a(argc, argv, obj);
|
||||||
|
|
||||||
for (i = RARRAY_LEN(ary); --i >= 0; ) {
|
len = RARRAY_LEN(ary);
|
||||||
rb_yield(RARRAY_AREF(ary, i));
|
while (len--) {
|
||||||
|
long nlen;
|
||||||
|
rb_yield(RARRAY_AREF(ary, len));
|
||||||
|
nlen = RARRAY_LEN(ary);
|
||||||
|
if (nlen < len) {
|
||||||
|
len = nlen;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
|
|
|
@ -735,6 +735,19 @@ class TestEnumerable < Test::Unit::TestCase
|
||||||
assert_equal([2,1,3,2,1], @obj.reverse_each.to_a)
|
assert_equal([2,1,3,2,1], @obj.reverse_each.to_a)
|
||||||
end
|
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
|
def test_chunk
|
||||||
e = [].chunk {|elt| true }
|
e = [].chunk {|elt| true }
|
||||||
assert_equal([], e.to_a)
|
assert_equal([], e.to_a)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue