diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-05 23:32:50 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-05 23:32:50 +0000 |
commit | 4b7f531553100afad272c7078b23e6e32989bc2a (patch) | |
tree | 454c368ed3e59dcc7071d7e9079611c9b7610487 | |
parent | 6c1f11592c6e8f5347b758e66c869bde7ca32733 (diff) |
enum.c: check argument first
* enum.c (enum_cycle_size): check an argument before the size of
the receiver, if it is given.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60668 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | enum.c | 15 | ||||
-rw-r--r-- | test/ruby/test_enumerator.rb | 2 | ||||
-rw-r--r-- | test/ruby/test_lazy_enumerator.rb | 4 |
3 files changed, 13 insertions, 8 deletions
@@ -2840,18 +2840,19 @@ cycle_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary)) static VALUE enum_cycle_size(VALUE self, VALUE args, VALUE eobj) { - long mul; + long mul = 0; VALUE n = Qnil; - VALUE size = enum_size(self, args, 0); - - if (size == Qnil) return Qnil; - if (FIXNUM_ZERO_P(size)) return size; + VALUE size; if (args && (RARRAY_LEN(args) > 0)) { n = RARRAY_AREF(args, 0); + if (!NIL_P(n)) mul = NUM2LONG(n); } - if (n == Qnil) return DBL2NUM(INFINITY); - mul = NUM2LONG(n); + + size = enum_size(self, args, 0); + if (NIL_P(size) || FIXNUM_ZERO_P(size)) return size; + + if (NIL_P(n)) return DBL2NUM(INFINITY); if (mul <= 0) return INT2FIX(0); n = LONG2FIX(mul); return rb_funcallv(size, '*', 1, &n); diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index 0ee11dad36..51f69be37d 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -590,6 +590,8 @@ class TestEnumerator < Test::Unit::TestCase assert_equal 126, @sized.cycle(3).size assert_equal Float::INFINITY, [].to_enum { 42 }.cycle.size assert_equal 0, [].to_enum { 0 }.cycle.size + + assert_raise(TypeError) {[].to_enum { 0 }.cycle("").size} end def test_size_for_loops diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb index 4db58e7619..56890f4b6e 100644 --- a/test/ruby/test_lazy_enumerator.rb +++ b/test/ruby/test_lazy_enumerator.rb @@ -507,7 +507,9 @@ EOS def size; 0; end include Enumerable end - assert_equal 0, obj.lazy.cycle.size + lazy = obj.lazy + assert_equal 0, lazy.cycle.size + assert_raise(TypeError) {lazy.cycle("").size} end def test_map_zip |