ruby-changes:31850
From: nobu <ko1@a...>
Date: Sat, 30 Nov 2013 16:25:23 +0900 (JST)
Subject: [ruby-changes:31850] nobu:r43929 (trunk): enumerator.c: should not store local variable address
nobu 2013-11-30 16:25:17 +0900 (Sat, 30 Nov 2013) New Revision: 43929 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43929 Log: enumerator.c: should not store local variable address * 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] Modified files: trunk/ChangeLog trunk/enumerator.c trunk/test/ruby/test_enumerator.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 43928) +++ ChangeLog (revision 43929) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Nov 30 16:25:14 2013 Nobuyoshi Nakada <nobu@r...> + + * 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] + Sat Nov 30 13:28:13 2013 Nobuyoshi Nakada <nobu@r...> * siphash.c (sip_hash24): fix for aligned word access little endian Index: enumerator.c =================================================================== --- enumerator.c (revision 43928) +++ enumerator.c (revision 43929) @@ -495,9 +495,9 @@ enumerator_each(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/enumerator.c#L495 static VALUE enumerator_with_index_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, m)) { - 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); @@ -537,7 +537,7 @@ enumerator_with_index(int argc, VALUE *a https://github.com/ruby/ruby/blob/trunk/enumerator.c#L537 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)); } /* Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 43928) +++ test/ruby/test_enumerator.rb (revision 43929) @@ -134,6 +134,16 @@ class TestEnumerator < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enumerator.rb#L134 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_separately([], <<-"end;") + bug = "#{bug9178}" + e = [1].to_enum(:chunk).with_index {|c,i| i == 5} + assert_kind_of(Enumerator, e) + assert_equal([false, [1]], e.to_a[0], bug) + end; + end + def test_with_object obj = [0, 1] ret = (1..10).each.with_object(obj) {|i, memo| -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/