WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#36713 closed enhancement (duplicate)

Check the number of arguments before calling call_user_func_array()

Reported by: lgedeon Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.6
Component: Plugins Keywords:
Focuses: Cc:
PR Number:

Description

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
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.

Change History (2)

#1 @swissspidy
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

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.

There's a patch on #14671 that does that.

#2 @SergeyBiryukov
3 years ago

  • Component changed from General to Plugins
Note: See TracTickets for help on using tickets.