From d60fe198be1f85e694d6684a10a0912834b2c6f2 Mon Sep 17 00:00:00 2001 From: juan-morales Date: Sat, 23 Mar 2024 13:58:34 -0300 Subject: [PATCH 1/4] QA - Fix test - filter_input_array - FILTER_NULL_ON_FAILURE --- ext/filter/tests/filter_input_array_001.phpt | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ext/filter/tests/filter_input_array_001.phpt 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..fc91d3eec6bb2 --- /dev/null +++ b/ext/filter/tests/filter_input_array_001.phpt @@ -0,0 +1,27 @@ +--TEST-- +filter_input_array: test FILTER_NULL_ON_FAILURE option +--EXTENSIONS-- +filter +--GET-- +b="test" +--FILE-- + [ + "flags" => FILTER_NULL_ON_FAILURE, + "filter" => FILTER_VALIDATE_BOOLEAN, + ], + "b" => [ + "filter" => FILTER_VALIDATE_BOOLEAN, + ] + ]; + + var_dump(filter_input_array(INPUT_GET, $args, true)); +?> +--EXPECT-- +array(2) { + ["a"]=> + NULL + ["b"]=> + bool(false) +} From 51dd45b81b5e76f4dfc7061f9941ae28b1c2626a Mon Sep 17 00:00:00 2001 From: juan-morales Date: Mon, 25 Mar 2024 16:09:12 -0300 Subject: [PATCH 2/4] filter_input_array solution 1 - extra argument --- ext/filter/filter.c | 15 ++++---------- ext/filter/filter.stub.php | 2 +- ext/filter/filter_arginfo.h | 3 ++- ext/filter/tests/filter_input_array_001.phpt | 21 ++++++++------------ 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/ext/filter/filter.c b/ext/filter/filter.c index b787ef706c4f2..b687c41969308 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 || (op_long & FILTER_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 index fc91d3eec6bb2..06af52eceb02d 100644 --- a/ext/filter/tests/filter_input_array_001.phpt +++ b/ext/filter/tests/filter_input_array_001.phpt @@ -2,26 +2,21 @@ filter_input_array: test FILTER_NULL_ON_FAILURE option --EXTENSIONS-- filter ---GET-- -b="test" --FILE-- [ + "c" => [ "flags" => FILTER_NULL_ON_FAILURE, - "filter" => FILTER_VALIDATE_BOOLEAN, - ], - "b" => [ - "filter" => FILTER_VALIDATE_BOOLEAN, ] ]; + 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-- -array(2) { - ["a"]=> - NULL - ["b"]=> - bool(false) -} +bool(false) +NULL +bool(false) +NULL \ No newline at end of file From 359c7186c0de5f502775ca582b1886531b3affc0 Mon Sep 17 00:00:00 2001 From: juan-morales Date: Mon, 25 Mar 2024 17:22:03 -0300 Subject: [PATCH 3/4] - --- ext/filter/filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/filter/filter.c b/ext/filter/filter.c index b687c41969308..0f5f921e3215f 100644 --- a/ext/filter/filter.c +++ b/ext/filter/filter.c @@ -701,7 +701,7 @@ PHP_FUNCTION(filter_input_array) * 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 (null_on_failure || (op_long & FILTER_NULL_ON_FAILURE)) { + if (null_on_failure) { RETURN_FALSE; } else { RETURN_NULL(); From c6905beb1c1332013f205798b7b99e1064790c33 Mon Sep 17 00:00:00 2001 From: juan-morales Date: Mon, 25 Mar 2024 18:04:51 -0300 Subject: [PATCH 4/4] remove indentation --- ext/filter/tests/filter_input_array_001.phpt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ext/filter/tests/filter_input_array_001.phpt b/ext/filter/tests/filter_input_array_001.phpt index 06af52eceb02d..44dd3938807ad 100644 --- a/ext/filter/tests/filter_input_array_001.phpt +++ b/ext/filter/tests/filter_input_array_001.phpt @@ -4,16 +4,16 @@ filter_input_array: test FILTER_NULL_ON_FAILURE option filter --FILE-- [ - "flags" => FILTER_NULL_ON_FAILURE, - ] - ]; +$args = [ + "c" => [ + "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)); +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)