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
base: master
from

Conversation

Projects
None yet
2 participants
@danielgustafsson
Member

danielgustafsson commented Apr 14, 2018

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.

danielgustafsson added some commits Apr 14, 2018

Avoid zeroing allocated memory from calloc
Since calloc(3) returns a pointer to already zeroed memory, there
is no need to explicitly clear the allocated storage with memset.
Refactor malloc+memset to use calloc
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

This comment has been minimized.

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.