Commit c2a96367205 for php
commit c2a963672051f4185651fff4273b9a74849c42f9
Author: lacatoire <la.catoire@gmail.com>
Date: Tue Oct 20 09:42:43 2026 +0200
Fix array_udiff_assoc() and array_uintersect_assoc() clobbering the user comparison callback
php_array_diff_key() and php_array_intersect_key() parse the callback straight
into BG(user_compare_fci) without saving the previous value, unlike the other
array_u*() functions. A comparison callback that calls one of them is replaced
by the inner callback for the rest of the sort.
Closes GH-24453
diff --git a/NEWS b/NEWS
index af24f1316bd..b4e6149982d 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,8 @@ PHP NEWS
- Standard:
. Fixed bug GH-25959 ("consumed" stream filter loses or repeats data).
(Artem Ukrainskiy)
+ . Fixed array_udiff_assoc() and array_uintersect_assoc() clobbering the user
+ comparison callback. (lacatoire)
- Date:
. Fixed bug GH-26032 (DateTimeZone->getTransitions() includes too-early
diff --git a/ext/standard/array.c b/ext/standard/array.c
index c10ad47c093..b23b0e4fbad 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -5194,6 +5194,9 @@ static void php_array_intersect_key(INTERNAL_FUNCTION_PARAMETERS, int data_compa
char *param_spec;
zend_string *key;
zend_ulong h;
+ PHP_ARRAY_CMP_FUNC_VARS;
+
+ PHP_ARRAY_CMP_FUNC_BACKUP();
/* Get the argument count */
argc = ZEND_NUM_ARGS();
@@ -5212,12 +5215,14 @@ static void php_array_intersect_key(INTERNAL_FUNCTION_PARAMETERS, int data_compa
}
if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &argc, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) {
+ PHP_ARRAY_CMP_FUNC_RESTORE();
RETURN_THROWS();
}
for (i = 0; i < argc; i++) {
if (Z_TYPE(args[i]) != IS_ARRAY) {
zend_argument_type_error(i + 1, "must be of type array, %s given", zend_zval_value_name(&args[i]));
+ PHP_ARRAY_CMP_FUNC_RESTORE();
RETURN_THROWS();
}
}
@@ -5261,6 +5266,8 @@ static void php_array_intersect_key(INTERNAL_FUNCTION_PARAMETERS, int data_compa
}
}
} ZEND_HASH_FOREACH_END();
+
+ PHP_ARRAY_CMP_FUNC_RESTORE();
}
/* }}} */
@@ -5594,16 +5601,21 @@ static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_ty
zval *val, *data;
zend_string *key;
zend_ulong h;
+ PHP_ARRAY_CMP_FUNC_VARS;
+
+ PHP_ARRAY_CMP_FUNC_BACKUP();
/* Get the argument count */
argc = ZEND_NUM_ARGS();
if (data_compare_type == DIFF_COMP_DATA_USER) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+f", &args, &argc, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) {
+ PHP_ARRAY_CMP_FUNC_RESTORE();
RETURN_THROWS();
}
diff_data_compare_func = zval_user_compare;
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &argc) == FAILURE) {
+ PHP_ARRAY_CMP_FUNC_RESTORE();
RETURN_THROWS();
}
if (data_compare_type == DIFF_COMP_DATA_INTERNAL) {
@@ -5614,6 +5626,7 @@ static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_ty
for (i = 0; i < argc; i++) {
if (Z_TYPE(args[i]) != IS_ARRAY) {
zend_argument_type_error(i + 1, "must be of type array, %s given", zend_zval_value_name(&args[i]));
+ PHP_ARRAY_CMP_FUNC_RESTORE();
RETURN_THROWS();
}
}
@@ -5657,6 +5670,8 @@ static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_ty
}
}
} ZEND_HASH_FOREACH_END();
+
+ PHP_ARRAY_CMP_FUNC_RESTORE();
}
/* }}} */
diff --git a/ext/standard/tests/array/array_user_assoc_nested_compare.phpt b/ext/standard/tests/array/array_user_assoc_nested_compare.phpt
new file mode 100644
index 00000000000..dba439210b0
--- /dev/null
+++ b/ext/standard/tests/array/array_user_assoc_nested_compare.phpt
@@ -0,0 +1,43 @@
+--TEST--
+array_udiff_assoc() and array_uintersect_assoc() restore the user comparison callback
+--FILE--
+<?php
+
+$sorted = [3, 1, 2];
+usort($sorted, function ($a, $b) {
+ echo "outer\n";
+ array_udiff_assoc([1], [1], function ($x, $y) {
+ echo "inner\n";
+ return $x <=> $y;
+ });
+ return $a <=> $b;
+});
+echo implode(',', $sorted), "\n";
+
+$sorted = [3, 1, 2];
+usort($sorted, function ($a, $b) {
+ echo "outer\n";
+ array_uintersect_assoc([1], [1], function ($x, $y) {
+ echo "inner\n";
+ return $x <=> $y;
+ });
+ return $a <=> $b;
+});
+echo implode(',', $sorted), "\n";
+
+?>
+--EXPECT--
+outer
+inner
+outer
+inner
+outer
+inner
+1,2,3
+outer
+inner
+outer
+inner
+outer
+inner
+1,2,3