[前][次][番号順一覧][スレッド一覧]

ruby-changes:37700

From: nobu <ko1@a...>
Date: Sat, 28 Feb 2015 17:19:00 +0900 (JST)
Subject: [ruby-changes:37700] nobu:r49781 (trunk): enum.c: limit size

nobu	2015-02-28 17:18:42 +0900 (Sat, 28 Feb 2015)

  New Revision: 49781

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49781

  Log:
    enum.c: limit size
    
    * enum.c (enum_each_slice, enum_each_cons): limit elements size by
      the enumerator size.  suggested by Hans Mackowiak <hanmac AT
      gmx.de> at [ruby-core:68335]

  Modified files:
    trunk/ChangeLog
    trunk/enum.c
    trunk/test/ruby/test_enum.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49780)
+++ ChangeLog	(revision 49781)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Feb 28 17:18:39 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* enum.c (enum_each_slice, enum_each_cons): limit elements size by
+	  the enumerator size.  suggested by Hans Mackowiak <hanmac AT
+	  gmx.de> at [ruby-core:68335]
+
 Sat Feb 28 15:44:20 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* vm_dump.c (rb_vm_bugreport): get rid of making new strings
Index: enum.c
===================================================================
--- enum.c	(revision 49780)
+++ enum.c	(revision 49781)
@@ -314,6 +314,24 @@ enum_size(VALUE self, VALUE args, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L314
     return (r == Qundef) ? Qnil : r;
 }
 
+static long
+limit_by_enum_size(VALUE obj, long n)
+{
+    unsigned long limit;
+    VALUE size = rb_check_funcall(obj, id_size, 0, 0);
+    if (size == Qundef) return n;
+    limit = NUM2ULONG(size);
+    return ((unsigned long)n > limit) ? limit : n;
+}
+
+static int
+enum_size_over_p(VALUE obj, long n)
+{
+    VALUE size = rb_check_funcall(obj, id_size, 0, 0);
+    if (size == Qundef) return 0;
+    return ((unsigned long)n > NUM2ULONG(size));
+}
+
 /*
  *  call-seq:
  *     enum.find_all { |obj| block } -> array
@@ -2187,6 +2205,7 @@ enum_each_slice(VALUE obj, VALUE n) https://github.com/ruby/ruby/blob/trunk/enum.c#L2205
 
     if (size <= 0) rb_raise(rb_eArgError, "invalid slice size");
     RETURN_SIZED_ENUMERATOR(obj, 1, &n, enum_each_slice_size);
+    size = limit_by_enum_size(obj, size);
     ary = rb_ary_new2(size);
     arity = rb_block_arity();
     memo = NEW_MEMO(ary, dont_recycle_block_arg(arity), size);
@@ -2264,6 +2283,7 @@ enum_each_cons(VALUE obj, VALUE n) https://github.com/ruby/ruby/blob/trunk/enum.c#L2283
     if (size <= 0) rb_raise(rb_eArgError, "invalid size");
     RETURN_SIZED_ENUMERATOR(obj, 1, &n, enum_each_cons_size);
     arity = rb_block_arity();
+    if (enum_size_over_p(obj, size)) return Qnil;
     memo = NEW_MEMO(rb_ary_new2(size), dont_recycle_block_arg(arity), size);
     rb_block_call(obj, id_each, 0, 0, each_cons_i, (VALUE)memo);
 
Index: test/ruby/test_enum.rb
===================================================================
--- test/ruby/test_enum.rb	(revision 49780)
+++ test/ruby/test_enum.rb	(revision 49781)
@@ -348,6 +348,14 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L348
     ary.clear
     (1..10).each_slice(3, &lambda {|a, *| ary << a})
     assert_equal([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]], ary, bug9749)
+
+    ary.clear
+    (1..10).each_slice(10) {|a| ary << a}
+    assert_equal([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], ary)
+
+    ary.clear
+    (1..10).each_slice(11) {|a| ary << a}
+    assert_equal([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], ary)
   end
 
   def test_each_cons
@@ -359,6 +367,14 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L367
     ary.clear
     (1..5).each_cons(3, &lambda {|a, *| ary << a})
     assert_equal([[1, 2, 3], [2, 3, 4], [3, 4, 5]], ary, bug9749)
+
+    ary.clear
+    (1..5).each_cons(5) {|a| ary << a}
+    assert_equal([[1, 2, 3, 4, 5]], ary)
+
+    ary.clear
+    (1..5).each_cons(6) {|a| ary << a}
+    assert_empty(ary)
   end
 
   def test_zip

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]