From 7316302483182a73136b00ad368585e551cbd86f Mon Sep 17 00:00:00 2001 From: marcandre Date: Wed, 14 Mar 2012 21:10:34 +0000 Subject: [PATCH] * include/ruby/intern.h: Add rb_check_arity, rb_error_arity [#6085] * array.c: Use rb_check_arity / rb_error_arity * class.c: ditto * enumerator.c: ditto * eval.c: ditto * file.c: ditto * hash.c: ditto * numeric.c: ditto * proc.c: ditto * process.c: ditto * random.c: ditto * re.c: ditto * signal.c: ditto * string.c: ditto * struct.c: ditto * transcode.c: ditto * vm_eval.c: ditto * vm_insnhelper.c: ditto & implementation of rb_error_arity * test/ruby/test_arity.rb: tests for above git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35024 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 49 +++++++++++++++++++++++++++++++++++++++++ array.c | 12 +++------- class.c | 7 +----- enumerator.c | 2 +- eval.c | 13 ++++------- file.c | 6 ++--- hash.c | 4 +--- include/ruby/intern.h | 6 +++++ numeric.c | 12 ++++------ proc.c | 6 ++--- process.c | 10 +++------ random.c | 4 ++-- re.c | 4 +--- signal.c | 13 ++++------- string.c | 37 ++++++++++--------------------- struct.c | 4 +--- test/ruby/test_arity.rb | 10 +++++++++ transcode.c | 10 +++------ vm_eval.c | 40 ++++++++++----------------------- vm_insnhelper.c | 18 +++++++-------- 20 files changed, 130 insertions(+), 137 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2d392c7f34..d5a9230e91 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,52 @@ +Thu Mar 15 06:08:06 2012 Marc-Andre Lafortune + + * include/ruby/intern.h: Add rb_check_arity, rb_error_arity [#6085] + + * array.c: Use rb_check_arity / rb_error_arity + + * class.c: ditto + + * enumerator.c: ditto + + * eval.c: ditto + + * file.c: ditto + + * hash.c: ditto + + * numeric.c: ditto + + * proc.c: ditto + + * process.c: ditto + + * random.c: ditto + + * re.c: ditto + + * signal.c: ditto + + * string.c: ditto + + * struct.c: ditto + + * transcode.c: ditto + + * vm_eval.c: ditto + + * vm_insnhelper.c: ditto & implementation of rb_error_arity + + * test/ruby/test_arity.rb: tests for above + +Thu Mar 15 06:08:05 2012 Marc-Andre Lafortune + + * vm_insnhelper.c: improve number of arguments error in case of + optional parameters (issue #6085) + + * include/ruby/intern.h: define UNLIMITED_ARGUMENTS + + * test/ruby/test_arity.rb: test for above + Thu Mar 15 00:58:04 2012 Shugo Maeda * enumerator.c (enumerable_lazy): fix the documentation of diff --git a/array.c b/array.c index 64b97f47ab..59572ecaad 100644 --- a/array.c +++ b/array.c @@ -1443,9 +1443,7 @@ rb_ary_aset(int argc, VALUE *argv, VALUE ary) rb_ary_splice(ary, beg, len, argv[2]); return argv[2]; } - if (argc != 2) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc); - } + rb_check_arity(argc, 2, 2); rb_ary_modify_check(ary); if (FIXNUM_P(argv[0])) { offset = FIX2LONG(argv[0]); @@ -1480,9 +1478,7 @@ rb_ary_insert(int argc, VALUE *argv, VALUE ary) { long pos; - if (argc < 1) { - rb_raise(rb_eArgError, "wrong number of arguments (at least 1)"); - } + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); rb_ary_modify_check(ary); if (argc == 1) return ary; pos = NUM2LONG(argv[0]); @@ -3902,9 +3898,7 @@ rb_ary_shuffle_bang(int argc, VALUE *argv, VALUE ary) if (OPTHASH_GIVEN_P(opts)) { randgen = rb_hash_lookup2(opts, sym_random, randgen); } - if (argc > 0) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); - } + rb_check_arity(argc, 0, 0); rb_ary_modify(ary); i = RARRAY_LEN(ary); ptr = RARRAY_PTR(ary); diff --git a/class.c b/class.c index 74147b753b..a34a3bd930 100644 --- a/class.c +++ b/class.c @@ -1655,12 +1655,7 @@ rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...) return argc; argc_error: - if (0 < n_opt) - rb_raise(rb_eArgError, "wrong number of arguments (%d for %d..%d%s)", - argc, n_mand, n_mand + n_opt, f_var ? "+" : ""); - else - rb_raise(rb_eArgError, "wrong number of arguments (%d for %d%s)", - argc, n_mand, f_var ? "+" : ""); + rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt); } /*! diff --git a/enumerator.c b/enumerator.c index c4d18ac076..28abc74987 100644 --- a/enumerator.c +++ b/enumerator.c @@ -352,7 +352,7 @@ enumerator_initialize(int argc, VALUE *argv, VALUE obj) if (argc == 0) { if (!rb_block_given_p()) - rb_raise(rb_eArgError, "wrong number of argument (0 for 1+)"); + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); recv = generator_init(generator_allocate(rb_cGenerator), rb_block_proc()); } diff --git a/eval.c b/eval.c index 34ff32c0cf..43ddb4197c 100644 --- a/eval.c +++ b/eval.c @@ -555,7 +555,7 @@ make_exception(int argc, VALUE *argv, int isstr) } break; default: - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..3)", argc); + rb_check_arity(argc, 0, 3); break; } if (argc > 0) { @@ -889,10 +889,7 @@ rb_mod_mix_into(int argc, VALUE *argv, VALUE klass) st_table *const_tbl = 0, *method_tbl = 0; int i = 0; - if (argc < 1 || argc > 3) { - wrong_args: - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc); - } + rb_check_arity(argc, 1, 3); module = argv[i++]; switch (TYPE(module)) { @@ -922,7 +919,7 @@ rb_mod_mix_into(int argc, VALUE *argv, VALUE klass) } methods = tmp; } - if (i < argc) goto wrong_args; + if (i < argc) rb_raise(rb_eArgError, "wrong arguments"); if (!NIL_P(constants)) { VALUE hash = rb_hash_new(); for (i = 0; i < RARRAY_LEN(constants); ++i) { @@ -1015,9 +1012,7 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj) { int i; - if (argc == 0) { - rb_raise(rb_eArgError, "wrong number of arguments (at least 1)"); - } + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); for (i = 0; i < argc; i++) Check_Type(argv[i], T_MODULE); while (argc--) { diff --git a/file.c b/file.c index 9f3f72a53c..1869a29c3b 100644 --- a/file.c +++ b/file.c @@ -2581,7 +2581,7 @@ rb_file_s_umask(int argc, VALUE *argv) omask = umask(NUM2INT(argv[0])); } else { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc); + rb_check_arity(argc, 0, 1); } return INT2FIX(omask); } @@ -4180,7 +4180,7 @@ test_check(int n, int argc, VALUE *argv) rb_secure(2); n+=1; - if (n != argc) rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, n); + rb_check_arity(argc, n, n); for (i=1; i 0) { - rb_raise(rb_eArgError, "wrong number of arguments"); - } + rb_check_arity(argc, 0, 0); ifnone = rb_block_proc(); default_proc_arity_check(ifnone); RHASH_IFNONE(hash) = ifnone; diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 763472589b..cf36aa6265 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -256,6 +256,12 @@ int rb_sourceline(void); const char *rb_sourcefile(void); VALUE rb_check_funcall(VALUE, ID, int, VALUE*); +NORETURN(void rb_error_arity(int, int, int)); +#define rb_check_arity(argc, min, max) do { \ + if (((argc) < (min)) || ((argc) > (max) && (max) != UNLIMITED_ARGUMENTS)) \ + rb_error_arity(argc, min, max); \ + } while(0) + #if defined(NFDBITS) && defined(HAVE_RB_FD_INIT) typedef struct { int maxfd; diff --git a/numeric.c b/numeric.c index 49bfbbaf01..b118d84d92 100644 --- a/numeric.c +++ b/numeric.c @@ -1765,13 +1765,9 @@ num_step(int argc, VALUE *argv, VALUE from) step = INT2FIX(1); } else { - if (argc == 2) { - to = argv[0]; - step = argv[1]; - } - else { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc); - } + rb_check_arity(argc, 1, 2); + to = argv[0]; + step = argv[1]; if (rb_equal(step, INT2FIX(0))) { rb_raise(rb_eArgError, "step can't be 0"); } @@ -2357,7 +2353,7 @@ int_chr(int argc, VALUE *argv, VALUE num) case 1: break; default: - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc); + rb_check_arity(argc, 0, 1); break; } enc = rb_to_encoding(argv[0]); diff --git a/proc.c b/proc.c index 1ea190c3b3..8dcdf8fc3a 100644 --- a/proc.c +++ b/proc.c @@ -1350,7 +1350,8 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod) id = rb_to_id(argv[0]); body = rb_block_lambda(); } - else if (argc == 2) { + else { + rb_check_arity(argc, 1, 2); id = rb_to_id(argv[0]); body = argv[1]; if (!rb_obj_is_method(body) && !rb_obj_is_proc(body)) { @@ -1359,9 +1360,6 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod) rb_obj_classname(body)); } } - else { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc); - } if (rb_obj_is_method(body)) { struct METHOD *method = (struct METHOD *)DATA_PTR(body); diff --git a/process.c b/process.c index 2323195f8f..494b533c60 100644 --- a/process.c +++ b/process.c @@ -1721,9 +1721,7 @@ rb_check_argv(int argc, VALUE *argv) int i; const char *name = 0; - if (argc == 0) { - rb_raise(rb_eArgError, "wrong number of arguments"); - } + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); prog = 0; tmp = rb_check_array_type(argv[0]); @@ -3385,11 +3383,9 @@ rb_f_sleep(int argc, VALUE *argv) if (argc == 0) { rb_thread_sleep_forever(); } - else if (argc == 1) { - rb_thread_wait_for(rb_time_interval(argv[0])); - } else { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc); + rb_check_arity(argc, 0, 1); + rb_thread_wait_for(rb_time_interval(argv[0])); } end = time(0) - beg; diff --git a/random.c b/random.c index 616cd68b52..1a93779dc9 100644 --- a/random.c +++ b/random.c @@ -1168,8 +1168,8 @@ rand_random(int argc, VALUE *argv, rb_random_t *rnd) if (argc == 0) { return rb_float_new(genrand_real(&rnd->mt)); } - else if (argc != 1) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc); + else { + rb_check_arity(argc, 0, 1); } vmax = argv[0]; if (NIL_P(vmax)) { diff --git a/re.c b/re.c index 38a40771aa..e24050cfab 100644 --- a/re.c +++ b/re.c @@ -2904,9 +2904,7 @@ rb_reg_initialize_m(int argc, VALUE *argv, VALUE self) const char *ptr; long len; - if (argc == 0 || argc > 3) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..3)", argc); - } + rb_check_arity(argc, 1, 3); if (TYPE(argv[0]) == T_REGEXP) { VALUE re = argv[0]; diff --git a/signal.c b/signal.c index 08ce482ded..e1b757e8c3 100644 --- a/signal.c +++ b/signal.c @@ -231,10 +231,7 @@ esignal_init(int argc, VALUE *argv, VALUE self) if (!NIL_P(sig)) argnum = 2; else sig = argv[0]; } - if (argc < 1 || argnum < argc) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", - argc, argnum); - } + rb_check_arity(argc, 1, argnum); if (argnum == 2) { signo = NUM2INT(sig); if (signo < 0 || signo > NSIG) { @@ -346,8 +343,8 @@ rb_f_kill(int argc, VALUE *argv) const char *s; rb_secure(2); - if (argc < 2) - rb_raise(rb_eArgError, "wrong number of arguments (%d for at least 2)", argc); + rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS); + switch (TYPE(argv[0])) { case T_FIXNUM: sig = FIX2INT(argv[0]); @@ -931,9 +928,7 @@ sig_trap(int argc, VALUE *argv) struct trap_arg arg; rb_secure(2); - if (argc < 1 || argc > 2) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc); - } + rb_check_arity(argc, 1, 2); arg.sig = trap_signm(argv[0]); if (reserved_signal_p(arg.sig)) { diff --git a/string.c b/string.c index 6355070324..f2694a8947 100644 --- a/string.c +++ b/string.c @@ -2727,7 +2727,7 @@ rb_str_match_m(int argc, VALUE *argv, VALUE str) { VALUE re, result; if (argc < 1) - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc); + rb_check_arity(argc, 1, 2); re = argv[0]; argv[0] = str; result = rb_funcall2(get_pat(re, 0), rb_intern("match"), argc, argv); @@ -3237,9 +3237,7 @@ rb_str_aref_m(int argc, VALUE *argv, VALUE str) } return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1])); } - if (argc != 1) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc); - } + rb_check_arity(argc, 1, 2); return rb_str_aref(str, argv[0]); } @@ -3467,9 +3465,7 @@ rb_str_aset_m(int argc, VALUE *argv, VALUE str) } return argv[2]; } - if (argc != 2) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc); - } + rb_check_arity(argc, 2, 3); return rb_str_aset(str, argv[0], argv[1]); } @@ -3532,9 +3528,7 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str) VALUE buf[3]; int i; - if (argc < 1 || 2 < argc) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc); - } + rb_check_arity(argc, 1, 2); for (i=0; i 0) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); - } + rb_check_arity(argc, 0, 0); RETURN_ENUMERATOR(s, 0, 0); result = rb_ary_new(); for (i = 0; i < RSTRUCT_LEN(s); i++) { diff --git a/test/ruby/test_arity.rb b/test/ruby/test_arity.rb index 23c2cd9e67..2d84330ffc 100644 --- a/test/ruby/test_arity.rb +++ b/test/ruby/test_arity.rb @@ -58,4 +58,14 @@ class TestArity < Test::Unit::TestCase assert_equal :ok, p.call(1, 2, 3) assert_equal :ok, p.call end + + def test_message_change_issue_6085 + assert_equal "3 for 1..2", err_mess{ SignalException.new(1, "", nil) } + assert_equal "1 for 0", err_mess{ Hash.new(1){} } + assert_equal "3 for 1..2", err_mess{ Module.send :define_method, 1, 2, 3 } + assert_equal "1 for 2", err_mess{ "".sub!(//) } + assert_equal "0 for 1..2", err_mess{ "".sub!{} } + assert_equal "0 for 1+", err_mess{ exec } + assert_equal "0 for 1+", err_mess{ Struct.new } + end end diff --git a/transcode.c b/transcode.c index ec5f93ce1e..c0049fd932 100644 --- a/transcode.c +++ b/transcode.c @@ -2665,9 +2665,7 @@ str_transcode0(int argc, VALUE *argv, VALUE *self, int ecflags, VALUE ecopts) const char *sname, *dname; int dencidx; - if (argc <0 || argc > 2) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc); - } + rb_check_arity(argc, 0, 2); if (argc == 0) { arg1 = rb_enc_default_internal(); @@ -2984,8 +2982,7 @@ econv_args(int argc, VALUE *argv, if (!NIL_P(flags_v)) { if (!NIL_P(opt)) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", - argc + 1); + rb_error_arity(argc + 1, 2, 3); } ecflags = NUM2INT(rb_to_int(flags_v)); ecopts = Qnil; @@ -3672,8 +3669,7 @@ econv_primitive_convert(int argc, VALUE *argv, VALUE self) if (!NIL_P(flags_v)) { if (!NIL_P(opt)) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..5)", - argc + 1); + rb_error_arity(argc + 1, 2, 5); } flags = NUM2INT(rb_to_int(flags_v)); } diff --git a/vm_eval.c b/vm_eval.c index aa326216b9..8bad08944d 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -87,16 +87,12 @@ vm_call0(rb_thread_t* th, VALUE recv, VALUE id, int argc, const VALUE *argv, break; } case VM_METHOD_TYPE_ATTRSET: { - if (argc != 1) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc); - } + rb_check_arity(argc, 1, 1); val = rb_ivar_set(recv, def->body.attr.id, argv[0]); break; } case VM_METHOD_TYPE_IVAR: { - if (argc != 0) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); - } + rb_check_arity(argc, 0, 0); val = rb_attr_get(recv, def->body.attr.id); break; } @@ -1283,36 +1279,24 @@ static VALUE specific_eval(int argc, VALUE *argv, VALUE klass, VALUE self) { if (rb_block_given_p()) { - if (argc > 0) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); - } + rb_check_arity(argc, 0, 0); return yield_under(klass, self, Qundef); } else { const char *file = "(eval)"; int line = 1; - if (argc == 0) { - rb_raise(rb_eArgError, "block not supplied"); + rb_check_arity(argc, 1, 3); + if (rb_safe_level() >= 4) { + StringValue(argv[0]); } else { - if (rb_safe_level() >= 4) { - StringValue(argv[0]); - } - else { - SafeStringValue(argv[0]); - } - if (argc > 3) { - const char *name = rb_id2name(rb_frame_callee()); - rb_raise(rb_eArgError, - "wrong number of arguments: %s(src) or %s{..}", - name, name); - } - if (argc > 2) - line = NUM2INT(argv[2]); - if (argc > 1) { - file = StringValuePtr(argv[1]); - } + SafeStringValue(argv[0]); + } + if (argc > 2) + line = NUM2INT(argv[2]); + if (argc > 1) { + file = StringValuePtr(argv[1]); } return eval_under(klass, self, argv[0], file, line); } diff --git a/vm_insnhelper.c b/vm_insnhelper.c index f471ca5ccb..bcdf0d7a34 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -163,6 +163,11 @@ unknown_keyword_error(const rb_iseq_t *iseq, VALUE hash) rb_raise(rb_eArgError, msg, RSTRING_PTR(keys)); } +void +rb_error_arity(int argc, int min, int max) { + rb_exc_raise(rb_arg_error_new(argc, min, max)); +} + #define VM_CALLEE_SETUP_ARG(ret, th, iseq, orig_argc, orig_argv, block) \ if (LIKELY((iseq)->arg_simple & 0x01)) { \ /* simple check */ \ @@ -354,10 +359,7 @@ call_cfunc(VALUE (*func)(), VALUE recv, { /* printf("len: %d, argc: %d\n", len, argc); */ - if (len >= 0 && argc != len) { - rb_raise(rb_eArgError, "wrong number of arguments(%d for %d)", - argc, len); - } + if (len >= 0) rb_check_arity(argc, len, len); switch (len) { case -2: @@ -581,17 +583,13 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, break; } case VM_METHOD_TYPE_ATTRSET:{ - if (num != 1) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", num); - } + rb_check_arity(num, 1, 1); val = rb_ivar_set(recv, me->def->body.attr.id, *(cfp->sp - 1)); cfp->sp -= 2; break; } case VM_METHOD_TYPE_IVAR:{ - if (num != 0) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", num); - } + rb_check_arity(num, 0, 0); val = rb_attr_get(recv, me->def->body.attr.id); cfp->sp -= 1; break;