Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2022
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
30 changes: 14 additions & 16 deletions ext/random/randomizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
RETURN_THROWS();
if (param_engine != NULL) {
ZVAL_COPY(&engine, param_engine);
} else {
/* Create default RNG instance */
object_init_ex(&engine, random_ce_Random_Engine_Secure);
}

/* 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);
}
zend_update_property(random_ce_Random_Randomizer, Z_OBJ_P(ZEND_THIS), "engine", strlen("engine"), &engine);

ZVAL_OBJ(&zengine_object, engine_object);
OBJ_RELEASE(Z_OBJ_P(&engine));
Copy link
Member Author

Choose a reason for hiding this comment

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

Should this be zval_ptr_dtor() instead?

Copy link
Member

Choose a reason for hiding this comment

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

OBJ_RELEASE is fine IMHO, as it prevents some indirection which would boils down to the same code

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know much about it, but from what I've read, OBJ_RELEASE should be fine.


zend_update_property(random_ce_Random_Randomizer, Z_OBJ_P(ZEND_THIS), "engine", strlen("engine"), &zengine_object);
if (EG(exception)) {
RETURN_THROWS();
}

randomizer_common_init(randomizer, engine_object);
randomizer_common_init(randomizer, Z_OBJ_P(&engine));
}
/* }}} */

Expand Down
23 changes: 17 additions & 6 deletions ext/random/tests/03_randomizer/construct_twice.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,39 @@ final class UserEngine implements \Random\Engine

try {
(new \Random\Randomizer())->__construct();
} catch (\BadMethodCallException $e) {
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
$r = new \Random\Randomizer(new \Random\Engine\Xoshiro256StarStar());
$r->__construct(new \Random\Engine\PcgOneseq128XslRr64());
} catch (\BadMethodCallException $e) {
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
$r = new \Random\Randomizer(new \UserEngine());
$r->__construct(new \UserEngine());
} catch (\BadMethodCallException $e) {
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
$r = new \Random\Randomizer(new \Random\Engine\Xoshiro256StarStar());
$r->__construct(new \UserEngine());
} catch (\Error $e) {
echo $e->getMessage(), PHP_EOL;
}

var_dump($r->engine::class);

die('success');
?>
--EXPECT--
Cannot call constructor twice
Cannot call constructor twice
Cannot call constructor twice
Cannot modify readonly property Random\Randomizer::$engine
Cannot modify readonly property Random\Randomizer::$engine
Cannot modify readonly property Random\Randomizer::$engine
Cannot modify readonly property Random\Randomizer::$engine
string(32) "Random\Engine\Xoshiro256StarStar"
success