php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Partial function application: named args in a constant expression cause out-of-bounds write in zp_compile()

Open apoc opened this issue 1 month ago • 0 comments

Description

zend_compile_args_ex() records named_positions only for named placeholders, but zend_ast_evaluate_arg_list() (Zend/zend_ast.c ~L704) records every named argument. zp_compile() (Zend/zend_partial.c ~L771-776, L861) uses the stored index to write into params, which is allocated with num_params elements, so params[param_offset] is written past the array and params[0] stays NULL.

The following code:

<?php
function h($a, $b = 2) {}
const C = h(a: 1, b: ?);
var_dump(C);

Resulted in this output:

<access violation, exit code 0xC0000005, no output>

But I expected this output instead: a partial function object (the same code inside a function body works), or a compile error.

A parameter without a default (function g($a, $b)) gives ArgumentCountError first, so the parameter after the placeholder needs a default to reach the write.

Only master/PHP-8.6 is affected (feature not yet released).

Suggested fix: add the named_positions entry only when the argument is a placeholder, mirroring zend_compile_args_ex(). The existing constexpr_*.phpt mixed-argument test trips the by-reference check first, so a f(a: 1, b: ?) case should be added.

PHP Version

PHP 8.7.0-dev (cli) (built: Oct 27 2026 17:51:17) (ZTS Visual C++ 2026 x64)
Zend Engine v4.7.0-dev (master @ 0af8a091eec28513a6a0b758bcb10b430ba6c6de)

Operating System

Windows 11 x64 (not platform specific by source inspection)

apoc avatar Oct 28 '26 16:10 apoc