1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-11-30 07:25:17 +00:00
parent 59ed302965
commit 520f0fec95
3 changed files with 20 additions and 4 deletions

View file

@ -1,3 +1,9 @@
Sat Nov 30 16:25:14 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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@ruby-lang.org> Sat Nov 30 13:28:13 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* siphash.c (sip_hash24): fix for aligned word access little endian * siphash.c (sip_hash24): fix for aligned word access little endian

View file

@ -495,9 +495,9 @@ enumerator_each(int argc, VALUE *argv, VALUE obj)
static VALUE static VALUE
enumerator_with_index_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, m)) enumerator_with_index_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, m))
{ {
VALUE *memo = (VALUE *)m; NODE *memo = (NODE *)m;
VALUE idx = *memo; VALUE idx = memo->u1.value;
*memo = rb_int_succ(idx); memo->u1.value = rb_int_succ(idx);
if (argc <= 1) if (argc <= 1)
return rb_yield_values(2, val, idx); return rb_yield_values(2, val, idx);
@ -537,7 +537,7 @@ enumerator_with_index(int argc, VALUE *argv, VALUE obj)
memo = INT2FIX(0); memo = INT2FIX(0);
else else
memo = rb_to_int(memo); 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));
} }
/* /*

View file

@ -134,6 +134,16 @@ class TestEnumerator < Test::Unit::TestCase
assert_raise(TypeError, bug8010){ @obj.to_enum(:foo, 1, 2, 3).with_index('1').to_a } assert_raise(TypeError, bug8010){ @obj.to_enum(:foo, 1, 2, 3).with_index('1').to_a }
end 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 def test_with_object
obj = [0, 1] obj = [0, 1]
ret = (1..10).each.with_object(obj) {|i, memo| ret = (1..10).each.with_object(obj) {|i, memo|