Skip to content

Commit 6d95a22

Browse files
authored
ext/standard: validate mode in array_filter() (#15647)
And add the missing ARRAY_FILTER_USE_VALUE for the default case.
1 parent 8c4c430 commit 6d95a22

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ PHP NEWS
9999
- Standard:
100100
. Fixed bug GH-19926 (reset internal pointer earlier while splicing array
101101
while COW violation flag is still set). (alexandre-daubois)
102+
. Invalid mode values now throw in array_filter() instead of being silently
103+
defaulted to 0. (Jorg Sowa)
102104

103105
- Streams:
104106
. Added so_keepalive, tcp_keepidle, tcp_keepintvl and tcp_keepcnt stream

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ PHP 8.6 UPGRADE NOTES
2323
. Invalid values now throw in Phar::mungServer() instead of being silently
2424
ignored.
2525

26+
- Standard:
27+
. Invalid mode values now throw in array_filter() instead of being silently
28+
defaulted to 0.
29+
2630
========================================
2731
2. New Features
2832
========================================

ext/standard/array.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6453,7 +6453,7 @@ PHP_FUNCTION(array_filter)
64536453
zval args[2];
64546454
zval retval;
64556455
bool have_callback = 0;
6456-
zend_long use_type = 0;
6456+
zend_long use_type = ARRAY_FILTER_USE_VALUE;
64576457
zend_string *string_key;
64586458
zend_fcall_info fci = empty_fcall_info;
64596459
zend_fcall_info_cache fci_cache;
@@ -6466,6 +6466,16 @@ PHP_FUNCTION(array_filter)
64666466
Z_PARAM_LONG(use_type)
64676467
ZEND_PARSE_PARAMETERS_END();
64686468

6469+
switch (use_type) {
6470+
case ARRAY_FILTER_USE_VALUE:
6471+
case ARRAY_FILTER_USE_BOTH:
6472+
case ARRAY_FILTER_USE_KEY:
6473+
break;
6474+
default:
6475+
zend_argument_value_error(3, "must be one of ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH");
6476+
RETURN_THROWS();
6477+
}
6478+
64696479
if (zend_hash_num_elements(Z_ARRVAL_P(array)) == 0) {
64706480
RETVAL_EMPTY_ARRAY();
64716481
return;
@@ -6486,7 +6496,7 @@ PHP_FUNCTION(array_filter)
64866496

64876497
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, string_key, operand) {
64886498
if (have_callback) {
6489-
if (use_type) {
6499+
if (use_type != ARRAY_FILTER_USE_VALUE) {
64906500
/* Set up the key */
64916501
if (!string_key) {
64926502
ZVAL_LONG(key, num_key);

ext/standard/basic_functions.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@
109109
*/
110110
const COUNT_RECURSIVE = UNKNOWN;
111111

112+
/**
113+
* @var int
114+
* @cvalue ARRAY_FILTER_USE_VALUE
115+
*/
116+
const ARRAY_FILTER_USE_VALUE = UNKNOWN;
112117
/**
113118
* @var int
114119
* @cvalue ARRAY_FILTER_USE_BOTH

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/php_array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ PHPAPI bool php_array_pick_keys(php_random_algo_with_state engine, zval *input,
5959
#define PHP_COUNT_NORMAL 0
6060
#define PHP_COUNT_RECURSIVE 1
6161

62+
#define ARRAY_FILTER_USE_VALUE 0
6263
#define ARRAY_FILTER_USE_BOTH 1
6364
#define ARRAY_FILTER_USE_KEY 2
6465

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test array_filter() function : usage variations - mode exception
3+
--FILE--
4+
<?php
5+
6+
try {
7+
var_dump(array_filter([], mode: 999));
8+
} catch (Throwable $e) {
9+
echo $e::class . ': '.$e->getMessage(), "\n";
10+
}
11+
12+
echo "Done"
13+
?>
14+
--EXPECT--
15+
ValueError: array_filter(): Argument #3 ($mode) must be one of ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH
16+
Done

0 commit comments

Comments
 (0)