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

use calloc(3) instead of malloc(3) & memset(3) ? #312

Closed
fluca1978 opened this issue Aug 19, 2022 · 1 comment
Closed

use calloc(3) instead of malloc(3) & memset(3) ? #312

fluca1978 opened this issue Aug 19, 2022 · 1 comment

Comments

@fluca1978
Copy link
Collaborator

Partially related to #190 , the source code is filled with memory allocations and zero filling like the following one:

ptr = malloc( ... );
memset( ptr, 0, ...);

wouldn't be possible to substitute this with calloc(3)? According to the documentation:

The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated  memory.        The  memory  is set to zero.

As an example, consider https://github.com/agroal/pgagroal/blob/master/src/libpgagroal/utils.c#L125:

an = malloc(size);
memset(an, 0, size);

would become

an = calloc(1, size);

Another advantage, that should not happen in pgagroal, is that calloc detects size overflows (integer computation), while malloc does not.

@jesperpedersen
Copy link
Collaborator

Internally calloc checks for overflow as you said, so that is the main difference. Either way I would say, since we don't have memset in the message read/write path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants