From b6c9957058c02541a38e9c5057bd5cc4f3823172 Mon Sep 17 00:00:00 2001 From: mame Date: Tue, 25 Mar 2008 10:13:03 +0000 Subject: * array.c (ary_new): fix size check. [ruby-dev:34123] * array.c (rb_ary_take, rb_ary_drop): check negative size and use NUM2LONG instead of FIX2LONG. [ruby-dev:34123] * enum.c (enum_take, enum_drop): check negative size. * test/ruby/test_array.rb: add tests for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15838 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- enum.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'enum.c') diff --git a/enum.c b/enum.c index 0715772a39..b09e82623f 100644 --- a/enum.c +++ b/enum.c @@ -1499,9 +1499,14 @@ static VALUE enum_take(VALUE obj, VALUE n) { VALUE args[2]; + long len = NUM2LONG(n); - args[1] = NUM2LONG(n); - args[0] = rb_ary_new2(args[1]); + if (len < 0) { + rb_raise(rb_eArgError, "attempt to take negative size"); + } + + args[1] = len; + args[0] = rb_ary_new(); rb_block_call(obj, id_each, 0, 0, take_i, (VALUE)args); return args[0]; } @@ -1566,9 +1571,14 @@ static VALUE enum_drop(VALUE obj, VALUE n) { VALUE args[2]; + long len = NUM2LONG(n); - args[1] = NUM2ULONG(n); - args[0] = rb_ary_new2(args[1]); + if (len < 0) { + rb_raise(rb_eArgError, "attempt to drop negative size"); + } + + args[1] = len; + args[0] = rb_ary_new(); rb_block_call(obj, id_each, 0, 0, drop_i, (VALUE)args); return args[0]; } -- cgit v1.2.3