[php-src] master: Merge branch 'PHP-8.6'

From: Date: Wed, 25 Nov 2026 14:44:27 +0000
Subject: [php-src] master: Merge branch 'PHP-8.6'
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144158@lists.php.net to get a copy of this message
Author: Arnaud Le Blanc (arnaud-lb) Date: 2026-11-25T15:44:14+01:00 Commit: https://github.com/php/php-src/commit/abab936a39594561990b002055944fc31abd7cb6 Raw diff: https://github.com/php/php-src/commit/abab936a39594561990b002055944fc31abd7cb6.diff Merge branch 'PHP-8.6' * PHP-8.6: Fix GH-25379: Prevent double evaluation and leaks on array_map() optimization fallback (#25546) Changed paths: A Zend/tests/array_map_foreach_optimization_gh25379.phpt M Zend/zend_compile.c Diff: diff --git a/Zend/tests/array_map_foreach_optimization_gh25379.phpt b/Zend/tests/array_map_foreach_optimization_gh25379.phpt new file mode 100644 index 000000000000..004ec247bb1d --- /dev/null +++ b/Zend/tests/array_map_foreach_optimization_gh25379.phpt @@ -0,0 +1,259 @@ +--TEST-- +GH-25379: array_map() foreach optimization callee single evaluation and memory leaks on fallback +--FILE-- +<?php + +class ConstHolder { + public const int VAL = 42; +} + +const CONST_INT = 10; + +function getf(): string { + echo "getf\n"; + return str_repeat('strto', 1) . 'upper'; +} + +function getc(): object { + echo "getc\n"; + return new class { + public static function m($x) { + return $x; + } + }; +} + +function getm(): string { + echo "getm\n"; + return str_repeat('m', 1); +} + +function throw_getm(): string { + throw new RuntimeException("boom m"); +} + +function fn_named(int $a, int $b): int { + return $a + $b; +} + +function getf_named(): string { + echo "getf_named\n"; + return str_repeat('fn_', 1) . 'named'; +} + +/* 1. Non-literal callee with IS_CONST int */ +try { + var_dump(array_map(getf()(...), 5)); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 2. Non-literal callee with IS_CONST string */ +try { + array_map(getf()(...), "hello"); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 3. Non-literal callee with IS_CONST null */ +try { + array_map(getf()(...), null); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 4. Non-literal callee with IS_CONST bool */ +try { + array_map(getf()(...), false); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 5. Non-literal class with IS_CONST int */ +try { + array_map(getc()::m(...), 5); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 6. Non-literal class and method with IS_CONST int */ +try { + array_map(getc()::{(getm())}(...), 5); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 7. Non-existent class with dynamic method and IS_CONST int */ +try { + array_map(Foo::{(getm())}(...), 5); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} + +/* 8. Exception in dynamic method evaluation */ +try { + array_map(getc()::{(throw_getm())}(...), 5); +} catch (RuntimeException $e) { + echo $e->getMessage(), "\n"; +} + +/* 9. Global constant non-array */ +try { + array_map(getf()(...), CONST_INT); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 10. Class constant non-array */ +try { + array_map(getf()(...), ConstHolder::VAL); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 11. Constant binary expression non-array */ +try { + array_map(getf()(...), 1 + 2); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 12. Variable callable with IS_CONST int */ +$f = 'strtoupper'; +try { + array_map($f(...), 5); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 13. Non-literal callee with __FILE__ */ +try { + array_map(getf()(...), __FILE__); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 14. Non-literal callee with stdClass::class */ +try { + array_map(getf()(...), stdClass::class); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 15. Non-literal callee with true */ +try { + array_map(getf()(...), true); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 16. Non-literal callee with __FILE__ . '.php' */ +try { + array_map(getf()(...), __FILE__ . '.php'); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 17. PFA with multiple placeholders falls back, callee evaluated once */ +try { + var_dump(array_map(getf()(?, ?), [1])); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} + +/* 18. PFA with variadic placeholder and named args falls back, callee evaluated once */ +try { + var_dump(array_map(getf_named()(a: 1, ...), [2])); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} + +/* 19. PFA with assert and \assert falls back cleanly */ +var_dump(array_map(assert(...), [1])); +var_dump(array_map(\assert(...), [1])); + +/* 20. Non-interned IS_CONST string produced during expression compilation */ +try { + array_map(getf()(...), "unique_non_interned_" . strlen("abcdef")); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 21. IS_CONST int produced during expression compilation */ +try { + array_map(getf()(...), strlen("abcdef")); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 22. Dynamic static call with non-interned IS_CONST string */ +try { + array_map(getc()::m(...), "unique_non_interned_static_" . strlen("abcdef")); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 23. Exception in dynamic method evaluation on optimized path */ +try { + array_map(getc()::{(throw_getm())}(...), "unique_non_interned_throw_" . strlen("abcdef")); +} catch (RuntimeException $e) { + echo $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +getf +array_map(): Argument #2 ($array) must be of type array, int given +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +array_map(): Argument #2 ($array) must be of type array, null given +getf +array_map(): Argument #2 ($array) must be of type array, false given +getc +array_map(): Argument #2 ($array) must be of type array, int given +getc +getm +array_map(): Argument #2 ($array) must be of type array, int given +getm +Class "Foo" not found +getc +boom m +getf +array_map(): Argument #2 ($array) must be of type array, int given +getf +array_map(): Argument #2 ($array) must be of type array, int given +getf +array_map(): Argument #2 ($array) must be of type array, int given +array_map(): Argument #2 ($array) must be of type array, int given +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +array_map(): Argument #2 ($array) must be of type array, true given +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +Partial application of strtoupper() expects at most 1 arguments, 2 given +getf_named +array(1) { + [0]=> + int(3) +} +array(1) { + [0]=> + bool(true) +} +array(1) { + [0]=> + bool(true) +} +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +array_map(): Argument #2 ($array) must be of type array, int given +getc +array_map(): Argument #2 ($array) must be of type array, string given +getc +boom m diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index f2cf095ac03c..6b78351c3989 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -5382,12 +5382,12 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg return FAILURE; } - /* Bail out if we do not have exactly two parameters. */ + /* Skip the optimization if we do not have exactly two parameters. */ if (args->children != 2) { return FAILURE; } - /* Bail out if callback is not a first class callable */ + /* Skip the optimization if callback is not a first class callable. */ zend_ast *callback = args->child[0]; if (callback->kind != ZEND_AST_CALL && callback->kind != ZEND_AST_STATIC_CALL) { return FAILURE; @@ -5418,6 +5418,25 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg } } + /* Skip the optimization if the input array is known at compile time to not + * be an array, before compiling any callee expressions or emitting opcodes. */ + zend_eval_const_expr(&args->child[1]); + if (args->child[1]->kind == ZEND_AST_ZVAL + && Z_TYPE_P(zend_ast_get_zval(args->child[1])) != IS_ARRAY) { + return FAILURE; + } + + znode value; + value.op_type = IS_TMP_VAR; + value.u.op.var = get_temporary_variable(); + + zend_ast *call_args = zend_partial_apply(callback, + zend_ast_create_znode(&value)); + if (!call_args) { + CG(active_op_array)->T--; + return FAILURE; + } + /* Evaluate class name */ znode class_node; if (callback->kind == ZEND_AST_STATIC_CALL @@ -5463,34 +5482,21 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg } } - znode value; - value.op_type = IS_TMP_VAR; - value.u.op.var = get_temporary_variable(); - - zend_ast *call_args = zend_partial_apply(callback, - zend_ast_create_znode(&value)); - if (!call_args) { - goto fail; - } - zend_op *opline; znode array; 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); - } /* Verify that the input array actually is an array. */ znode name; name.op_type = IS_CONST; ZVAL_STR_COPY(&name.u.constant, lcname); opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array); + /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. + * Addref after emitting the opline so string literals are interned first. */ + if (opline->op2_type == IS_CONST) { + Z_TRY_ADDREF_P(CT_CONSTANT(opline->op2)); + } opline->lineno = lineno; opline->extended_value = (2 << 16) | IS_ARRAY; const zval *fbc_zv = zend_hash_find(CG(function_table), lcname); @@ -5567,17 +5573,6 @@ 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) /* {{{ */

« previous php.cvs (#144158) next »