aboutsummaryrefslogtreecommitdiffstats
path: root/array.c
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-18 11:38:01 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-18 11:38:01 +0000
commite1335a307c859f612d666245bf891436da8fdd86 (patch)
tree8c8c74fbaa0c43e568e214c840733b380a148996 /array.c
parent6f49bc635bd6b0f049d4dacc2b9e46ff82d24866 (diff)
downloadruby-e1335a307c859f612d666245bf891436da8fdd86.tar.gz
* 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
Diffstat (limited to 'array.c')
-rw-r--r--array.c9
1 files changed, 4 insertions, 5 deletions
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++;
}
}