Skip to content

Fix memory leak on Randomizer::__construct() call twice #9091

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 12 commits into from
Jul 23, 2022

Conversation

zeriyoshi
Copy link
Contributor

Either way, updating the readonly property will result in an error, so an exception is thrown in accordance with the Phar implementation.

Closes GH-9089

@@ -80,7 +85,7 @@ PHP_METHOD(Random_Randomizer, __construct)

ZVAL_OBJ(&zengine_object, engine_object);

zend_update_property(random_ce_Random_Randomizer, Z_OBJ_P(ZEND_THIS), "engine", strlen("engine"), &zengine_object);
zend_update_property(random_ce_Random_Randomizer, Z_OBJ_P(ZEND_THIS), "engine", sizeof("engine") - 1, &zengine_object);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure how this change is related, but that's still strlen("engine") in the zend_read_property call near the end of this file

zengine = zend_read_property(randomizer->std.ce, &randomizer->std, "engine", strlen("engine"), 0, NULL);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. This is a digression from the purpose of this PR, but it was minor and has been corrected.
4afc9db

Copy link
Member

Choose a reason for hiding this comment

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

Please don't bundle unrelated changes into a PR, it distracts from the relevant parts. Can you please revert the strlen / sizeof changes?

I also disagree with using sizeof in principle: Modern compilers can optimize both. For older compilers the string is very short and it's also not in a hot path. strlen() is much more readable, because the - 1 is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right.
But, the current situation where it is not unified is not so good, so we will create a separate PR to unify it with strlen.

@@ -80,7 +85,7 @@ PHP_METHOD(Random_Randomizer, __construct)

ZVAL_OBJ(&zengine_object, engine_object);

zend_update_property(random_ce_Random_Randomizer, Z_OBJ_P(ZEND_THIS), "engine", strlen("engine"), &zengine_object);
zend_update_property(random_ce_Random_Randomizer, Z_OBJ_P(ZEND_THIS), "engine", sizeof("engine") - 1, &zengine_object);
Copy link
Member

Choose a reason for hiding this comment

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

Please don't bundle unrelated changes into a PR, it distracts from the relevant parts. Can you please revert the strlen / sizeof changes?

I also disagree with using sizeof in principle: Modern compilers can optimize both. For older compilers the string is very short and it's also not in a hot path. strlen() is much more readable, because the - 1 is not needed.

Copy link
Member

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

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

Thanks! Don't forget to add the NEWS entry before "Squash and Merge".

@zeriyoshi zeriyoshi merged commit 34b352d into php:master Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak when calling Randomizer::__construct() on an existing object
3 participants