Skip to content

Use free() for memory allocated with malloc. #2513

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
Mar 11, 2019
Merged

Conversation

rsgowman
Copy link
Member

Rather than delete, which technically results in undefined behaviour.

Rather than delete, which technically results in undefined behaviour.
@rsgowman rsgowman self-assigned this Mar 11, 2019
@rsgowman rsgowman requested a review from wilhuff March 11, 2019 16:21
@rsgowman rsgowman assigned wilhuff and unassigned rsgowman Mar 11, 2019
Copy link
Contributor

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -73,7 +74,7 @@ class String : public util::Comparable<String> {
}

~String() {
delete bytes_;
std::free(bytes_);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch. Do you think it would be worth moving this destructor into the .cc to avoid bleeding #include <cstdlib> into all consumers?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe? Realistically, stdlib.h is pretty much guaranteed to be pulled in regardless. But another decent reason to move it might be to keep the malloc/free in the same file (so it's more obvious when a mismatch occurs.) Moved.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a much better reason than the one I offered :-).

@wilhuff wilhuff assigned rsgowman and unassigned wilhuff Mar 11, 2019
@rsgowman rsgowman merged commit b2bcf8f into master Mar 11, 2019
@rsgowman rsgowman deleted the rsgowman/trivial_ub_fix branch March 11, 2019 18:40
@firebase firebase locked and limited conversation to collaborators Oct 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants