Skip to content

[On Hold - Ignore] QA - Bug fix - filter input array - solution 1 #13803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions ext/filter/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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)) {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion ext/filter/filter.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
3 changes: 2 additions & 1 deletion ext/filter/filter_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions ext/filter/tests/filter_input_array_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
filter_input_array: test FILTER_NULL_ON_FAILURE option
--EXTENSIONS--
filter
--FILE--
<?php
$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));
?>
--EXPECT--
bool(false)
NULL
bool(false)
NULL