diff --git a/ext/filter/filter.c b/ext/filter/filter.c index b787ef706c4f2..0f5f921e3215f 100644 --- a/ext/filter/filter.c +++ b/ext/filter/filter.c @@ -548,7 +548,6 @@ static void php_filter_array_handler(zval *input, HashTable *op_ht, zend_long op php_filter_call(return_value, -1, NULL, op_long, 0, FILTER_REQUIRE_ARRAY); } else { array_init(return_value); - ZEND_HASH_FOREACH_STR_KEY_VAL(op_ht, arg_key, arg_elm) { if (arg_key == NULL) { zend_argument_type_error(2, "must contain only string keys"); @@ -674,14 +673,16 @@ PHP_FUNCTION(filter_input_array) zend_long fetch_from; zval *array_input = NULL; bool add_empty = 1; + bool null_on_failure = 0; HashTable *op_ht = NULL; zend_long op_long = FILTER_DEFAULT; - ZEND_PARSE_PARAMETERS_START(1, 3) + ZEND_PARSE_PARAMETERS_START(1, 4) Z_PARAM_LONG(fetch_from) Z_PARAM_OPTIONAL Z_PARAM_ARRAY_HT_OR_LONG(op_ht, op_long) Z_PARAM_BOOL(add_empty) + Z_PARAM_BOOL(null_on_failure) ZEND_PARSE_PARAMETERS_END(); if (!op_ht && !PHP_FILTER_ID_EXISTS(op_long)) { @@ -695,20 +696,12 @@ PHP_FUNCTION(filter_input_array) } if (!array_input) { - zend_long filter_flags = 0; - zval *option; - if (op_long) { - filter_flags = op_long; - } else if (op_ht && (option = zend_hash_str_find(op_ht, "flags", sizeof("flags") - 1)) != NULL) { - filter_flags = zval_get_long(option); - } - /* The FILTER_NULL_ON_FAILURE flag inverts the usual return values of * the function: normally when validation fails false is returned, and * when the input value doesn't exist NULL is returned. With the flag * set, NULL and false should be returned, respectively. Ergo, although * the code below looks incorrect, it's actually right. */ - if (filter_flags & FILTER_NULL_ON_FAILURE) { + if (null_on_failure) { RETURN_FALSE; } else { RETURN_NULL(); diff --git a/ext/filter/filter.stub.php b/ext/filter/filter.stub.php index 030de50f51890..4c3712b800a29 100644 --- a/ext/filter/filter.stub.php +++ b/ext/filter/filter.stub.php @@ -301,7 +301,7 @@ function filter_input(int $type, string $var_name, int $filter = FILTER_DEFAULT, function filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed {} /** @refcount 1 */ -function filter_input_array(int $type, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null {} +function filter_input_array(int $type, array|int $options = FILTER_DEFAULT, bool $add_empty = true, bool $null_on_failure = true): array|false|null {} /** @refcount 1 */ function filter_var_array(array $array, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null {} diff --git a/ext/filter/filter_arginfo.h b/ext/filter/filter_arginfo.h index a05806c5e1201..55a8f3583d4c4 100644 --- a/ext/filter/filter_arginfo.h +++ b/ext/filter/filter_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: c3f3240137eaa89316276920acf35f975b2dd8f9 */ + * Stub hash: 10c31cbd4046384683842cd79d4c6724eab74ce8 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_has_var, 0, 2, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, input_type, IS_LONG, 0) @@ -23,6 +23,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filter_input_array, 0, 1, MAY_BE ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0) ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG, "FILTER_DEFAULT") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, add_empty, _IS_BOOL, 0, "true") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, null_on_failure, _IS_BOOL, 0, "true") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filter_var_array, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE|MAY_BE_NULL) diff --git a/ext/filter/tests/filter_input_array_001.phpt b/ext/filter/tests/filter_input_array_001.phpt new file mode 100644 index 0000000000000..44dd3938807ad --- /dev/null +++ b/ext/filter/tests/filter_input_array_001.phpt @@ -0,0 +1,22 @@ +--TEST-- +filter_input_array: test FILTER_NULL_ON_FAILURE option +--EXTENSIONS-- +filter +--FILE-- + [ + "flags" => FILTER_NULL_ON_FAILURE, + ] +]; + +var_dump(filter_input_array(INPUT_GET, $args, true, true)); +var_dump(filter_input_array(INPUT_GET, $args, true)); +var_dump(filter_input_array(INPUT_GET, FILTER_DEFAULT, true, true)); +var_dump(filter_input_array(INPUT_GET, FILTER_DEFAULT, true)); +?> +--EXPECT-- +bool(false) +NULL +bool(false) +NULL \ No newline at end of file