Skip to content

Commit ee5cf11

Browse files
authored
Merge pull request #142 from davidozog/pr/alloc_size_zero_fix
Add zero-size/null-ptr checks to mem managment
2 parents 0ffb5a9 + 1bcd904 commit ee5cf11

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/internal/mem_impl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ void *OSHMPI_malloc(size_t size)
1111
{
1212
void *ptr = NULL;
1313

14+
if (size == 0)
15+
return ptr;
16+
1417
OSHMPI_THREAD_ENTER_CS(&OSHMPI_global.symm_heap_mspace_cs);
1518
ptr = mspace_malloc(OSHMPI_global.symm_heap_mspace, size);
1619
OSHMPI_THREAD_EXIT_CS(&OSHMPI_global.symm_heap_mspace_cs);
@@ -49,6 +52,9 @@ void *OSHMPI_realloc(void *ptr, size_t size)
4952
{
5053
void *rptr = NULL;
5154

55+
if (size == 0 && ptr == NULL)
56+
return ptr;
57+
5258
OSHMPI_THREAD_ENTER_CS(&OSHMPI_global.symm_heap_mspace_cs);
5359
rptr = mspace_realloc(OSHMPI_global.symm_heap_mspace, ptr, size);
5460
OSHMPI_THREAD_EXIT_CS(&OSHMPI_global.symm_heap_mspace_cs);
@@ -75,6 +81,9 @@ void *OSHMPI_calloc(size_t count, size_t size)
7581
{
7682
void *ptr = NULL;
7783

84+
if (size == 0)
85+
return ptr;
86+
7887
OSHMPI_THREAD_ENTER_CS(&OSHMPI_global.symm_heap_mspace_cs);
7988
ptr = mspace_calloc(OSHMPI_global.symm_heap_mspace, count, size);
8089
OSHMPI_THREAD_EXIT_CS(&OSHMPI_global.symm_heap_mspace_cs);

0 commit comments

Comments
 (0)