Skip to content

Suspicious usage of malloc #166

Description

@sleeptightAnsiC

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 ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions