drop-in type check for rb_define_global_function

We can check the function pointer passed to rb_define_global_function
like we do so in rb_define_method.  It turns out that almost anybody
is misunderstanding the API.
This commit is contained in:
卜部昌平 2019-08-28 18:19:11 +09:00
parent 7b6fde4258
commit 7bcfd9189a
13 changed files with 140 additions and 38 deletions

View File

@ -1767,7 +1767,9 @@ rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS)
rb_define_singleton_method(module, name, func, argc);
}
#ifdef rb_define_global_function
#undef rb_define_global_function
#endif
/*!
* Defines a global function
* \param name name of the function

40
eval.c
View File

@ -768,6 +768,12 @@ rb_f_raise(int argc, VALUE *argv)
UNREACHABLE_RETURN(Qnil);
}
static VALUE
f_raise(int c, VALUE *v, VALUE _)
{
return rb_f_raise(c, v);
}
static VALUE
make_exception(int argc, const VALUE *argv, int isstr)
{
@ -1883,7 +1889,7 @@ errat_setter(VALUE val, ID id, VALUE *var)
*/
static VALUE
rb_f_method_name(void)
rb_f_method_name(VALUE _)
{
ID fname = prev_frame_func(); /* need *method* ID */
@ -1905,7 +1911,7 @@ rb_f_method_name(void)
*/
static VALUE
rb_f_callee_name(void)
rb_f_callee_name(VALUE _)
{
ID fname = prev_frame_callee(); /* need *callee* ID */
@ -1928,7 +1934,7 @@ rb_f_callee_name(void)
*
*/
static VALUE
f_current_dirname(void)
f_current_dirname(VALUE _)
{
VALUE base = rb_current_realfilepath();
if (NIL_P(base)) {
@ -1938,16 +1944,34 @@ f_current_dirname(void)
return base;
}
static VALUE
f_global_variables(VALUE _)
{
return rb_f_global_variables();
}
static VALUE
f_trace_var(int c, const VALUE *a, VALUE _)
{
return rb_f_trace_var(c, a);
}
static VALUE
f_untrace_var(int c, const VALUE *a, VALUE _)
{
return rb_f_untrace_var(c, a);
}
void
Init_eval(void)
{
rb_define_virtual_variable("$@", errat_getter, errat_setter);
rb_define_virtual_variable("$!", errinfo_getter, 0);
rb_define_global_function("raise", rb_f_raise, -1);
rb_define_global_function("fail", rb_f_raise, -1);
rb_define_global_function("raise", f_raise, -1);
rb_define_global_function("fail", f_raise, -1);
rb_define_global_function("global_variables", rb_f_global_variables, 0); /* in variable.c */
rb_define_global_function("global_variables", f_global_variables, 0);
rb_define_global_function("__method__", rb_f_method_name, 0);
rb_define_global_function("__callee__", rb_f_callee_name, 0);
@ -1980,8 +2004,8 @@ Init_eval(void)
rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
rb_define_global_function("trace_var", rb_f_trace_var, -1); /* in variable.c */
rb_define_global_function("untrace_var", rb_f_untrace_var, -1); /* in variable.c */
rb_define_global_function("trace_var", f_trace_var, -1);
rb_define_global_function("untrace_var", f_untrace_var, -1);
rb_vm_register_special_exception(ruby_error_reenter, rb_eFatal, "exception reentered");
rb_vm_register_special_exception(ruby_error_stackfatal, rb_eFatal, "machine stack overflow in critical region");

View File

@ -35,7 +35,7 @@ rb_call_end_proc(VALUE data)
*/
static VALUE
rb_f_at_exit(void)
rb_f_at_exit(VALUE _)
{
VALUE proc;

2
file.c
View File

@ -5225,7 +5225,7 @@ test_check(int n, int argc, VALUE *argv)
*/
static VALUE
rb_f_test(int argc, VALUE *argv)
rb_f_test(int argc, VALUE *argv, VALUE _)
{
int cmd;

View File

@ -2774,6 +2774,47 @@ __attribute__((__unused__,__weakref__("rb_define_module_function"),__nonnull__(2
#define rb_define_module_function_choose_prototypem2(n) rb_define_method_if_constexpr((n)==-2,rb_define_module_functionm2,rb_define_module_function_choose_prototypem1(n))
#define rb_define_module_function_choose_prototypem3(n, f) rb_define_method_if_constexpr(rb_f_notimplement_p(f),rb_define_module_functionm3,rb_define_module_function_choose_prototypem2(n))
#define rb_define_module_function(klass, mid, func, arity) rb_define_module_function_choose_prototypem3((arity),(func))((klass),(mid),(func),(arity));
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_functionm3(const char*,VALUE(*)(ANYARGS),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_functionm2(const char*,VALUE(*)(VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_functionm1(const char*,VALUE(*)(int,union __attribute__((__transparent_union__)){VALUE*x;const VALUE*y;},VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function0 (const char*,VALUE(*)(VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function1 (const char*,VALUE(*)(VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function2 (const char*,VALUE(*)(VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function3 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function4 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function5 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function6 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function7 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function8 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function9 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function10(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function11(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function12(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function13(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function14(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function15(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int);
#define rb_define_global_function_choose_prototype15(n) rb_define_method_if_constexpr((n)==15,rb_define_global_function15,rb_define_global_functionm3)
#define rb_define_global_function_choose_prototype14(n) rb_define_method_if_constexpr((n)==14,rb_define_global_function14,rb_define_global_function_choose_prototype15(n))
#define rb_define_global_function_choose_prototype13(n) rb_define_method_if_constexpr((n)==13,rb_define_global_function13,rb_define_global_function_choose_prototype14(n))
#define rb_define_global_function_choose_prototype12(n) rb_define_method_if_constexpr((n)==12,rb_define_global_function12,rb_define_global_function_choose_prototype13(n))
#define rb_define_global_function_choose_prototype11(n) rb_define_method_if_constexpr((n)==11,rb_define_global_function11,rb_define_global_function_choose_prototype12(n))
#define rb_define_global_function_choose_prototype10(n) rb_define_method_if_constexpr((n)==10,rb_define_global_function10,rb_define_global_function_choose_prototype11(n))
#define rb_define_global_function_choose_prototype9(n) rb_define_method_if_constexpr((n)== 9,rb_define_global_function9, rb_define_global_function_choose_prototype10(n))
#define rb_define_global_function_choose_prototype8(n) rb_define_method_if_constexpr((n)== 8,rb_define_global_function8, rb_define_global_function_choose_prototype9(n))
#define rb_define_global_function_choose_prototype7(n) rb_define_method_if_constexpr((n)== 7,rb_define_global_function7, rb_define_global_function_choose_prototype8(n))
#define rb_define_global_function_choose_prototype6(n) rb_define_method_if_constexpr((n)== 6,rb_define_global_function6, rb_define_global_function_choose_prototype7(n))
#define rb_define_global_function_choose_prototype5(n) rb_define_method_if_constexpr((n)== 5,rb_define_global_function5, rb_define_global_function_choose_prototype6(n))
#define rb_define_global_function_choose_prototype4(n) rb_define_method_if_constexpr((n)== 4,rb_define_global_function4, rb_define_global_function_choose_prototype5(n))
#define rb_define_global_function_choose_prototype3(n) rb_define_method_if_constexpr((n)== 3,rb_define_global_function3, rb_define_global_function_choose_prototype4(n))
#define rb_define_global_function_choose_prototype2(n) rb_define_method_if_constexpr((n)== 2,rb_define_global_function2, rb_define_global_function_choose_prototype3(n))
#define rb_define_global_function_choose_prototype1(n) rb_define_method_if_constexpr((n)== 1,rb_define_global_function1, rb_define_global_function_choose_prototype2(n))
#define rb_define_global_function_choose_prototype0(n) rb_define_method_if_constexpr((n)== 0,rb_define_global_function0, rb_define_global_function_choose_prototype1(n))
#define rb_define_global_function_choose_prototypem1(n) rb_define_method_if_constexpr((n)==-1,rb_define_global_functionm1,rb_define_global_function_choose_prototype0(n))
#define rb_define_global_function_choose_prototypem2(n) rb_define_method_if_constexpr((n)==-2,rb_define_global_functionm2,rb_define_global_function_choose_prototypem1(n))
#define rb_define_global_function_choose_prototypem3(n, f) rb_define_method_if_constexpr(rb_f_notimplement_p(f),rb_define_global_functionm3,rb_define_global_function_choose_prototypem2(n))
#define rb_define_global_function(mid, func, arity) rb_define_global_function_choose_prototypem3((arity),(func))((mid),(func),(arity));
#endif
#endif

8
io.c
View File

@ -7183,7 +7183,7 @@ check_pipe_command(VALUE filename_or_command)
*/
static VALUE
rb_f_open(int argc, VALUE *argv)
rb_f_open(int argc, VALUE *argv, VALUE _)
{
ID to_open = 0;
int redirect = FALSE;
@ -7518,7 +7518,7 @@ rb_io_printf(int argc, const VALUE *argv, VALUE out)
*/
static VALUE
rb_f_printf(int argc, VALUE *argv)
rb_f_printf(int argc, VALUE *argv, VALUE _)
{
VALUE out;
@ -7619,7 +7619,7 @@ rb_io_print(int argc, const VALUE *argv, VALUE out)
*/
static VALUE
rb_f_print(int argc, const VALUE *argv)
rb_f_print(int argc, const VALUE *argv, VALUE _)
{
rb_io_print(argc, argv, rb_stdout);
return Qnil;
@ -10007,7 +10007,7 @@ rb_io_fcntl(int argc, VALUE *argv, VALUE io)
*/
static VALUE
rb_f_syscall(int argc, VALUE *argv)
rb_f_syscall(int argc, VALUE *argv, VALUE _)
{
VALUE arg[8];
#if SIZEOF_VOIDP == 8 && defined(HAVE___SYSCALL) && SIZEOF_INT != 8 /* mainly *BSD */

2
load.c
View File

@ -690,7 +690,7 @@ rb_load_protect(VALUE fname, int wrap, int *pstate)
*/
static VALUE
rb_f_load(int argc, VALUE *argv)
rb_f_load(int argc, VALUE *argv, VALUE _)
{
VALUE fname, wrap, path, orig_fname;

View File

@ -4048,6 +4048,12 @@ rb_obj_dig(int argc, VALUE *argv, VALUE obj, VALUE notfound)
return obj;
}
static VALUE
f_sprintf(int c, const VALUE *v, VALUE _)
{
return rb_f_sprintf(c, v);
}
/*
* Document-class: Class
*
@ -4299,8 +4305,8 @@ InitVM_Object(void)
rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */
rb_define_global_function("sprintf", f_sprintf, -1);
rb_define_global_function("format", f_sprintf, -1);
rb_define_global_function("Integer", rb_f_integer, -1);
rb_define_global_function("Float", rb_f_float, -1);

16
proc.c
View File

@ -836,6 +836,12 @@ rb_block_proc(void)
return proc_new(rb_cProc, FALSE);
}
static VALUE
f_proc(VALUE _)
{
return rb_block_proc();
}
/*
* call-seq:
* lambda { |...| block } -> a_proc
@ -850,6 +856,12 @@ rb_block_lambda(void)
return proc_new(rb_cProc, TRUE);
}
static VALUE
f_lambda(VALUE _)
{
return rb_block_lambda();
}
/* Document-method: Proc#===
*
* call-seq:
@ -3623,8 +3635,8 @@ Init_Proc(void)
rb_vm_register_special_exception(ruby_error_sysstack, rb_eSysStackError, "stack level too deep");
/* utility functions */
rb_define_global_function("proc", rb_block_proc, 0);
rb_define_global_function("lambda", rb_block_lambda, 0);
rb_define_global_function("proc", f_proc, 0);
rb_define_global_function("lambda", f_lambda, 0);
/* Method */
rb_cMethod = rb_define_class("Method", rb_cObject);

View File

@ -2981,6 +2981,12 @@ rb_f_exec(int argc, const VALUE *argv)
UNREACHABLE_RETURN(Qnil);
}
static VALUE
f_exec(int c, const VALUE *a, VALUE _)
{
return rb_f_exec(c, a);
}
#define ERRMSG(str) do { if (errmsg && 0 < errmsg_buflen) strlcpy(errmsg, (str), errmsg_buflen); } while (0)
#define ERRMSG1(str, a) do { if (errmsg && 0 < errmsg_buflen) snprintf(errmsg, errmsg_buflen, (str), (a)); } while (0)
#define ERRMSG2(str, a, b) do { if (errmsg && 0 < errmsg_buflen) snprintf(errmsg, errmsg_buflen, (str), (a), (b)); } while (0)
@ -4256,6 +4262,11 @@ rb_f_exit(int argc, const VALUE *argv)
UNREACHABLE_RETURN(Qnil);
}
static VALUE
f_exit(int c, const VALUE *a, VALUE _)
{
return rb_f_exit(c, a);
}
/*
* call-seq:
@ -4293,6 +4304,12 @@ rb_f_abort(int argc, const VALUE *argv)
UNREACHABLE_RETURN(Qnil);
}
static VALUE
f_abort(int c, const VALUE *a, VALUE _)
{
return rb_f_abort(c, a);
}
void
rb_syswait(rb_pid_t pid)
{
@ -4484,7 +4501,7 @@ rb_spawn(int argc, const VALUE *argv)
*/
static VALUE
rb_f_system(int argc, VALUE *argv)
rb_f_system(int argc, VALUE *argv, VALUE _)
{
/*
* n.b. using alloca for now to simplify future Thread::Light code
@ -4811,7 +4828,7 @@ rb_f_system(int argc, VALUE *argv)
*/
static VALUE
rb_f_spawn(int argc, VALUE *argv)
rb_f_spawn(int argc, VALUE *argv, VALUE _)
{
rb_pid_t pid;
char errmsg[CHILD_ERRMSG_BUFLEN] = { '\0' };
@ -4855,7 +4872,7 @@ rb_f_spawn(int argc, VALUE *argv)
*/
static VALUE
rb_f_sleep(int argc, VALUE *argv)
rb_f_sleep(int argc, VALUE *argv, VALUE _)
{
time_t beg, end;
@ -8092,14 +8109,14 @@ InitVM_process(void)
#define rb_intern(str) rb_intern_const(str)
rb_define_virtual_variable("$?", get_CHILD_STATUS, 0);
rb_define_virtual_variable("$$", get_PROCESS_ID, 0);
rb_define_global_function("exec", rb_f_exec, -1);
rb_define_global_function("exec", f_exec, -1);
rb_define_global_function("fork", rb_f_fork, 0);
rb_define_global_function("exit!", rb_f_exit_bang, -1);
rb_define_global_function("system", rb_f_system, -1);
rb_define_global_function("spawn", rb_f_spawn, -1);
rb_define_global_function("sleep", rb_f_sleep, -1);
rb_define_global_function("exit", rb_f_exit, -1);
rb_define_global_function("abort", rb_f_abort, -1);
rb_define_global_function("exit", f_exit, -1);
rb_define_global_function("abort", f_abort, -1);
rb_mProcess = rb_define_module("Process");
@ -8118,12 +8135,12 @@ InitVM_process(void)
rb_define_const(rb_mProcess, "WUNTRACED", INT2FIX(0));
#endif
rb_define_singleton_method(rb_mProcess, "exec", rb_f_exec, -1);
rb_define_singleton_method(rb_mProcess, "exec", f_exec, -1);
rb_define_singleton_method(rb_mProcess, "fork", rb_f_fork, 0);
rb_define_singleton_method(rb_mProcess, "spawn", rb_f_spawn, -1);
rb_define_singleton_method(rb_mProcess, "exit!", rb_f_exit_bang, -1);
rb_define_singleton_method(rb_mProcess, "exit", rb_f_exit, -1);
rb_define_singleton_method(rb_mProcess, "abort", rb_f_abort, -1);
rb_define_singleton_method(rb_mProcess, "exit", f_exit, -1);
rb_define_singleton_method(rb_mProcess, "abort", f_abort, -1);
rb_define_singleton_method(rb_mProcess, "last_status", proc_s_last_status, 0);
rb_define_module_function(rb_mProcess, "kill", proc_rb_f_kill, -1);

8
ruby.c
View File

@ -1484,7 +1484,7 @@ uscore_get(void)
*/
static VALUE
rb_f_sub(int argc, VALUE *argv)
rb_f_sub(int argc, VALUE *argv, VALUE _)
{
VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("sub"), argc, argv);
rb_lastline_set(str);
@ -1503,7 +1503,7 @@ rb_f_sub(int argc, VALUE *argv)
*/
static VALUE
rb_f_gsub(int argc, VALUE *argv)
rb_f_gsub(int argc, VALUE *argv, VALUE _)
{
VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("gsub"), argc, argv);
rb_lastline_set(str);
@ -1521,7 +1521,7 @@ rb_f_gsub(int argc, VALUE *argv)
*/
static VALUE
rb_f_chop(void)
rb_f_chop(VALUE _)
{
VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chop"), 0, 0);
rb_lastline_set(str);
@ -1541,7 +1541,7 @@ rb_f_chop(void)
*/
static VALUE
rb_f_chomp(int argc, VALUE *argv)
rb_f_chomp(int argc, VALUE *argv, VALUE _)
{
VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chomp"), argc, argv);
rb_lastline_set(str);

View File

@ -1007,7 +1007,7 @@ rb_vm_thread_backtrace_locations(int argc, const VALUE *argv, VALUE thval)
*/
static VALUE
rb_f_caller(int argc, VALUE *argv)
rb_f_caller(int argc, VALUE *argv, VALUE _)
{
return ec_backtrace_to_ary(GET_EC(), argc, argv, 1, 1, 1);
}
@ -1035,7 +1035,7 @@ rb_f_caller(int argc, VALUE *argv)
* entries within the specified range.
*/
static VALUE
rb_f_caller_locations(int argc, VALUE *argv)
rb_f_caller_locations(int argc, VALUE *argv, VALUE _)
{
return ec_backtrace_to_ary(GET_EC(), argc, argv, 1, 1, 0);
}

View File

@ -1880,7 +1880,7 @@ uncaught_throw_to_s(VALUE exc)
*/
static VALUE
rb_f_throw(int argc, VALUE *argv)
rb_f_throw(int argc, VALUE *argv, VALUE _)
{
VALUE tag, value;
@ -2085,7 +2085,7 @@ local_var_list_add(const struct local_var_list *vars, ID lid)
*/
static VALUE
rb_f_local_variables(void)
rb_f_local_variables(VALUE _)
{
struct local_var_list vars;
rb_execution_context_t *ec = GET_EC();
@ -2142,7 +2142,7 @@ rb_f_local_variables(void)
static VALUE
rb_f_block_given_p(void)
rb_f_block_given_p(VALUE _)
{
rb_execution_context_t *ec = GET_EC();
rb_control_frame_t *cfp = ec->cfp;