Skip to content
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

Refactor malloc+memset to use calloc #2497

Closed
wants to merge 2 commits into from

Conversation

danielgustafsson
Copy link
Member

When a zeroed out allocation is required, most callsites in the code use calloc() rather than
malloc() followed by an explicit memset(). This PR refactors the remaining ones to use calloc() instead to make the code more consistent and to avoid the risk of subtle bugs when code is injected
between malloc() and memset() by accident. Spotted while reading code.

Also remove to calls to memset() on a calloc()d allocation which is already zeroed out.

Since calloc(3) returns a pointer to already zeroed memory, there
is no need to explicitly clear the allocated storage with memset.
When a zeroed out allocation is required, use calloc() rather than
malloc() followed by an explicit memset(). The result will be the
same, but using calloc() everywhere increases consistency in the
codebase and avoids the risk of subtle bugs when code is injected
between malloc and memset by accident.
@jay jay closed this in 94400f3 Apr 15, 2018
@jay
Copy link
Member

jay commented Apr 15, 2018

Thanks

@lock lock bot locked as resolved and limited conversation to collaborators Jul 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants