diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-05-07 08:24:09 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-05-07 08:24:09 +0000 |
commit | a352b0a20744a19c2fcb2ea5af576805c0706aea (patch) | |
tree | 21ab713b028bfa5e27eb997bb395b8e432a776d1 | |
parent | f8661f7ffb42bb831f62f06f43b6a0fc706c23a0 (diff) |
numeric.c: check keyword arguments
* numeric.c (num_step_scan_args): check keyword arguments and fail
if they conflict with positional arguments.
[ruby-dev:48177] [Bug #9811]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45861 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | numeric.c | 15 | ||||
-rw-r--r-- | test/ruby/test_numeric.rb | 8 |
3 files changed, 27 insertions, 2 deletions
@@ -1,3 +1,9 @@ +Wed May 7 17:24:07 2014 Nobuyoshi Nakada <[email protected]> + + * numeric.c (num_step_scan_args): check keyword arguments and fail + if they conflict with positional arguments. + [ruby-dev:48177] [Bug #9811] + Wed May 7 12:06:14 2014 Koichi Sasada <[email protected]> * benchmark/driver.rb: remove debug output and output results into @@ -1867,8 +1867,19 @@ num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step) argc = rb_scan_args(argc, argv, "02:", to, step, &hash); if (!NIL_P(hash)) { - *step = rb_hash_aref(hash, sym_by); - *to = rb_hash_aref(hash, sym_to); + ID keys[2]; + VALUE values[2]; + keys[0] = sym_to; + keys[1] = sym_by; + rb_get_kwargs(hash, keys, 0, 2, values); + if (values[0] != Qundef) { + if (argc > 0) rb_raise(rb_eArgError, "to is given twice"); + *to = values[0]; + } + if (values[1] != Qundef) { + if (argc > 1) rb_raise(rb_eArgError, "step is given twice"); + *step = values[1]; + } } else { /* compatibility */ diff --git a/test/ruby/test_numeric.rb b/test/ruby/test_numeric.rb index 7265c0ed46..674e869473 100644 --- a/test/ruby/test_numeric.rb +++ b/test/ruby/test_numeric.rb @@ -268,6 +268,14 @@ class TestNumeric < Test::Unit::TestCase assert_nothing_raised { 1.step(by: nil) } assert_nothing_raised { 1.step(by: nil).size } + bug9811 = '[ruby-dev:48177] [Bug #9811]' + assert_raise(ArgumentError, bug9811) { 1.step(10, foo: nil) {} } + assert_raise(ArgumentError, bug9811) { 1.step(10, foo: nil).size } + assert_raise(ArgumentError, bug9811) { 1.step(10, to: 11) {} } + assert_raise(ArgumentError, bug9811) { 1.step(10, to: 11).size } + assert_raise(ArgumentError, bug9811) { 1.step(10, 1, by: 11) {} } + assert_raise(ArgumentError, bug9811) { 1.step(10, 1, by: 11).size } + assert_equal(bignum*2+1, (-bignum).step(bignum, 1).size) assert_equal(bignum*2, (-bignum).step(bignum-1, 1).size) |