summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-03-25 10:13:03 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-03-25 10:13:03 +0000
commitb6c9957058c02541a38e9c5057bd5cc4f3823172 (patch)
treeab61130c08bc30fcae5f0f6441a0f9fbb0d9e465 /array.c
parentf6001be537623a52513919ee1890b2bbccc119f0 (diff)
* 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
Diffstat (limited to 'array.c')
-rw-r--r--array.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/array.c b/array.c
index 332dc3b993..da19f1c6a6 100644
--- a/array.c
+++ b/array.c
@@ -114,7 +114,7 @@ ary_new(VALUE klass, long len)
if (len < 0) {
rb_raise(rb_eArgError, "negative array size (or size too big)");
}
- if (len > 0 && len * sizeof(VALUE) <= len) {
+ if (len > 0 && len * (long)sizeof(VALUE) <= len) {
rb_raise(rb_eArgError, "array size too big");
}
ary = ary_alloc(klass);
@@ -3242,7 +3242,11 @@ rb_ary_product(int argc, VALUE *argv, VALUE ary)
static VALUE
rb_ary_take(VALUE obj, VALUE n)
{
- return rb_ary_subseq(obj, 0, FIX2LONG(n));
+ long len = NUM2LONG(n);
+ if (len < 0) {
+ rb_raise(rb_eArgError, "attempt to take negative size");
+ }
+ return rb_ary_subseq(obj, 0, len);
}
/*
@@ -3286,8 +3290,12 @@ static VALUE
rb_ary_drop(VALUE ary, VALUE n)
{
VALUE result;
+ long pos = NUM2LONG(n);
+ if (pos < 0) {
+ rb_raise(rb_eArgError, "attempt to drop negative size");
+ }
- result = rb_ary_subseq(ary, FIX2LONG(n), RARRAY_LEN(ary));
+ result = rb_ary_subseq(ary, pos, RARRAY_LEN(ary));
if (result == Qnil) result = rb_ary_new();
return result;
}