ruby-changes:42535
From: akr <ko1@a...>
Date: Sat, 16 Apr 2016 00:30:01 +0900 (JST)
Subject: [ruby-changes:42535] akr:r54609 (trunk): * array.c (rb_ary_sum): Don't yield same element twice.
akr 2016-04-16 01:26:38 +0900 (Sat, 16 Apr 2016) New Revision: 54609 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54609 Log: * array.c (rb_ary_sum): Don't yield same element twice. Found by nagachika. Modified files: trunk/ChangeLog trunk/array.c trunk/test/ruby/test_array.rb Index: array.c =================================================================== --- array.c (revision 54608) +++ array.c (revision 54609) @@ -5736,12 +5736,14 @@ rb_ary_sum(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5736 f = NUM2DBL(v); c = 0.0; + goto has_float_value; for (; i < RARRAY_LEN(ary); i++) { double x, y, t; e = RARRAY_AREF(ary, i); if (block_given) e = rb_yield(e); if (RB_FLOAT_TYPE_P(e)) + has_float_value: x = RFLOAT_VALUE(e); else if (FIXNUM_P(e)) x = FIX2LONG(e); @@ -5763,10 +5765,12 @@ rb_ary_sum(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5765 v = DBL2NUM(f); } + goto has_some_value; for (; i < RARRAY_LEN(ary); i++) { e = RARRAY_AREF(ary, i); if (block_given) e = rb_yield(e); + has_some_value: v = rb_funcall(v, idPLUS, 1, e); } return v; Index: ChangeLog =================================================================== --- ChangeLog (revision 54608) +++ ChangeLog (revision 54609) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Apr 16 01:16:02 2016 Tanaka Akira <akr@f...> + + * array.c (rb_ary_sum): Don't yield same element twice. + Found by nagachika. + Sat Apr 16 01:03:32 2016 Tanaka Akira <akr@f...> * array.c (rb_ary_sum): Fix SEGV by [1/2r, 1].sum. Index: test/ruby/test_array.rb =================================================================== --- test/ruby/test_array.rb (revision 54608) +++ test/ruby/test_array.rb (revision 54609) @@ -1,6 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1 # coding: US-ASCII # frozen_string_literal: false require 'test/unit' +require "delegate" require "rbconfig/sizeof" class TestArray < Test::Unit::TestCase @@ -2769,6 +2770,12 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2770 assert_int_equal(13, [1, 2].sum(10)) assert_int_equal(16, [1, 2].sum(10) {|v| v * 2 }) + yielded = [] + three = SimpleDelegator.new(3) + ary = [1, 2.0, three] + assert_float_equal(12.0, ary.sum {|x| yielded << x; x * 2 }) + assert_equal(ary, yielded) + assert_raise(TypeError) { [Object.new].sum } large_number = 100000000 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/