Fix bug GH-14456: Attempting to initialize class with private constructor calls destructor

Closes GH-14469
This commit is contained in:
Gina Peter Banyard 2024-06-04 17:24:19 +01:00
parent 86b93bc479
commit cdb7677b38
No known key found for this signature in database
GPG Key ID: 3306078E3194AEBD
3 changed files with 23 additions and 0 deletions

2
NEWS
View File

@ -8,6 +8,8 @@ PHP NEWS
when running on Apple Silicon). (Manuel Kress)
. Fixed bug GH-14387 (Crash when stack walking in destructor of yielded from
values during Generator->throw()). (Bob)
. Fixed bug GH-14456 (Attempting to initialize class with private constructor
calls destructor). (Girgias)
- BCMatch:
. Fixed bug (bcpowmod() with mod = -1 returns 1 when it must be 0). (Girgias)

20
Zend/tests/gh14456.phpt Normal file
View File

@ -0,0 +1,20 @@
--TEST--
GH-14456: Attempting to initialize class with private constructor calls destructor
--FILE--
<?php
class PrivateUser {
private function __construct() {}
public function __destruct() {
echo 'Destructor for ', __CLASS__, PHP_EOL;
}
}
try {
new PrivateUser();
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
Error: Call to private PrivateUser::__construct() from global scope

View File

@ -1622,6 +1622,7 @@ ZEND_API zend_function *zend_std_get_constructor(zend_object *zobj) /* {{{ */
if (UNEXPECTED(constructor->op_array.fn_flags & ZEND_ACC_PRIVATE)
|| UNEXPECTED(!zend_check_protected(zend_get_function_root_class(constructor), scope))) {
zend_bad_constructor_call(constructor, scope);
zend_object_store_ctor_failed(zobj);
constructor = NULL;
}
}