Clean up the implementation of Randomizer::__construct() (#9222)

* Verify that the engine doesn't change in construct_twice.phpt

* Clean up the implementation of Randomizer::__construct()

Instead of manually checking whether the constructor was already called, we
rely on the `readonly` modifier of the `$engine` property.

Additionally use `object_init_ex()` instead of manually calling
`->create_object()`.
This commit is contained in:
Tim Düsterhus 2022-08-02 17:30:18 +02:00 committed by GitHub
parent 54e406cc50
commit a6922fdecd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 24 deletions

View file

@ -62,32 +62,30 @@ static inline void randomizer_common_init(php_random_randomizer *randomizer, zen
PHP_METHOD(Random_Randomizer, __construct)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
zend_object *engine_object = NULL;
zval zengine_object;
zval engine;
zval *param_engine = NULL;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_OBJ_OF_CLASS_OR_NULL(engine_object, random_ce_Random_Engine);
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(param_engine, random_ce_Random_Engine);
ZEND_PARSE_PARAMETERS_END();
if (randomizer->algo) {
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot call constructor twice");
if (param_engine != NULL) {
ZVAL_COPY(&engine, param_engine);
} else {
/* Create default RNG instance */
object_init_ex(&engine, random_ce_Random_Engine_Secure);
}
zend_update_property(random_ce_Random_Randomizer, Z_OBJ_P(ZEND_THIS), "engine", strlen("engine"), &engine);
OBJ_RELEASE(Z_OBJ_P(&engine));
if (EG(exception)) {
RETURN_THROWS();
}
/* Create default RNG instance */
if (!engine_object) {
engine_object = random_ce_Random_Engine_Secure->create_object(random_ce_Random_Engine_Secure);
/* No need self-refcount */
GC_DELREF(engine_object);
}
ZVAL_OBJ(&zengine_object, engine_object);
zend_update_property(random_ce_Random_Randomizer, Z_OBJ_P(ZEND_THIS), "engine", strlen("engine"), &zengine_object);
randomizer_common_init(randomizer, engine_object);
randomizer_common_init(randomizer, Z_OBJ_P(&engine));
}
/* }}} */