From 3d0a654b679a673602f88b17906d2a85b33ca844 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Fri, 30 Aug 2024 00:21:44 +0200 Subject: [PATCH 1/7] Validate mode in array_filter() --- ext/standard/array.c | 13 ++++++++++++- ext/standard/php_array.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index 893e07dd8f99a..87b6e216b1fe3 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -6453,7 +6453,7 @@ PHP_FUNCTION(array_filter) zval args[2]; zval retval; bool have_callback = 0; - zend_long use_type = 0; + zend_long use_type = ARRAY_FILTER_USE_VALUE; zend_string *string_key; zend_fcall_info fci = empty_fcall_info; zend_fcall_info_cache fci_cache; @@ -6466,10 +6466,21 @@ PHP_FUNCTION(array_filter) Z_PARAM_LONG(use_type) ZEND_PARSE_PARAMETERS_END(); + switch (use_type) { + case ARRAY_FILTER_USE_VALUE: + case ARRAY_FILTER_USE_BOTH: + case ARRAY_FILTER_USE_KEY: + break; + default: + zend_argument_value_error(3, "must be a valid mode"); + RETURN_THROWS(); + } + if (zend_hash_num_elements(Z_ARRVAL_P(array)) == 0) { RETVAL_EMPTY_ARRAY(); return; } + array_init(return_value); if (ZEND_FCI_INITIALIZED(fci)) { diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index 2a35af6038083..2205082e91dfd 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -59,6 +59,7 @@ PHPAPI bool php_array_pick_keys(php_random_algo_with_state engine, zval *input, #define PHP_COUNT_NORMAL 0 #define PHP_COUNT_RECURSIVE 1 +#define ARRAY_FILTER_USE_VALUE 0 #define ARRAY_FILTER_USE_BOTH 1 #define ARRAY_FILTER_USE_KEY 2 From 4792f8c4f181baffe83a5abad8c0a1fb81a43a79 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Fri, 30 Aug 2024 00:23:35 +0200 Subject: [PATCH 2/7] Added test for validation of mode in array_filter() --- .../tests/array/array_filter_invalid_mode.phpt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ext/standard/tests/array/array_filter_invalid_mode.phpt diff --git a/ext/standard/tests/array/array_filter_invalid_mode.phpt b/ext/standard/tests/array/array_filter_invalid_mode.phpt new file mode 100644 index 0000000000000..06b0e1a058ef5 --- /dev/null +++ b/ext/standard/tests/array/array_filter_invalid_mode.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test array_filter() function : usage variations - mode exception +--FILE-- +getMessage(), "\n"; +} + +echo "Done" +?> +--EXPECT-- +ValueError: array_filter(): Argument #3 ($mode) must be a valid mode +Done From fac7bda0ac3aed56169f32a8ab6f88b6205a9530 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Sat, 17 Jan 2026 00:21:28 +0100 Subject: [PATCH 3/7] nit: reverted empty line --- ext/standard/array.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index 87b6e216b1fe3..fea5c6cf07416 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -6480,7 +6480,6 @@ PHP_FUNCTION(array_filter) RETVAL_EMPTY_ARRAY(); return; } - array_init(return_value); if (ZEND_FCI_INITIALIZED(fci)) { From e9f9f4e78a5b78fbbee0e6127b9bf203c4491e9b Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Mon, 19 Jan 2026 23:46:26 +0100 Subject: [PATCH 4/7] added ARRAY_FILTER_USE_VALUE constant --- ext/standard/basic_functions.stub.php | 5 +++++ ext/standard/basic_functions_arginfo.h | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index e27dca069c55b..4ad26063a6718 100644 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -109,6 +109,11 @@ */ const COUNT_RECURSIVE = UNKNOWN; +/** + * @var int + * @cvalue ARRAY_FILTER_USE_VALUE + */ +const ARRAY_FILTER_USE_VALUE = UNKNOWN; /** * @var int * @cvalue ARRAY_FILTER_USE_BOTH diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 6f202c01463fd..3b9ec0aee200b 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 1a1667a5c59111f096a758d5bb4aa7cf3ec09cfe */ + * Stub hash: a4324854949e2df355625c18a4e26d066c8f7a17 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0) @@ -3530,6 +3530,7 @@ static void register_basic_functions_symbols(int module_number) REGISTER_LONG_CONSTANT("CASE_UPPER", PHP_CASE_UPPER, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("COUNT_NORMAL", PHP_COUNT_NORMAL, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("COUNT_RECURSIVE", PHP_COUNT_RECURSIVE, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("ARRAY_FILTER_USE_VALUE", ARRAY_FILTER_USE_VALUE, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("ARRAY_FILTER_USE_BOTH", ARRAY_FILTER_USE_BOTH, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("ARRAY_FILTER_USE_KEY", ARRAY_FILTER_USE_KEY, CONST_PERSISTENT); zend_constant *const_ASSERT_ACTIVE = REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", PHP_ASSERT_ACTIVE, CONST_PERSISTENT | CONST_DEPRECATED); From 990e1849cba2ef19520a9e2ea32e0bb5558957ee Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Mon, 19 Jan 2026 23:47:22 +0100 Subject: [PATCH 5/7] improved error message --- ext/standard/array.c | 4 ++-- ext/standard/tests/array/array_filter_invalid_mode.phpt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index fea5c6cf07416..f8dd7d891dd39 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -6472,7 +6472,7 @@ PHP_FUNCTION(array_filter) case ARRAY_FILTER_USE_KEY: break; default: - zend_argument_value_error(3, "must be a valid mode"); + zend_argument_value_error(3, "must be one of ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH"); RETURN_THROWS(); } @@ -6496,7 +6496,7 @@ PHP_FUNCTION(array_filter) ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, string_key, operand) { if (have_callback) { - if (use_type) { + if (use_type != ARRAY_FILTER_USE_VALUE) { /* Set up the key */ if (!string_key) { ZVAL_LONG(key, num_key); diff --git a/ext/standard/tests/array/array_filter_invalid_mode.phpt b/ext/standard/tests/array/array_filter_invalid_mode.phpt index 06b0e1a058ef5..b312bcfe5a231 100644 --- a/ext/standard/tests/array/array_filter_invalid_mode.phpt +++ b/ext/standard/tests/array/array_filter_invalid_mode.phpt @@ -12,5 +12,5 @@ try { echo "Done" ?> --EXPECT-- -ValueError: array_filter(): Argument #3 ($mode) must be a valid mode +ValueError: array_filter(): Argument #3 ($mode) must be one of ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH Done From cd686d9ea197bd589fcfa0d29d00c74107a3e49e Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Mon, 19 Jan 2026 23:51:43 +0100 Subject: [PATCH 6/7] added NEWS entry --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index 74816c94a2906..1dab523618933 100644 --- a/NEWS +++ b/NEWS @@ -97,6 +97,8 @@ PHP NEWS - Standard: . Fixed bug GH-19926 (reset internal pointer earlier while splicing array while COW violation flag is still set). (alexandre-daubois) + . Invalid mode values now throw in array_filter() instead of being silently + defaulted to 0. (Jorg Sowa) - Streams: . Added so_keepalive, tcp_keepidle, tcp_keepintvl and tcp_keepcnt stream From 4c2201ae90b5db94fe828467747cc98f7f2dbb3d Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Tue, 20 Jan 2026 23:54:32 +0100 Subject: [PATCH 7/7] add entry to UPGRADING --- UPGRADING | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/UPGRADING b/UPGRADING index 3d0a56756cdcc..522050fe720d9 100644 --- a/UPGRADING +++ b/UPGRADING @@ -23,6 +23,10 @@ PHP 8.6 UPGRADE NOTES . Invalid values now throw in Phar::mungServer() instead of being silently ignored. +- Standard: + . Invalid mode values now throw in array_filter() instead of being silently + defaulted to 0. + ======================================== 2. New Features ========================================