[php-src] master: Merge branch 'PHP-8.6'
| From: | Arnaud Le Blanc | Date: | Thu, 15 Oct 2026 13:20:34 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.6' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-142315@lists.php.net to get a copy of this message | ||
Author: Arnaud Le Blanc (arnaud-lb)
Date: 2026-10-15T15:20:19+02:00
Commit: https://github.com/php/php-src/commit/da054f484bac63fdbb2313fe7f02063c0a53cc4f
Raw diff: https://github.com/php/php-src/commit/da054f484bac63fdbb2313fe7f02063c0a53cc4f.diff
Merge branch 'PHP-8.6'
* PHP-8.6:
Fix memory leak in array_map optimization (#24138)
Changed paths:
A Zend/tests/array_map_foreach_optimization_const_non_array.phpt
M Zend/zend_compile.c
Diff:
diff --git a/Zend/tests/array_map_foreach_optimization_const_non_array.phpt
b/Zend/tests/array_map_foreach_optimization_const_non_array.phpt
new file mode 100644
index 000000000000..5ccfbdd7b646
--- /dev/null
+++ b/Zend/tests/array_map_foreach_optimization_const_non_array.phpt
@@ -0,0 +1,32 @@
+--TEST--
+array_map(): foreach optimization - constant non-array argument does not leak
+--FILE--
+<?php
+
+$tests = [
+ 'string' => fn() => array_map(strtoupper(...), "str"),
+ 'int' => fn() => array_map(strtoupper(...), 1),
+ 'float' => fn() => array_map(strtoupper(...), 1.5),
+ 'true' => fn() => array_map(strtoupper(...), true),
+ 'null' => fn() => array_map(strtoupper(...), null),
+ 'folded string' => fn() => array_map(strtoupper(...), "a" .
"b"),
+ 'static call' => fn() => array_map(Foo::bar(...), "str"),
+];
+
+foreach ($tests as $name => $test) {
+ try {
+ $test();
+ } catch (Throwable $e) {
+ echo $name, ': ', get_class($e), ': ', $e->getMessage(),
"\n";
+ }
+}
+
+?>
+--EXPECT--
+string: TypeError: array_map(): Argument #2 ($array) must be of type array, string given
+int: TypeError: array_map(): Argument #2 ($array) must be of type array, int given
+float: TypeError: array_map(): Argument #2 ($array) must be of type array, float given
+true: TypeError: array_map(): Argument #2 ($array) must be of type array, true given
+null: TypeError: array_map(): Argument #2 ($array) must be of type array, null given
+folded string: TypeError: array_map(): Argument #2 ($array) must be of type array, string given
+static call: Error: Class "Foo" not found
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 2633e7d536d3..ffd40f06014d 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -5392,15 +5392,7 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list
*arg
zend_ast *call_args = zend_partial_apply(callback,
zend_ast_create_znode(&value));
if (!call_args) {
- CG(active_op_array)->T--;
- if (func_node.op_type == IS_CONST) {
- zval_ptr_dtor_nogc(&func_node.u.constant);
- }
- if (class_node.op_type == IS_CONST) {
- zval_ptr_dtor_nogc(&class_node.u.constant);
- }
- /* The callback is not a FCC/PFA, or is not optimizable */
- return FAILURE;
+ goto fail;
}
zend_op *opline;
@@ -5409,6 +5401,10 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list
*arg
zend_compile_expr(&array, args->child[1]);
/* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */
if (array.op_type == IS_CONST) {
+ if (Z_TYPE(array.u.constant) != IS_ARRAY) {
+ zval_ptr_dtor_nogc(&array.u.constant);
+ goto fail;
+ }
Z_TRY_ADDREF(array.u.constant);
}
@@ -5489,6 +5485,17 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list
*arg
}
return SUCCESS;
+
+fail:
+ CG(active_op_array)->T--;
+ if (func_node.op_type == IS_CONST) {
+ zval_ptr_dtor_nogc(&func_node.u.constant);
+ }
+ if (class_node.op_type == IS_CONST) {
+ zval_ptr_dtor_nogc(&class_node.u.constant);
+ }
+
+ return FAILURE;
}
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname,
zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */