From e1335a307c859f612d666245bf891436da8fdd86 Mon Sep 17 00:00:00 2001 From: eregon Date: Thu, 18 Jul 2013 11:38:01 +0000 Subject: [PATCH] * array.c (rb_ary_count): check length to avoid SEGV while iterating. Remove other pointer loop when arg is given. * test/ruby/test_array.rb (test_count): add test for bug. [ruby-core:56072] [Bug #8654] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42041 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ array.c | 9 ++++----- test/ruby/test_array.rb | 10 ++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index ed5850d975..517f376642 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Thu Jul 18 20:35:14 2013 Benoit Daloze + + * array.c (rb_ary_count): check length to avoid SEGV + while iterating. Remove other pointer loop when arg is given. + + * test/ruby/test_array.rb (test_count): add test for bug. + [ruby-core:56072] [Bug #8654] + Thu Jul 18 18:14:36 2013 Masaki Matsushita * array.c (rb_ary_count): iterate items appropriately. diff --git a/array.c b/array.c index ef9007ae0d..0cd2f0d91a 100644 --- a/array.c +++ b/array.c @@ -4174,10 +4174,9 @@ rb_ary_compact(VALUE ary) static VALUE rb_ary_count(int argc, VALUE *argv, VALUE ary) { - long n = 0; + long i, n = 0; if (argc == 0) { - long i; VALUE v; if (!rb_block_given_p()) @@ -4189,14 +4188,14 @@ rb_ary_count(int argc, VALUE *argv, VALUE ary) } } 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++; } } diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 93ef59a3f5..b158db0ec9 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -572,6 +572,16 @@ class TestArray < Test::Unit::TestCase 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