diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-05-07 05:26:16 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-05-07 05:26:16 +0000 |
commit | 9936642c409c48cfaced1a1f9d0f95fa100744e5 (patch) | |
tree | 706fa9f742a16d692610ccb8b5baba5f5a9fe708 | |
parent | 013b277316157a255141f2f5ddc2e0da9f167214 (diff) | |
download | ruby-9936642c409c48cfaced1a1f9d0f95fa100744e5.tar.gz |
* array.c (rb_ary_sample): negative sample number is invalid.
[ruby-core:23374]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | array.c | 1 | ||||
-rw-r--r-- | test/ruby/test_array.rb | 2 |
3 files changed, 8 insertions, 0 deletions
@@ -1,3 +1,8 @@ +Thu May 7 14:26:14 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * array.c (rb_ary_sample): negative sample number is invalid. + [ruby-core:23374] + Thu May 7 14:16:24 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> * string.c, include/ruby/encoding.h: fixed types. @@ -3450,6 +3450,7 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary) } rb_scan_args(argc, argv, "1", &nv); n = NUM2LONG(nv); + if (n < 0) rb_raise(rb_eArgError, "negative sample number"); ptr = RARRAY_PTR(ary); len = RARRAY_LEN(ary); if (n > len) n = len; diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 9b3dfdf45f..a20c6b23cd 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1605,6 +1605,8 @@ class TestArray < Test::Unit::TestCase end assert_operator(h.values.min * 2, :>=, h.values.max) if n != 0 end + + assert_raise(ArgumentError, '[ruby-core:23374]') {[1, 2].sample(-1)} end def test_cycle |