From f8b4df93d22930ad5ca4f73ea7740ea253d4859b Mon Sep 17 00:00:00 2001 From: usa Date: Sun, 18 Mar 2018 15:33:37 +0000 Subject: merge revision(s) 60666,60667,60668: [Backport #14082] Fix size on Enumerable#cycle when the size is 0 [Bug #14082]. Patch by Kenichi Kamiya test/ruby/test_lazy_enumerator.rb: test for [Bug #14082] 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/branches/ruby_2_3@62827 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 15 +++++++++++++++ enum.c | 14 ++++++++------ test/ruby/test_enumerator.rb | 9 +++++++++ test/ruby/test_lazy_enumerator.rb | 9 +++++++++ version.h | 2 +- 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index fd0e113fd1..e572dcda3c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +Mon Mar 19 00:32:31 2018 Nobuyoshi Nakada + + * test/ruby/test_lazy_enumerator.rb: test for [Bug #14082] + + enum.c: check argument first + + * enum.c (enum_cycle_size): check an argument before the size of the + receiver, if it is given. + +Mon Mar 19 00:32:31 2018 Marc-Andre Lafortune + + Fix size on Enumerable#cycle when the size is 0 [Bug #14082]. + + Patch by Kenichi Kamiya + Mon Mar 19 00:28:28 2018 Nobuyoshi Nakada * parse.y (parser_here_document): an escaped newline is not an diff --git a/enum.c b/enum.c index 795aaf13a3..41561c2847 100644 --- a/enum.c +++ b/enum.c @@ -2678,17 +2678,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; + 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) || size == INT2FIX(0)) return size; + + if (NIL_P(n)) return DBL2NUM(INFINITY); if (mul <= 0) return INT2FIX(0); return rb_funcall(size, '*', 1, LONG2FIX(mul)); } diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index aa36187ba2..dfb3e02db9 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -575,13 +575,22 @@ class TestEnumerator < Test::Unit::TestCase assert_equal Float::INFINITY, [:foo].cycle.size assert_equal 10, [:foo, :bar].cycle(5).size assert_equal 0, [:foo, :bar].cycle(-10).size + assert_equal Float::INFINITY, {foo: 1}.cycle.size + assert_equal 10, {foo: 1, bar: 2}.cycle(5).size + assert_equal 0, {foo: 1, bar: 2}.cycle(-10).size assert_equal 0, [].cycle.size assert_equal 0, [].cycle(5).size + assert_equal 0, {}.cycle.size + assert_equal 0, {}.cycle(5).size assert_equal nil, @obj.cycle.size assert_equal nil, @obj.cycle(5).size assert_equal Float::INFINITY, @sized.cycle.size 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 5feddd5bde..0b9e4dc199 100644 --- a/test/ruby/test_lazy_enumerator.rb +++ b/test/ruby/test_lazy_enumerator.rb @@ -480,6 +480,15 @@ EOS assert_equal Float::INFINITY, loop.lazy.cycle.size assert_equal nil, lazy.select{}.cycle(4).size assert_equal nil, lazy.select{}.cycle.size + + class << (obj = Object.new) + def each; end + def size; 0; end + include Enumerable + end + lazy = obj.lazy + assert_equal 0, lazy.cycle.size + assert_raise(TypeError) {lazy.cycle("").size} end def test_map_zip diff --git a/version.h b/version.h index 5a363b8a1c..11f95fb1f1 100644 --- a/version.h +++ b/version.h @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.3.7" #define RUBY_RELEASE_DATE "2018-03-19" -#define RUBY_PATCHLEVEL 427 +#define RUBY_PATCHLEVEL 428 #define RUBY_RELEASE_YEAR 2018 #define RUBY_RELEASE_MONTH 3 -- cgit v1.2.3