#36713 closed enhancement (duplicate)
Check the number of arguments before calling call_user_func_array()
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.6 |
Component: | Plugins | Keywords: | |
Focuses: | Cc: |
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)
Note: See
TracTickets for help on using
tickets.
There's a patch on #14671 that does that.