Skip to content

Commit

Permalink
Add a frozenness check to Enumerator#initialize.
Browse files Browse the repository at this point in the history
* enumerator.c (enumerator_init): Add a frozenness check to
  prevent a frozen Enumerator object from being reinitialized with
  a different enumerable object.  This is the least we should do,
  and more fixes will follow. [Fixes rubyGH-368] Patch by Kenichi
  Kamiya.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42233 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
knu committed Jul 29, 2013
1 parent 8caa801 commit 3bd62e2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
@@ -1,3 +1,11 @@
Mon Jul 29 20:53:24 2013 Akinori MUSHA <knu@iDaemons.org>

* enumerator.c (enumerator_init): Add a frozenness check to
prevent a frozen Enumerator object from being reinitialized with
a different enumerable object. This is the least we should do,
and more fixes will follow. [Fixes GH-368] Patch by Kenichi
Kamiya.

Mon Jul 29 20:14:24 2013 Masaki Matsushita <glass.saga@gmail.com>

* hash.c (rb_hash_assoc): revert r42224. table->type->compare is
Expand Down
1 change: 1 addition & 0 deletions enumerator.c
Expand Up @@ -270,6 +270,7 @@ enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, VALUE *argv, rb
{
struct enumerator *ptr;

rb_check_frozen(enum_obj);
TypedData_Get_Struct(enum_obj, struct enumerator, &enumerator_data_type, ptr);

if (!ptr) {
Expand Down
10 changes: 10 additions & 0 deletions test/ruby/test_enumerator.rb
Expand Up @@ -67,6 +67,16 @@ def test_initialize
assert_match 'Enumerator.new without a block is deprecated', err
assert_equal([1, 2, 3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.take(3))
assert_raise(ArgumentError) { Enumerator.new }

enum = @obj.to_enum
assert_raise(NoMethodError) { enum.each {} }
enum.freeze
assert_raise(RuntimeError) {
capture_io do
# warning: Enumerator.new without a block is deprecated; use Object#to_enum
enum.__send__(:initialize, @obj, :foo)
end
}
end

def test_initialize_copy
Expand Down

0 comments on commit 3bd62e2

Please sign in to comment.