Skip to content

Commit 68302fe

Browse files
committed
ext/filter fixes
1 parent ebbf195 commit 68302fe

File tree

6 files changed

+65
-33
lines changed

6 files changed

+65
-33
lines changed

ext/filter/filter.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -532,20 +532,18 @@ static void php_filter_call(
532532
zval *option;
533533
char *charset = NULL;
534534

535-
if (filter_args_long) {
536-
zend_long lval = filter_args_long;
537-
535+
if (!filter_args_ht) {
538536
if (filter != -1) { /* handler for array apply */
539537
/* filter_args is the filter_flags */
540-
filter_flags = lval;
538+
filter_flags = filter_args_long;
541539

542540
if (!(filter_flags & FILTER_REQUIRE_ARRAY || filter_flags & FILTER_FORCE_ARRAY)) {
543541
filter_flags |= FILTER_REQUIRE_SCALAR;
544542
}
545543
} else {
546-
filter = lval;
544+
filter = filter_args_long;
547545
}
548-
} else if (filter_args_ht) {
546+
} else {
549547
if ((option = zend_hash_str_find(filter_args_ht, "filter", sizeof("filter") - 1)) != NULL) {
550548
filter = zval_get_long(option);
551549
}
@@ -609,12 +607,9 @@ static void php_filter_array_handler(zval *input, HashTable *op_ht, zend_long op
609607
zend_string *arg_key;
610608
zval *tmp, *arg_elm;
611609

612-
if (!op_long && !op_ht) {
613-
ZVAL_DUP(return_value, input);
614-
php_filter_call(return_value, FILTER_DEFAULT, NULL, 0, 0, FILTER_REQUIRE_ARRAY);
615-
} else if (op_long) {
610+
if (!op_ht) {
616611
ZVAL_DUP(return_value, input);
617-
php_filter_call(return_value, op_long, NULL, 0, 0, FILTER_REQUIRE_ARRAY);
612+
php_filter_call(return_value, -1, NULL, op_long, 0, FILTER_REQUIRE_ARRAY);
618613
} else {
619614
array_init(return_value);
620615

@@ -665,6 +660,7 @@ PHP_FUNCTION(filter_input)
665660
ZEND_PARSE_PARAMETERS_END();
666661

667662
if (!PHP_FILTER_ID_EXISTS(filter)) {
663+
php_error_docref(NULL, E_WARNING, "Unknown filter with ID " ZEND_LONG_FMT, filter);
668664
RETURN_FALSE;
669665
}
670666

@@ -726,6 +722,7 @@ PHP_FUNCTION(filter_var)
726722
ZEND_PARSE_PARAMETERS_END();
727723

728724
if (!PHP_FILTER_ID_EXISTS(filter)) {
725+
php_error_docref(NULL, E_WARNING, "Unknown filter with ID " ZEND_LONG_FMT, filter);
729726
RETURN_FALSE;
730727
}
731728

@@ -742,7 +739,7 @@ PHP_FUNCTION(filter_input_array)
742739
zval *array_input = NULL;
743740
zend_bool add_empty = 1;
744741
HashTable *op_ht = NULL;
745-
zend_long op_long = 0;
742+
zend_long op_long = FILTER_DEFAULT;
746743

747744
ZEND_PARSE_PARAMETERS_START(1, 3)
748745
Z_PARAM_LONG(fetch_from)
@@ -751,7 +748,8 @@ PHP_FUNCTION(filter_input_array)
751748
Z_PARAM_BOOL(add_empty)
752749
ZEND_PARSE_PARAMETERS_END();
753750

754-
if (op_long && !PHP_FILTER_ID_EXISTS(op_long)) {
751+
if (!op_ht && !PHP_FILTER_ID_EXISTS(op_long)) {
752+
php_error_docref(NULL, E_WARNING, "Unknown filter with ID " ZEND_LONG_FMT, op_long);
755753
RETURN_FALSE;
756754
}
757755

@@ -791,7 +789,7 @@ PHP_FUNCTION(filter_var_array)
791789
zval *array_input = NULL;
792790
zend_bool add_empty = 1;
793791
HashTable *op_ht = NULL;
794-
zend_long op_long = 0;
792+
zend_long op_long = FILTER_DEFAULT;
795793

796794
ZEND_PARSE_PARAMETERS_START(1, 3)
797795
Z_PARAM_ARRAY(array_input)
@@ -800,7 +798,8 @@ PHP_FUNCTION(filter_var_array)
800798
Z_PARAM_BOOL(add_empty)
801799
ZEND_PARSE_PARAMETERS_END();
802800

803-
if (op_long && !PHP_FILTER_ID_EXISTS(op_long)) {
801+
if (!op_ht && !PHP_FILTER_ID_EXISTS(op_long)) {
802+
php_error_docref(NULL, E_WARNING, "Unknown filter with ID " ZEND_LONG_FMT, op_long);
804803
RETURN_FALSE;
805804
}
806805

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 $options = []): mixed {}
7+
function filter_input(int $type, string $variable_name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed {}
88

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

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

13-
function filter_var_array(array $data, array|int $options = [], bool $add_empty = true): array|false|null {}
13+
function filter_var_array(array $data, array|int $options = FILTER_DEFAULT, 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: dfd87cd01b448776214514a5fb5e25bb0de9d1b5 */
2+
* Stub hash: dae0cf8beeb47a996123cffd3b429482047d42c1 */
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, "[]")
13+
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG, "0")
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, "[]")
19+
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG, "0")
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, "[]")
24+
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG, "FILTER_DEFAULT")
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, "[]")
30+
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_LONG, "FILTER_DEFAULT")
3131
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, add_empty, _IS_BOOL, 0, "true")
3232
ZEND_END_ARG_INFO()
3333

ext/filter/tests/010.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var_dump(filter_var(1, 0, array()));
1717

1818
echo "Done\n";
1919
?>
20-
--EXPECT--
20+
--EXPECTF--
2121
array(7) {
2222
[0]=>
2323
int(1)
@@ -55,6 +55,10 @@ array(7) {
5555
string(1) "1"
5656
string(1) "1"
5757
string(1) "1"
58+
59+
Warning: filter_var(): Unknown filter with ID -1 in %s on line %d
5860
bool(false)
61+
62+
Warning: filter_var(): Unknown filter with ID 0 in %s on line %d
5963
bool(false)
6064
Done

ext/filter/tests/039.phpt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var_dump($a, $b);
8080

8181
echo "Done\n";
8282
?>
83-
--EXPECT--
83+
--EXPECTF--
8484
-- (1)
8585
array(0) {
8686
}
@@ -90,15 +90,27 @@ array(2) {
9090
["blah"]=>
9191
string(4) "hoho"
9292
}
93+
94+
Warning: filter_var_array(): Unknown filter with ID -1 in %s on line %d
9395
bool(false)
96+
97+
Warning: filter_var_array(): Unknown filter with ID 1000000 in %s on line %d
9498
bool(false)
9599
filter_var_array(): Argument #2 ($options) must be of type array|int, string given
96100
-- (2)
101+
102+
Warning: filter_var_array(): Unknown filter with ID -1 in %s on line %d
97103
bool(false)
104+
105+
Warning: filter_var_array(): Unknown filter with ID 1000000 in %s on line %d
98106
bool(false)
99107
filter_var_array(): Argument #2 ($options) must be of type array|int, string given
100108
-- (3)
109+
110+
Warning: filter_var_array(): Unknown filter with ID -1 in %s on line %d
101111
bool(false)
112+
113+
Warning: filter_var_array(): Unknown filter with ID 1000000 in %s on line %d
102114
bool(false)
103115
filter_var_array(): Argument #2 ($options) must be of type array|int, string given
104116
-- (4)
@@ -120,12 +132,16 @@ array(1) {
120132
-- (5)
121133
filter_var_array(): Argument #2 ($options) cannot contain empty keys
122134
filter_var_array(): Argument #2 ($options) cannot contain empty keys
135+
136+
Warning: filter_var_array(): Unknown filter with ID -1 in %s on line %d
123137
bool(false)
124138
array(1) {
125139
[""]=>
126140
string(0) ""
127141
}
128142
int(-1)
143+
144+
Warning: filter_var_array(): Unknown filter with ID 100000 in %s on line %d
129145
bool(false)
130146
array(1) {
131147
[""]=>

ext/filter/tests/057.phpt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,29 @@ foreach (array(null, true, false, 1, "", new stdClass) as $invalid) {
1818
}
1919
}
2020
?>
21-
--EXPECT--
22-
NULL
23-
array(0) {
24-
}
21+
--EXPECTF--
22+
Warning: filter_input_array(): Unknown filter with ID 0 in %s on line %d
2523
bool(false)
24+
25+
Warning: filter_var_array(): Unknown filter with ID 0 in %s on line %d
2626
bool(false)
27-
NULL
28-
array(0) {
29-
}
27+
28+
Warning: filter_input_array(): Unknown filter with ID 1 in %s on line %d
3029
bool(false)
30+
31+
Warning: filter_var_array(): Unknown filter with ID 1 in %s on line %d
32+
bool(false)
33+
34+
Warning: filter_input_array(): Unknown filter with ID 0 in %s on line %d
35+
bool(false)
36+
37+
Warning: filter_var_array(): Unknown filter with ID 0 in %s on line %d
38+
bool(false)
39+
40+
Warning: filter_input_array(): Unknown filter with ID 1 in %s on line %d
41+
bool(false)
42+
43+
Warning: filter_var_array(): Unknown filter with ID 1 in %s on line %d
3144
bool(false)
3245
filter_input_array(): Argument #2 ($options) must be of type array|int, string given
3346
filter_var_array(): Argument #2 ($options) must be of type array|int, string given

0 commit comments

Comments
 (0)