Skip to content

Commit 18652e6

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: ext/reflection: Don't call destructor when constructor is not public in newInstance(Args)()
2 parents f59f30c + 250c74e commit 18652e6

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ PHP NEWS
153153
. Fixed ReflectionMethod::getClosure() returning a closure for abstract
154154
methods, which crashed when called for the ones of internal classes.
155155
(Nicolas Grekas)
156+
. Don't call destructor when constructor is not public in newInstance(Args)().
157+
(ndossche)
156158

157159
- SNMP:
158160
. Fixed snmp3_set() citing the wrong argument number for an invalid $timeout

‎ext/reflection/php_reflection.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5184,6 +5184,7 @@ ZEND_METHOD(ReflectionClass, newInstance)
51845184

51855185
if (!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
51865186
zend_throw_exception_ex(reflection_exception_ptr, 0, "Access to non-public constructor of class %s", ZSTR_VAL(ce->name));
5187+
zend_object_store_ctor_failed(Z_OBJ_P(return_value));
51875188
zval_ptr_dtor(return_value);
51885189
RETURN_NULL();
51895190
}
@@ -5257,6 +5258,7 @@ ZEND_METHOD(ReflectionClass, newInstanceArgs)
52575258
if (constructor) {
52585259
if (!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
52595260
zend_throw_exception_ex(reflection_exception_ptr, 0, "Access to non-public constructor of class %s", ZSTR_VAL(ce->name));
5261+
zend_object_store_ctor_failed(Z_OBJ_P(return_value));
52605262
zval_ptr_dtor(return_value);
52615263
RETURN_NULL();
52625264
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
ReflectionClass::newInstance(Args)() must not call the destructor when the constructor is not public
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
private function __construct() {}
8+
9+
public function __destruct() {
10+
echo "dtor\n";
11+
}
12+
}
13+
14+
$rc = new ReflectionClass(A::class);
15+
try {
16+
$rc->newInstance();
17+
} catch (Throwable $e) {
18+
echo $e::class, ": ", $e->getMessage(), "\n";
19+
}
20+
try {
21+
$rc->newInstanceArgs();
22+
} catch (Throwable $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
26+
?>
27+
--EXPECT--
28+
ReflectionException: Access to non-public constructor of class A
29+
ReflectionException: Access to non-public constructor of class A

0 commit comments

Comments
 (0)