Project

General

Profile

Bug #8654 ยป 0001-array.c-rb_ary_count-check-length-to-avoid-SEGV.patch

Eregon (Benoit Daloze), 07/18/2013 07:50 PM

View differences:

ChangeLog
Thu Jul 18 19:48:05 2013 Benoit Daloze <eregontp@gmail.com>
* array.c (rb_ary_count): check length to avoid SEGV
while iterating. [ruby-core:56072] [Bug #8654]
Wed Jul 17 17:19:54 2013 Koichi Sasada <ko1@atdot.net>
* gc.c: rename heap management functions with prefix "heap_".
array.c
static VALUE
rb_ary_count(int argc, VALUE *argv, VALUE ary)
{
long n = 0;
long i, n = 0;
if (argc == 0) {
VALUE *p, *pend;
if (!rb_block_given_p())
return LONG2NUM(RARRAY_LEN(ary));
for (p = RARRAY_PTR(ary), pend = p + RARRAY_LEN(ary); p < pend; p++) {
if (RTEST(rb_yield(*p))) n++;
for (i=0; i<RARRAY_LEN(ary); i++) {
if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) n++;
}
}
else {
VALUE obj, *p, *pend;
VALUE obj;
rb_scan_args(argc, argv, "1", &obj);
if (rb_block_given_p()) {
rb_warn("given block not used");
}
for (p = RARRAY_PTR(ary), pend = p + RARRAY_LEN(ary); p < pend; p++) {
if (rb_equal(*p, obj)) n++;
for (i=0; i<RARRAY_LEN(ary); i++) {
if (rb_equal(RARRAY_AREF(ary, i), obj)) n++;
}
}
test/ruby/test_array.rb
assert_equal(3, a.count {|x| x % 2 == 1 })
assert_equal(2, a.count(1) {|x| x % 2 == 1 })
assert_raise(ArgumentError) { a.count(0, 1) }
bug8654 = '[ruby-core:56072]'
assert_in_out_err [], <<-EOS, ["0"], [], bug8654
a1 = []
a2 = Array.new(100) { |i| i }
r = a2.count do |i|
p i
a2.replace(a1) if i == 0
end
EOS
end
def test_delete
    (1-1/1)