fix segmentation fault in Array#first

This commit is contained in:
Akira Kuroda
2012-07-29 00:21:40 +09:00
parent 919a84771b
commit a2aa7e7f52
2 changed files with 21 additions and 1 deletions
+3
View File
@@ -764,6 +764,9 @@ mrb_ary_first(mrb_state *mrb, mrb_value self)
if (mrb_get_args(mrb, "|i", &size) == 0) {
return (a->len > 0)? a->ptr[0]: mrb_nil_value();
}
if (size < 0) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array size");
}
if (size > a->len) size = a->len;
if (a->flags & MRB_ARY_SHARED) {
+18 -1
View File
@@ -118,7 +118,24 @@ assert('Array#first', '15.2.12.5.13') do
a = []
b = [1,2,3]
a.first == nil and b.first == 1
e2 = nil
e3 = nil
begin
# this will cause an exception due to the wrong argument
[1,2,3].first(-1)
rescue => e1
e2 = e1
end
begin
# this will cause an exception due to the wrong argument
[1,2,3].first(1,2)
rescue => e1
e3 = e1
end
a.first == nil and b.first == 1 and b.first(0) == [] and
b.first(1) == [1] and b.first(4) == [1,2,3] and
e2.class == ArgumentError and e3.class == ArgumentError
end
assert('Array#index', '15.2.12.5.14') do