Skip to content
Merged
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
34 changes: 34 additions & 0 deletions Zend/tests/partial_application/construct_throw_arg_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Partial application must not leak bound arguments when construction throws
--FILE--
<?php
class Obj {}

function typed(int $a, $b, $c) {}
try {
typed([1, 2, 3], new Obj(), ?);
} catch (\TypeError $e) {
echo $e::class, "\n";
}

function typed2($a, int $b, $c, $d) {}
try {
typed2(new Obj(), [1, 2], new Obj(), ?);
} catch (\TypeError $e) {
echo $e::class, "\n";
}

function one($a) {}
try {
one(new Obj(), new Obj(), ?);
} catch (\ArgumentCountError $e) {
echo $e::class, "\n";
}

echo "OK\n";
?>
--EXPECT--
TypeError
TypeError
ArgumentCountError
OK
9 changes: 9 additions & 0 deletions Zend/zend_partial.c
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,13 @@ static const zend_op_array *zp_get_op_array(zval *this_ptr, zend_function *funct
return op_array;
}

static void zp_free_unbound_args(uint32_t start, uint32_t argc, zval *argv)
{
for (uint32_t offset = start; offset < argc; offset++) {
zval_ptr_dtor_nogc(&argv[offset]);
}
}

/* Bind pre-bound arguments as lexical vars */
static void zp_bind(zval *result, zend_function *function, uint32_t argc, zval *argv,
zend_array *extra_named_params) {
Expand Down Expand Up @@ -1102,6 +1109,7 @@ static void zp_bind(zval *result, zend_function *function, uint32_t argc, zval *
zend_verify_arg_error(function, arg_info, offset+1, var);
zval_ptr_dtor(result);
ZVAL_NULL(result);
zp_free_unbound_args(offset, argc, argv);
return;
}
ZEND_ASSERT(zp_arg_must_be_sent_by_ref(function, offset+1) ? Z_ISREF_P(var) : !Z_ISREF_P(var));
Expand Down Expand Up @@ -1131,6 +1139,7 @@ void zend_partial_create(zval *result, zval *this_ptr, zend_function *function,

if (UNEXPECTED(!op_array)) {
ZEND_ASSERT(EG(exception));
zp_free_unbound_args(0, argc, argv);
ZVAL_NULL(result);
return;
}
Expand Down
Loading