[wp-trac] [WordPress Trac] #36713: Check the number of arguments before calling call_user_func_array()

WordPress Trac noreply at wordpress.org
Fri Apr 29 01:20:07 UTC 2016


#36713: Check the number of arguments before calling call_user_func_array()
-------------------------+-----------------------------
 Reporter:  lgedeon      |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 If you do not send enough args to an action, filter or callback, WordPress
 implodes. We can prevent this by checking the function we are about to
 call to see how many args it requires.

 Here is concept code that I will convert to a patch if the idea is
 approved:

 {{{#!php
 <?php
 function helper_count_required_args ( $function ) {
         static $arg_counts = array();

         $key = is_scalar( $function ) ? $function : serialize( $function
 );

         if ( isset( $arg_counts[$key] ) ) {
                 echo "\n<br>arg_counts: ";var_export($arg_counts);

                 return $arg_counts[$key];
         }

         if ( is_string( $function ) && function_exists( $function ) ) {
                 $r = new ReflectionFunction( $function );
         } elseif ( isset( $function[0], $function[1] ) && method_exists(
 $function[0], $function[1] ) ) {
                 $r = new ReflectionMethod( $function[0], $function[1] );
         } else {
                 return $arg_counts[$key] = false;
         }

         return $arg_counts[$key] = $r->getNumberOfRequiredParameters();
 }
 }}}

 One concern I can see with this is that it will slow things down a bit,
 but at the same time it might prevent a few problems.

 To keep down overhead, I suggest we do the check when the hook is added
 not when it is run. We could even increase the fourth parameter
 ($accepted_args) automatically if that is what the function really
 requires.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/36713>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list