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

hash.c: fix possible crash in Hash#transform_keys!

Fix up r59328. It is possible that the given block abuses
ObjectSpace.each_object to shrink the temporary array.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59334 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
rhe 2017-07-14 09:41:05 +00:00
parent 7c9c2bc1dd
commit 677751e1bc

8
hash.c
View file

@ -1865,8 +1865,8 @@ rb_hash_transform_keys(VALUE hash)
/*
* call-seq:
* hsh.transform_keys! {|value| block } -> hsh
* hsh.transform_keys! -> an_enumerator
* hsh.transform_keys! {|key| block } -> hsh
* hsh.transform_keys! -> an_enumerator
*
* Invokes the given block once for each key in <i>hsh</i>, replacing it
* with the new key returned by the block, and then returns <i>hsh</i>.
@ -1889,8 +1889,8 @@ rb_hash_transform_keys_bang(VALUE hash)
long i;
VALUE keys = rb_hash_keys(hash);
for (i = 0; i < RARRAY_LEN(keys); ++i) {
VALUE new_key = rb_yield(RARRAY_AREF(keys, i));
rb_hash_aset(hash, new_key, rb_hash_delete(hash, RARRAY_AREF(keys, i)));
VALUE key = RARRAY_AREF(keys, i), new_key = rb_yield(key);
rb_hash_aset(hash, new_key, rb_hash_delete(hash, key));
}
}
return hash;