From 4a429c11e1319b0e13368851454f109217ab5de5 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 5 Mar 2013 01:20:20 +0000 Subject: [PATCH] enumerator.c: allow Bignum for with_index * enumerator.c (enumerator_with_index_i): allow Bignum as offset, to get rid of conversion exception and integer overflow. [ruby-dev:47131] [Bug #8010] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39594 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 +++++- enumerator.c | 7 ++----- internal.h | 2 ++ numeric.c | 12 ++++++++---- test/ruby/test_enumerator.rb | 8 ++++++++ 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0515989cac..3d3cabd155 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,8 @@ -Tue Mar 5 10:19:09 2013 Nobuyoshi Nakada +Tue Mar 5 10:20:16 2013 Nobuyoshi Nakada + + * enumerator.c (enumerator_with_index_i): allow Bignum as offset, to + get rid of conversion exception and integer overflow. + [ruby-dev:47131] [Bug #8010] * numeric.c (rb_int_succ, rb_int_pred): shortcut optimization for Bignum. diff --git a/enumerator.c b/enumerator.c index edaa4ad307..bd039d28bc 100644 --- a/enumerator.c +++ b/enumerator.c @@ -460,11 +460,9 @@ enumerator_each(int argc, VALUE *argv, VALUE obj) static VALUE enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv) { - VALUE idx; VALUE *memo = (VALUE *)m; - - idx = INT2FIX(*memo); - ++*memo; + VALUE idx = *memo; + *memo = rb_int_succ(idx); if (argc <= 1) return rb_yield_values(2, val, idx); @@ -494,7 +492,6 @@ enumerator_with_index(int argc, VALUE *argv, VALUE obj) rb_scan_args(argc, argv, "01", &memo); RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_size); - memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo); return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)&memo); } diff --git a/internal.h b/internal.h index e66b5fd038..36bb1c90e7 100644 --- a/internal.h +++ b/internal.h @@ -169,6 +169,8 @@ VALUE num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl); int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl); double ruby_float_mod(double x, double y); int rb_num_negative_p(VALUE); +VALUE rb_int_succ(VALUE num); +VALUE rb_int_pred(VALUE num); /* object.c */ VALUE rb_obj_equal(VALUE obj1, VALUE obj2); diff --git a/numeric.c b/numeric.c index 342527c01d..6b6b527a13 100644 --- a/numeric.c +++ b/numeric.c @@ -2383,8 +2383,8 @@ fix_succ(VALUE num) * (-1).next #=> 0 */ -static VALUE -int_succ(VALUE num) +VALUE +rb_int_succ(VALUE num) { if (FIXNUM_P(num)) { long i = FIX2LONG(num) + 1; @@ -2396,6 +2396,8 @@ int_succ(VALUE num) return rb_funcall(num, '+', 1, INT2FIX(1)); } +#define int_succ rb_int_succ + /* * call-seq: * int.pred -> integer @@ -2406,8 +2408,8 @@ int_succ(VALUE num) * (-1).pred #=> -2 */ -static VALUE -int_pred(VALUE num) +VALUE +rb_int_pred(VALUE num) { if (FIXNUM_P(num)) { long i = FIX2LONG(num) - 1; @@ -2419,6 +2421,8 @@ int_pred(VALUE num) return rb_funcall(num, '-', 1, INT2FIX(1)); } +#define int_pred rb_int_pred + VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc) { diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index d66d7d0500..8843d8d732 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -104,6 +104,14 @@ class TestEnumerator < Test::Unit::TestCase assert_equal([[1,5],[2,6],[3,7]], @obj.to_enum(:foo, 1, 2, 3).with_index(5).to_a) end + def test_with_index_large_offset + bug8010 = '[ruby-dev:47131] [Bug #8010]' + s = 1 << (8*1.size-2) + assert_equal([[1,s],[2,s+1],[3,s+2]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a, bug8010) + s <<= 1 + assert_equal([[1,s],[2,s+1],[3,s+2]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a, bug8010) + end + def test_with_object obj = [0, 1] ret = (1..10).each.with_object(obj) {|i, memo|