diff --git a/ext/standard/array.c b/ext/standard/array.c index a2fef8b145283..2282c3f8ec8af 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -6500,7 +6500,7 @@ PHP_FUNCTION(array_filter) zval args[2]; zval retval; bool have_callback = 0; - zend_long use_type = 0; + zend_long mode = ARRAY_FILTER_USE_VALUE; zend_string *string_key; zend_fcall_info fci = empty_fcall_info; zend_fcall_info_cache fci_cache = empty_fcall_info_cache; @@ -6510,19 +6510,30 @@ PHP_FUNCTION(array_filter) Z_PARAM_ARRAY(array) Z_PARAM_OPTIONAL Z_PARAM_FUNC_OR_NULL(fci, fci_cache) - Z_PARAM_LONG(use_type) + Z_PARAM_LONG(mode) ZEND_PARSE_PARAMETERS_END(); + switch (mode) { + case ARRAY_FILTER_USE_VALUE: + case ARRAY_FILTER_USE_BOTH: + case ARRAY_FILTER_USE_KEY: + break; + default: + zend_argument_value_error(3, "must be a valid mode"); + RETURN_THROWS(); + } + if (zend_hash_num_elements(Z_ARRVAL_P(array)) == 0) { RETVAL_EMPTY_ARRAY(); return; } + array_init(return_value); if (ZEND_FCI_INITIALIZED(fci)) { have_callback = 1; fci.retval = &retval; - if (use_type == ARRAY_FILTER_USE_BOTH) { + if (mode == ARRAY_FILTER_USE_BOTH) { fci.param_count = 2; key = &args[1]; } else { @@ -6533,7 +6544,7 @@ PHP_FUNCTION(array_filter) ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, string_key, operand) { if (have_callback) { - if (use_type) { + if (mode) { /* Set up the key */ if (!string_key) { ZVAL_LONG(key, num_key); @@ -6541,7 +6552,7 @@ PHP_FUNCTION(array_filter) ZVAL_STR_COPY(key, string_key); } } - if (use_type != ARRAY_FILTER_USE_KEY) { + if (mode != ARRAY_FILTER_USE_KEY) { ZVAL_COPY(&args[0], operand); } fci.params = args; @@ -6550,7 +6561,7 @@ PHP_FUNCTION(array_filter) bool retval_true; zval_ptr_dtor(&args[0]); - if (use_type == ARRAY_FILTER_USE_BOTH) { + if (mode == ARRAY_FILTER_USE_BOTH) { zval_ptr_dtor(&args[1]); } retval_true = zend_is_true(&retval); @@ -6560,7 +6571,7 @@ PHP_FUNCTION(array_filter) } } else { zval_ptr_dtor(&args[0]); - if (use_type == ARRAY_FILTER_USE_BOTH) { + if (mode == ARRAY_FILTER_USE_BOTH) { zval_ptr_dtor(&args[1]); } return; diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index 2a35af6038083..2205082e91dfd 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -59,6 +59,7 @@ PHPAPI bool php_array_pick_keys(php_random_algo_with_state engine, zval *input, #define PHP_COUNT_NORMAL 0 #define PHP_COUNT_RECURSIVE 1 +#define ARRAY_FILTER_USE_VALUE 0 #define ARRAY_FILTER_USE_BOTH 1 #define ARRAY_FILTER_USE_KEY 2 diff --git a/ext/standard/tests/array/array_filter_invalid_mode.phpt b/ext/standard/tests/array/array_filter_invalid_mode.phpt new file mode 100644 index 0000000000000..06b0e1a058ef5 --- /dev/null +++ b/ext/standard/tests/array/array_filter_invalid_mode.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test array_filter() function : usage variations - mode exception +--FILE-- +getMessage(), "\n"; +} + +echo "Done" +?> +--EXPECT-- +ValueError: array_filter(): Argument #3 ($mode) must be a valid mode +Done