php / php-src

The PHP Interpreter
https://www.php.net
Other
37.98k stars 7.73k forks source link

Assertion failure with array_find when references are involved #15982

Closed YuanchengJiang closed 58 minutes ago

YuanchengJiang commented 3 hours ago

Description

The following code:

<?php
$a = array('foo' => 'original.foo');
$ref = &$a;
extract($a, EXTR_REFS);
$fusion = $ref;
var_dump(array_find($fusion, function ($value, $key) {
return true;
}));

Resulted in this output:

php: Zend/zend_vm_execute.h:1361: int ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER(zend_execute_data *): Assertion `(call->func->common.fn_flags & (1 << 12)) ? (zval_get_type(&(*(ret))) == 10) : !(zval_get_type(&(*(ret))) == 10)' failed.
Aborted (core dumped)

PHP Version

PHP 8.4.0-dev

Operating System

ubuntu 22.04

nielsdos commented 3 hours ago

Simplified:

<?php
$var = "hello";
$arrayWithRef = [];
$arrayWithRef[0] =& $var;
var_dump(array_find($arrayWithRef, fn () => true));

array_find returns a reference in this case, but the function does not claim it can return a reference. The question is if this should indeed return a reference or should get rid of the reference. I would assume the latter but I need to think a bit.

nielsdos commented 3 hours ago

Functions like array_shift deref the result, so I think the new 8.4 array functions should as well.