Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Zend/tests/arg_unpack/gh25313.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
GH-25313 (Assertion !EG(exception) in ZEND_FE_FREE when unpacking a generator into a by-ref parameter throws)
--CREDITS--
YuanchengJiang
--FILE--
<?php
set_error_handler(function($n, $s) { throw new Exception($s); });

function test($a, $b, $c, &$ref) {}

function gen($array) {
foreach ($array as $e)
yield $e;
}

try {
test(...gen([1, 2]), ...gen([3, 4]));
} catch (Throwable $e) {
echo $e::class, ": ", $e->getMessage(), "\n";
}

try {
test(...gen(['a' => 1, 'b' => 2]), ...gen(['c' => 3, 'ref' => 4]));
} catch (Throwable $e) {
echo $e::class, ": ", $e->getMessage(), "\n";
}
?>
--EXPECT--
Exception: Cannot pass by-reference argument 4 of test() by unpacking a Traversable, passing by-value instead
Exception: Cannot pass by-reference argument 4 of test() by unpacking a Traversable, passing by-value instead
14 changes: 10 additions & 4 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -5356,14 +5356,18 @@ ZEND_VM_C_LABEL(send_again):
Z_TRY_ADDREF_P(arg);

if (ARG_MUST_BE_SENT_BY_REF(EX(call)->func, arg_num)) {
ZVAL_NEW_REF(top, arg);
zend_error(
E_WARNING, "Cannot pass by-reference argument %d of %s%s%s()"
" by unpacking a Traversable, passing by-value instead", arg_num,
EX(call)->func->common.scope ? ZSTR_VAL(EX(call)->func->common.scope->name) : "",
EX(call)->func->common.scope ? "::" : "",
ZSTR_VAL(EX(call)->func->common.function_name)
);
ZVAL_NEW_REF(top, arg);
if (UNEXPECTED(EG(exception) != NULL)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string-key branch hit the same assertion: test(...gen(['a' => 1, 'b' => 2]), ...gen(['c' => 3, 'ref' => 4])) aborts on PHP-8.4 and passes with this change. Could the test cover that case too?

zend_string_release(name);
break;
}
} else {
ZVAL_COPY_VALUE(top, arg);
}
Expand All @@ -5380,21 +5384,23 @@ ZEND_VM_C_LABEL(send_again):
top = ZEND_CALL_ARG(EX(call), arg_num);
ZVAL_DEREF(arg);
Z_TRY_ADDREF_P(arg);
ZEND_CALL_NUM_ARGS(EX(call))++;

if (ARG_MUST_BE_SENT_BY_REF(EX(call)->func, arg_num)) {
ZVAL_NEW_REF(top, arg);
zend_error(
E_WARNING, "Cannot pass by-reference argument %d of %s%s%s()"
" by unpacking a Traversable, passing by-value instead", arg_num,
EX(call)->func->common.scope ? ZSTR_VAL(EX(call)->func->common.scope->name) : "",
EX(call)->func->common.scope ? "::" : "",
ZSTR_VAL(EX(call)->func->common.function_name)
);
ZVAL_NEW_REF(top, arg);
if (UNEXPECTED(EG(exception) != NULL)) {
break;
}
} else {
ZVAL_COPY_VALUE(top, arg);
}

ZEND_CALL_NUM_ARGS(EX(call))++;
}

funcs->move_forward(iter);
Expand Down
14 changes: 10 additions & 4 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading