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

Suspicious usage of malloc #166

Open
sleeptightAnsiC opened this issue Jan 11, 2025 · 0 comments
Open

Suspicious usage of malloc #166

sleeptightAnsiC opened this issue Jan 11, 2025 · 0 comments

Comments

@sleeptightAnsiC
Copy link

Hi,

I've been reading the bam's source tree lately and noticed a bit strange pattern:

  • result of malloc is rarely being checked, neither in the place where the call happens nor somewhere else.
  • when memory needs to be zero-initialized the malloc+memset is being used instead of calloc

For example:

bam/src/statcache.c

Lines 28 to 35 in a44a2c7

struct STATCACHE* statcache_create()
{
struct STATCACHE* statcache = malloc(sizeof(struct STATCACHE));
memset(statcache, 0, sizeof(struct STATCACHE));
statcache->heap = mem_create();
return statcache;
}

In this case sizeof(struct STATCACHE) is pretty big so calloc would be beneficial.
(see https://stackoverflow.com/questions/2688466/why-mallocmemset-is-slower-than-calloc )
Also, in OOM situation, this will corrupt memory and cause a crash later.

Is there a reason for this? Are there any platforms that do not support calloc ?

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

1 participant