From 0a57e6c975559abf5e2f16d5042117da1d46f525 Mon Sep 17 00:00:00 2001 From: nagachika Date: Wed, 9 Oct 2013 16:04:12 +0000 Subject: merge revision(s) 42040,42041,42047: [Backport #8654] * array.c (rb_ary_count): iterate items appropriately. [Bug #8654] * 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] * test/ruby/test_array.rb (test_count): add a test case for #count with an argument. See Bug #8654. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@43228 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- array.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'array.c') diff --git a/array.c b/array.c index 6d4256a947..9f900ba305 100644 --- a/array.c +++ b/array.c @@ -4113,27 +4113,28 @@ 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) { - VALUE *p, *pend; + VALUE v; 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++) { + v = RARRAY_PTR(ary)[i]; + if (RTEST(rb_yield(v))) 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_PTR(ary)[i], obj)) n++; } } -- cgit v1.2.3