From cb52dda1469a81d9be2538b727137358f41b60f7 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 14 Apr 2017 08:33:08 +0000 Subject: [PATCH] ruby.h: check argc to rb_yield_values * include/ruby/ruby.h (rb_yield_values): check if argc matches the number of variadic arguments, and replace with rb_yield_values2. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58350 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- include/ruby/ruby.h | 34 ++++++++++++++++++++++++++++++++++ vm_eval.c | 1 + 2 files changed, 35 insertions(+) diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index d770c30735..01ab9e55cd 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -1742,6 +1742,27 @@ VALUE rb_check_symbol(volatile VALUE *namep); (__builtin_constant_p(str) ? \ __extension__ (rb_intern2((str), (long)strlen(str))) : \ (rb_intern)(str)) + +# define rb_varargs_argc_check_runtime(argc, vargc) \ + (((argc) <= (vargc)) ? (argc) : \ + (rb_fatal("argc(%d) exceeds actual arguments(%d)", \ + argc, vargc), 0)) +# if defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P) +# if HAVE_ATTRIBUTE_ERRORFUNC +ERRORFUNC((" argument length doesn't match"), int rb_varargs_bad_length(int,int)); +# else +# define rb_varargs_bad_length(argc, vargc) ((argc)/((argc) == (vargc))) +# endif +# define rb_varargs_argc_check(argc, vargc) \ + __builtin_choose_expr(__builtin_constant_p(argc), \ + (((argc) == (vargc)) ? (argc) : \ + rb_varargs_bad_length(argc, vargc)), \ + rb_varargs_argc_check_runtime(argc, vargc)) +# else +# define rb_varargs_argc_check(argc, vargc) \ + rb_varargs_argc_check_runtime(argc, vargc) +# endif + #else #define rb_intern_const(str) rb_intern2((str), (long)strlen(str)) #endif @@ -2418,6 +2439,19 @@ rb_scan_args_set(int argc, const VALUE *argv, } #endif +#if defined(__GNUC__) && defined(__OPTIMIZE__) +# define rb_yield_values(argc, ...) \ +__extension__({ \ + const int rb_yield_values_argc = (argc); \ + const VALUE rb_yield_values_args[] = {__VA_ARGS__}; \ + const int rb_yield_values_nargs = \ + (int)(sizeof(rb_yield_values_args) / sizeof(VALUE)); \ + rb_yield_values2( \ + rb_varargs_argc_check(rb_yield_values_argc, rb_yield_values_nargs), \ + rb_yield_values_args); \ + }) +#endif + #ifndef RUBY_DONT_SUBST #include "ruby/subst.h" #endif diff --git a/vm_eval.c b/vm_eval.c index ed9463da79..d2a1d9754d 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -1023,6 +1023,7 @@ rb_yield(VALUE val) } } +#undef rb_yield_values VALUE rb_yield_values(int n, ...) {