Permalink
Browse files

Fix segfault in Array#sample

  • Loading branch information...
bouk committed Nov 23, 2016
1 parent 669bbc7 commit c7262be0aeaf55b5c92b26db82c08f2fe6b95412
Showing with 14 additions and 1 deletion.
  1. +2 −1 mrbgems/mruby-random/src/random.c
  2. +12 −0 mrbgems/mruby-random/test/random.rb
@@ -266,14 +266,15 @@ mrb_ary_sample(mrb_state *mrb, mrb_value ary)
mrb_int n = 0;
mrb_bool given;
mt_state *random = NULL;
mrb_int len = RARRAY_LEN(ary);
mrb_int len;
mrb_get_args(mrb, "|i?d", &n, &given, &random, &mt_state_type);
if (random == NULL) {
random = get_random_state(mrb);
}
mrb_random_rand_seed(mrb, random);
mt_rand(random);
len = RARRAY_LEN(ary);
if (!given) { /* pick one element */
switch (len) {
case 0:
@@ -74,3 +74,15 @@
ary1 != [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and 10.times { |x| ary1.include? x } and ary1 == ary2
end
assert('Array#sample checks input length after reading arguments') do
$ary = [1, 2, 3]
class ArrayChange
def to_i
$ary << 4
4
end
end
assert_equal [1, 2, 3, 4], $ary.sample(ArrayChange.new).sort
end

0 comments on commit c7262be

Please sign in to comment.