Skip to content

Commit

Permalink
Merge pull request #142 from davidozog/pr/alloc_size_zero_fix
Browse files Browse the repository at this point in the history
Add zero-size/null-ptr checks to mem managment
  • Loading branch information
yfguo authored Aug 31, 2024
2 parents 0ffb5a9 + 1bcd904 commit ee5cf11
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/internal/mem_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ void *OSHMPI_malloc(size_t size)
{
void *ptr = NULL;

if (size == 0)
return ptr;

OSHMPI_THREAD_ENTER_CS(&OSHMPI_global.symm_heap_mspace_cs);
ptr = mspace_malloc(OSHMPI_global.symm_heap_mspace, size);
OSHMPI_THREAD_EXIT_CS(&OSHMPI_global.symm_heap_mspace_cs);
Expand Down Expand Up @@ -49,6 +52,9 @@ void *OSHMPI_realloc(void *ptr, size_t size)
{
void *rptr = NULL;

if (size == 0 && ptr == NULL)
return ptr;

OSHMPI_THREAD_ENTER_CS(&OSHMPI_global.symm_heap_mspace_cs);
rptr = mspace_realloc(OSHMPI_global.symm_heap_mspace, ptr, size);
OSHMPI_THREAD_EXIT_CS(&OSHMPI_global.symm_heap_mspace_cs);
Expand All @@ -75,6 +81,9 @@ void *OSHMPI_calloc(size_t count, size_t size)
{
void *ptr = NULL;

if (size == 0)
return ptr;

OSHMPI_THREAD_ENTER_CS(&OSHMPI_global.symm_heap_mspace_cs);
ptr = mspace_calloc(OSHMPI_global.symm_heap_mspace, count, size);
OSHMPI_THREAD_EXIT_CS(&OSHMPI_global.symm_heap_mspace_cs);
Expand Down

0 comments on commit ee5cf11

Please sign in to comment.