Skip to content

random: Rely on free(NULL) being safe for random status freeing #10246

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
Jan 10, 2023

Conversation

TimWolla
Copy link
Member

@TimWolla TimWolla commented Jan 6, 2023

No description provided.

Comment on lines 264 to 266
if (status == NULL) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

Can status ever be NULL or should an assertion be added?

Copy link
Member Author

Choose a reason for hiding this comment

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

See https://chat.stackoverflow.com/transcript/message/55792527#55792527 for context. I'm used to free()-style functions being a noop if passed NULL and consider it a good thing, because is avoids checking for NULL in every place that frees something that might be absent. In this case there might be no such place, but still this should just work in case there is.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've just restructured the method a little, though. Instead of returning early, it checks for NULL before dereferencing status and just passes a possible-NULL status to pefree(). This pattern is a little more common for more complex structs.

@TimWolla TimWolla requested review from Girgias and cmb69 January 9, 2023 16:27
Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

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

I think we can assume that free() and friends handle NULL as nop, even though custom allocators (USE_ZEND_ALLOC=0) may not. It might be a good idea to document that assumption (and others, like the infallability) in the phpinternalsbook.

@TimWolla
Copy link
Member Author

It might be a good idea to document that assumption (and others, like the infallability) in the phpinternalsbook.

I've added an issue for that in the internals book repository.

@TimWolla TimWolla merged commit e7c0f4e into php:master Jan 10, 2023
@TimWolla TimWolla deleted the random-free branch January 10, 2023 17:47
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.

4 participants