diff --git a/ChangeLog b/ChangeLog index 0e34a702f4..efbcc1e025 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Wed Jan 29 14:26:10 2014 Nobuyoshi Nakada + + * enumerator.c (enumerator_with_index): should not store local variable + address to memoise the arguments. it is invalidated after the return. + [ruby-core:58692] [Bug #9178] + +Wed Jan 29 14:26:10 2014 NARUSE, Yui + + * enumerator.c (enumerator_with_index): try to convert given offset to + integer. fix bug introduced in r39594. + Wed Jan 29 14:20:11 2014 Eric Hodel * enumerator.c (enumerator_with_index): Restore handling of a nil memo diff --git a/common.mk b/common.mk index ccc964701e..e48cdc5776 100644 --- a/common.mk +++ b/common.mk @@ -619,7 +619,7 @@ encoding.$(OBJEXT): {$(VPATH)}encoding.c $(RUBY_H_INCLUDES) \ {$(VPATH)}internal.h enum.$(OBJEXT): {$(VPATH)}enum.c $(RUBY_H_INCLUDES) {$(VPATH)}node.h \ {$(VPATH)}util.h $(ID_H_INCLUDES) -enumerator.$(OBJEXT): {$(VPATH)}enumerator.c $(RUBY_H_INCLUDES) +enumerator.$(OBJEXT): {$(VPATH)}enumerator.c $(RUBY_H_INCLUDES) {$(VPATH)}node.h error.$(OBJEXT): {$(VPATH)}error.c {$(VPATH)}known_errors.inc \ $(RUBY_H_INCLUDES) $(VM_CORE_H_INCLUDES) $(ENCODING_H_INCLUDES) \ {$(VPATH)}debug.h \ diff --git a/enumerator.c b/enumerator.c index 451327569e..ef99bf85fa 100644 --- a/enumerator.c +++ b/enumerator.c @@ -13,6 +13,7 @@ ************************************************/ #include "ruby/ruby.h" +#include "node.h" /* * Document-class: Enumerator @@ -368,9 +369,9 @@ enumerator_each(VALUE obj) static VALUE enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv) { - VALUE *memo = (VALUE *)m; - VALUE idx = *memo; - *memo = rb_int_succ(idx); + NODE *memo = (NODE *)m; + VALUE idx = memo->u1.value; + memo->u1.value = rb_int_succ(idx); if (argc <= 1) return rb_yield_values(2, val, idx); @@ -401,7 +402,7 @@ enumerator_with_index(int argc, VALUE *argv, VALUE obj) memo = INT2FIX(0); else memo = rb_to_int(memo); - return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)&memo); + return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)NEW_MEMO(memo, 0, 0)); } /* diff --git a/node.h b/node.h index bb96107711..a15e15781b 100644 --- a/node.h +++ b/node.h @@ -447,6 +447,7 @@ typedef struct RNode { #define NEW_ATTRASGN(r,m,a) NEW_NODE(NODE_ATTRASGN,r,m,a) #define NEW_PRELUDE(p,b) NEW_NODE(NODE_PRELUDE,p,b,0) #define NEW_OPTBLOCK(a) NEW_NODE(NODE_OPTBLOCK,a,0,0) +#define NEW_MEMO(a,b,c) NEW_NODE(NODE_MEMO,a,b,c) #if defined __GNUC__ && __GNUC__ >= 4 #pragma GCC visibility push(default) diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index fabb266f8f..feaa2de698 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -1,4 +1,5 @@ require 'test/unit' +require_relative "envutil" class TestEnumerator < Test::Unit::TestCase def setup @@ -105,6 +106,28 @@ class TestEnumerator < Test::Unit::TestCase 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_index_nonnum_offset + bug8010 = '[ruby-dev:47131] [Bug #8010]' + s = Object.new + def s.to_int; 1 end + assert_equal([[1,1],[2,2],[3,3]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a, bug8010) + end + + def test_with_index_string_offset + bug8010 = '[ruby-dev:47131] [Bug #8010]' + assert_raise(TypeError, bug8010){ @obj.to_enum(:foo, 1, 2, 3).with_index('1').to_a } + end + + def test_with_index_dangling_memo + bug9178 = '[ruby-core:58692] [Bug #9178]' + assert_in_out_err([], <<-"end;", ["Enumerator", "[false, [1]]"], [], bug9178) + bug = "#{bug9178}" + e = [1].to_enum(:chunk).with_index {|c,i| i == 5} + puts e.class + p e.to_a[0] + end; + end + def test_with_object obj = [0, 1] ret = (1..10).each.with_object(obj) {|i, memo| diff --git a/version.h b/version.h index d0f38330ba..ffd523fd26 100644 --- a/version.h +++ b/version.h @@ -1,5 +1,5 @@ #define RUBY_VERSION "1.9.3" -#define RUBY_PATCHLEVEL 499 +#define RUBY_PATCHLEVEL 500 #define RUBY_RELEASE_DATE "2014-01-29" #define RUBY_RELEASE_YEAR 2014