From 1bcd904c5af235d48ec6c844043f12e735add587 Mon Sep 17 00:00:00 2001 From: David Ozog Date: Mon, 9 Oct 2023 10:26:43 -0700 Subject: [PATCH] Add zero-size/null-ptr checks to mem managment --- src/internal/mem_impl.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/internal/mem_impl.c b/src/internal/mem_impl.c index 6a651c6..5b920e7 100644 --- a/src/internal/mem_impl.c +++ b/src/internal/mem_impl.c @@ -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); @@ -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); @@ -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);