Skip to content

Commit 4281daa

Browse files
committed
Fix default values in ext/filter
1 parent 5907070 commit 4281daa

File tree

6 files changed

+61
-68
lines changed

6 files changed

+61
-68
lines changed

ext/filter/filter.c

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,14 @@ PHP_FUNCTION(filter_has_var)
525525
/* }}} */
526526

527527
static void php_filter_call(
528-
zval *filtered, zend_long filter, HashTable *filter_args_ht, zend_long filter_args_long, zend_bool filter_args_is_null,
528+
zval *filtered, zend_long filter, HashTable *filter_args_ht, zend_long filter_args_long,
529529
const int copy, zend_long filter_flags
530530
) /* {{{ */ {
531531
zval *options = NULL;
532532
zval *option;
533533
char *charset = NULL;
534534

535-
if (!filter_args_is_null && !filter_args_ht) {
535+
if (filter_args_long) {
536536
zend_long lval = filter_args_long;
537537

538538
if (filter != -1) { /* handler for array apply */
@@ -545,7 +545,7 @@ static void php_filter_call(
545545
} else {
546546
filter = lval;
547547
}
548-
} else if (!filter_args_is_null) {
548+
} else if (filter_args_ht) {
549549
if ((option = zend_hash_str_find(filter_args_ht, "filter", sizeof("filter") - 1)) != NULL) {
550550
filter = zval_get_long(option);
551551
}
@@ -603,18 +603,18 @@ static void php_filter_call(
603603
}
604604
/* }}} */
605605

606-
static void php_filter_array_handler(zval *input, HashTable *op_ht, zend_long op_long, zend_bool op_is_null,
606+
static void php_filter_array_handler(zval *input, HashTable *op_ht, zend_long op_long,
607607
zval *return_value, zend_bool add_empty
608608
) /* {{{ */ {
609609
zend_string *arg_key;
610610
zval *tmp, *arg_elm;
611611

612-
if (op_is_null) {
612+
if (!op_long && !op_ht) {
613613
ZVAL_DUP(return_value, input);
614-
php_filter_call(return_value, FILTER_DEFAULT, NULL, 0, 1, 0, FILTER_REQUIRE_ARRAY);
615-
} else if (!op_ht) {
614+
php_filter_call(return_value, FILTER_DEFAULT, NULL, 0, 0, FILTER_REQUIRE_ARRAY);
615+
} else if (op_long) {
616616
ZVAL_DUP(return_value, input);
617-
php_filter_call(return_value, op_long, NULL, 0, 1, 0, FILTER_REQUIRE_ARRAY);
617+
php_filter_call(return_value, op_long, NULL, 0, 0, FILTER_REQUIRE_ARRAY);
618618
} else {
619619
array_init(return_value);
620620

@@ -638,7 +638,7 @@ static void php_filter_array_handler(zval *input, HashTable *op_ht, zend_long op
638638
php_filter_call(&nval, -1,
639639
Z_TYPE_P(arg_elm) == IS_ARRAY ? Z_ARRVAL_P(arg_elm) : NULL,
640640
Z_TYPE_P(arg_elm) == IS_ARRAY ? 0 : zval_get_long(arg_elm),
641-
Z_TYPE_P(arg_elm) == IS_NULL, 0, FILTER_REQUIRE_SCALAR
641+
0, FILTER_REQUIRE_SCALAR
642642
);
643643
zend_hash_update(Z_ARRVAL_P(return_value), arg_key, &nval);
644644
}
@@ -654,15 +654,14 @@ PHP_FUNCTION(filter_input)
654654
zval *input = NULL, *tmp;
655655
zend_string *var;
656656
HashTable *filter_args_ht = NULL;
657-
zend_long filter_args_long;
658-
zend_bool filter_args_is_null = 1;
657+
zend_long filter_args_long = 0;
659658

660659
ZEND_PARSE_PARAMETERS_START(2, 4)
661660
Z_PARAM_LONG(fetch_from)
662661
Z_PARAM_STR(var)
663662
Z_PARAM_OPTIONAL
664663
Z_PARAM_LONG(filter)
665-
Z_PARAM_ARRAY_HT_OR_LONG_OR_NULL(filter_args_ht, filter_args_long, filter_args_is_null)
664+
Z_PARAM_ARRAY_HT_OR_LONG(filter_args_ht, filter_args_long)
666665
ZEND_PARSE_PARAMETERS_END();
667666

668667
if (!PHP_FILTER_ID_EXISTS(filter)) {
@@ -677,21 +676,19 @@ PHP_FUNCTION(filter_input)
677676
if (!input || (tmp = zend_hash_find(Z_ARRVAL_P(input), var)) == NULL) {
678677
zend_long filter_flags = 0;
679678
zval *option, *opt, *def;
680-
if (!filter_args_is_null) {
681-
if (!filter_args_ht) {
682-
filter_flags = filter_args_long;
683-
} else {
684-
if ((option = zend_hash_str_find(filter_args_ht, "flags", sizeof("flags") - 1)) != NULL) {
685-
filter_flags = zval_get_long(option);
686-
}
679+
if (filter_args_long) {
680+
filter_flags = filter_args_long;
681+
} else if (filter_args_ht) {
682+
if ((option = zend_hash_str_find(filter_args_ht, "flags", sizeof("flags") - 1)) != NULL) {
683+
filter_flags = zval_get_long(option);
684+
}
687685

688-
if ((opt = zend_hash_str_find_deref(filter_args_ht, "options", sizeof("options") - 1)) != NULL &&
689-
Z_TYPE_P(opt) == IS_ARRAY &&
690-
(def = zend_hash_str_find_deref(Z_ARRVAL_P(opt), "default", sizeof("default") - 1)) != NULL
691-
) {
692-
ZVAL_COPY(return_value, def);
693-
return;
694-
}
686+
if ((opt = zend_hash_str_find_deref(filter_args_ht, "options", sizeof("options") - 1)) != NULL &&
687+
Z_TYPE_P(opt) == IS_ARRAY &&
688+
(def = zend_hash_str_find_deref(Z_ARRVAL_P(opt), "default", sizeof("default") - 1)) != NULL
689+
) {
690+
ZVAL_COPY(return_value, def);
691+
return;
695692
}
696693
}
697694

@@ -709,7 +706,7 @@ PHP_FUNCTION(filter_input)
709706

710707
ZVAL_DUP(return_value, tmp);
711708

712-
php_filter_call(return_value, filter, filter_args_ht, filter_args_long, filter_args_is_null, 1, FILTER_REQUIRE_SCALAR);
709+
php_filter_call(return_value, filter, filter_args_ht, filter_args_long, 1, FILTER_REQUIRE_SCALAR);
713710
}
714711
/* }}} */
715712

@@ -719,14 +716,13 @@ PHP_FUNCTION(filter_var)
719716
zend_long filter = FILTER_DEFAULT;
720717
zval *data;
721718
HashTable *filter_args_ht = NULL;
722-
zend_long filter_args_long;
723-
zend_bool filter_args_is_null = 1;
719+
zend_long filter_args_long = 0;
724720

725721
ZEND_PARSE_PARAMETERS_START(1, 3)
726722
Z_PARAM_ZVAL(data)
727723
Z_PARAM_OPTIONAL
728724
Z_PARAM_LONG(filter)
729-
Z_PARAM_ARRAY_HT_OR_LONG_OR_NULL(filter_args_ht, filter_args_long, filter_args_is_null)
725+
Z_PARAM_ARRAY_HT_OR_LONG(filter_args_ht, filter_args_long)
730726
ZEND_PARSE_PARAMETERS_END();
731727

732728
if (!PHP_FILTER_ID_EXISTS(filter)) {
@@ -735,7 +731,7 @@ PHP_FUNCTION(filter_var)
735731

736732
ZVAL_DUP(return_value, data);
737733

738-
php_filter_call(return_value, filter, filter_args_ht, filter_args_long, filter_args_is_null, 1, FILTER_REQUIRE_SCALAR);
734+
php_filter_call(return_value, filter, filter_args_ht, filter_args_long, 1, FILTER_REQUIRE_SCALAR);
739735
}
740736
/* }}} */
741737

@@ -746,17 +742,16 @@ PHP_FUNCTION(filter_input_array)
746742
zval *array_input = NULL;
747743
zend_bool add_empty = 1;
748744
HashTable *op_ht = NULL;
749-
zend_long op_long;
750-
zend_bool op_is_null = 1;
745+
zend_long op_long = 0;
751746

752747
ZEND_PARSE_PARAMETERS_START(1, 3)
753748
Z_PARAM_LONG(fetch_from)
754749
Z_PARAM_OPTIONAL
755-
Z_PARAM_ARRAY_HT_OR_LONG_OR_NULL(op_ht, op_long, op_is_null)
750+
Z_PARAM_ARRAY_HT_OR_LONG(op_ht, op_long)
756751
Z_PARAM_BOOL(add_empty)
757752
ZEND_PARSE_PARAMETERS_END();
758753

759-
if (!op_is_null && !op_ht && !PHP_FILTER_ID_EXISTS(op_long)) {
754+
if (op_long && !PHP_FILTER_ID_EXISTS(op_long)) {
760755
RETURN_FALSE;
761756
}
762757

@@ -768,12 +763,10 @@ PHP_FUNCTION(filter_input_array)
768763
if (!array_input) {
769764
zend_long filter_flags = 0;
770765
zval *option;
771-
if (!op_is_null) {
772-
if (!op_ht) {
773-
filter_flags = op_long;
774-
} else if ((option = zend_hash_str_find(op_ht, "flags", sizeof("flags") - 1)) != NULL) {
775-
filter_flags = zval_get_long(option);
776-
}
766+
if (op_long) {
767+
filter_flags = op_long;
768+
} else if (op_ht && (option = zend_hash_str_find(op_ht, "flags", sizeof("flags") - 1)) != NULL) {
769+
filter_flags = zval_get_long(option);
777770
}
778771

779772
/* The FILTER_NULL_ON_FAILURE flag inverts the usual return values of
@@ -788,7 +781,7 @@ PHP_FUNCTION(filter_input_array)
788781
}
789782
}
790783

791-
php_filter_array_handler(array_input, op_ht, op_long, op_is_null, return_value, add_empty);
784+
php_filter_array_handler(array_input, op_ht, op_long, return_value, add_empty);
792785
}
793786
/* }}} */
794787

@@ -798,21 +791,20 @@ PHP_FUNCTION(filter_var_array)
798791
zval *array_input = NULL;
799792
zend_bool add_empty = 1;
800793
HashTable *op_ht = NULL;
801-
zend_long op_long;
802-
zend_bool op_is_null = 1;
794+
zend_long op_long = 0;
803795

804796
ZEND_PARSE_PARAMETERS_START(1, 3)
805797
Z_PARAM_ARRAY(array_input)
806798
Z_PARAM_OPTIONAL
807-
Z_PARAM_ARRAY_HT_OR_LONG_OR_NULL(op_ht, op_long, op_is_null)
799+
Z_PARAM_ARRAY_HT_OR_LONG(op_ht, op_long)
808800
Z_PARAM_BOOL(add_empty)
809801
ZEND_PARSE_PARAMETERS_END();
810802

811-
if (!op_is_null && !op_ht && !PHP_FILTER_ID_EXISTS(op_long)) {
803+
if (op_long && !PHP_FILTER_ID_EXISTS(op_long)) {
812804
RETURN_FALSE;
813805
}
814806

815-
php_filter_array_handler(array_input, op_ht, op_long, op_is_null, return_value, add_empty);
807+
php_filter_array_handler(array_input, op_ht, op_long, return_value, add_empty);
816808
}
817809
/* }}} */
818810

ext/filter/filter.stub.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
function filter_has_var(int $type, string $variable_name): bool {}
66

7-
function filter_input(int $type, string $variable_name, int $filter = FILTER_DEFAULT, array|int|null $options = null): mixed {}
7+
function filter_input(int $type, string $variable_name, int $filter = FILTER_DEFAULT, array|int $options = []): mixed {}
88

9-
function filter_var(mixed $variable, int $filter = FILTER_DEFAULT, array|int|null $options = null): mixed {}
9+
function filter_var(mixed $variable, int $filter = FILTER_DEFAULT, array|int $options = []): mixed {}
1010

11-
function filter_input_array(int $type, array|int|null $options = null, bool $add_empty = true): array|false|null {}
11+
function filter_input_array(int $type, array|int $options = [], bool $add_empty = true): array|false|null {}
1212

13-
function filter_var_array(array $data, array|int|null $options = null, bool $add_empty = true): array|false|null {}
13+
function filter_var_array(array $data, array|int $options = [], bool $add_empty = true): array|false|null {}
1414

1515
function filter_list(): array {}
1616

ext/filter/filter_arginfo.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 6e5d5380e10a17df23769d1e8f04778143252827 */
2+
* Stub hash: dfd87cd01b448776214514a5fb5e25bb0de9d1b5 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_has_var, 0, 2, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
@@ -10,24 +10,24 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_input, 0, 2, IS_MIXED, 0)
1010
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
1111
ZEND_ARG_TYPE_INFO(0, variable_name, IS_STRING, 0)
1212
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filter, IS_LONG, 0, "FILTER_DEFAULT")
13-
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG|MAY_BE_NULL, "null")
13+
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG, "[]")
1414
ZEND_END_ARG_INFO()
1515

1616
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_var, 0, 1, IS_MIXED, 0)
1717
ZEND_ARG_TYPE_INFO(0, variable, IS_MIXED, 0)
1818
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filter, IS_LONG, 0, "FILTER_DEFAULT")
19-
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG|MAY_BE_NULL, "null")
19+
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG, "[]")
2020
ZEND_END_ARG_INFO()
2121

2222
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filter_input_array, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE|MAY_BE_NULL)
2323
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
24-
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG|MAY_BE_NULL, "null")
24+
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG, "[]")
2525
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, add_empty, _IS_BOOL, 0, "true")
2626
ZEND_END_ARG_INFO()
2727

2828
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filter_var_array, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE|MAY_BE_NULL)
2929
ZEND_ARG_TYPE_INFO(0, data, IS_ARRAY, 0)
30-
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG|MAY_BE_NULL, "null")
30+
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG, "[]")
3131
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, add_empty, _IS_BOOL, 0, "true")
3232
ZEND_END_ARG_INFO()
3333

ext/filter/tests/011.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ echo "Done\n";
3030
string(4) "test"
3131
string(18) "http://example.com"
3232
string(27) "<b>test</b>"
33-
filter_input(): Argument #4 ($options) must be of type array|int|null, stdClass given
33+
filter_input(): Argument #4 ($options) must be of type array|int, stdClass given
3434
string(6) "string"
3535
float(12345.7)
3636
string(29) "<p>string</p>"

ext/filter/tests/039.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ array(2) {
9292
}
9393
bool(false)
9494
bool(false)
95-
filter_var_array(): Argument #2 ($options) must be of type array|int|null, string given
95+
filter_var_array(): Argument #2 ($options) must be of type array|int, string given
9696
-- (2)
9797
bool(false)
9898
bool(false)
99-
filter_var_array(): Argument #2 ($options) must be of type array|int|null, string given
99+
filter_var_array(): Argument #2 ($options) must be of type array|int, string given
100100
-- (3)
101101
bool(false)
102102
bool(false)
103-
filter_var_array(): Argument #2 ($options) must be of type array|int|null, string given
103+
filter_var_array(): Argument #2 ($options) must be of type array|int, string given
104104
-- (4)
105-
filter_var_array(): Argument #2 ($options) must be of type array|int|null, stdClass given
105+
filter_var_array(): Argument #2 ($options) must be of type array|int, stdClass given
106106
array(0) {
107107
}
108108
array(1) {
@@ -132,7 +132,7 @@ array(1) {
132132
string(0) ""
133133
}
134134
int(100000)
135-
filter_var_array(): Argument #2 ($options) must be of type array|int|null, string given
135+
filter_var_array(): Argument #2 ($options) must be of type array|int, string given
136136
array(1) {
137137
[""]=>
138138
string(0) ""

ext/filter/tests/057.phpt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ array(0) {
2424
}
2525
bool(false)
2626
bool(false)
27+
NULL
28+
array(0) {
29+
}
2730
bool(false)
2831
bool(false)
29-
bool(false)
30-
bool(false)
31-
filter_input_array(): Argument #2 ($options) must be of type array|int|null, string given
32-
filter_var_array(): Argument #2 ($options) must be of type array|int|null, string given
33-
filter_input_array(): Argument #2 ($options) must be of type array|int|null, stdClass given
34-
filter_var_array(): Argument #2 ($options) must be of type array|int|null, stdClass given
32+
filter_input_array(): Argument #2 ($options) must be of type array|int, string given
33+
filter_var_array(): Argument #2 ($options) must be of type array|int, string given
34+
filter_input_array(): Argument #2 ($options) must be of type array|int, stdClass given
35+
filter_var_array(): Argument #2 ($options) must be of type array|int, stdClass given

0 commit comments

Comments
 (0)