From f2518147794cf631e35d1a7e186656770b14a2ca Mon Sep 17 00:00:00 2001 From: Jason Evans Date: Tue, 29 Dec 2009 00:09:15 -0800 Subject: [PATCH] Handle 0-size allocation requests in posix_memalign(). --- jemalloc/src/jemalloc.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/jemalloc/src/jemalloc.c b/jemalloc/src/jemalloc.c index be6cdecc..1644ee46 100644 --- a/jemalloc/src/jemalloc.c +++ b/jemalloc/src/jemalloc.c @@ -5876,7 +5876,7 @@ malloc(size_t size) if (malloc_init()) { ret = NULL; - goto RETURN; + goto OOM; } if (size == 0) { @@ -5886,6 +5886,14 @@ malloc(size_t size) size = 1; #ifdef JEMALLOC_SYSV else { +# ifdef JEMALLOC_XMALLOC + if (opt_xmalloc) { + malloc_message("", + ": Error in malloc(): invalid size 0\n", "", + ""); + abort(); + } +# endif ret = NULL; goto RETURN; } @@ -5894,7 +5902,7 @@ malloc(size_t size) ret = imalloc(size); -RETURN: +OOM: if (ret == NULL) { #ifdef JEMALLOC_XMALLOC if (opt_xmalloc) { @@ -5907,6 +5915,9 @@ RETURN: errno = ENOMEM; } +#ifdef JEMALLOC_SYSV +RETURN: +#endif #ifdef JEMALLOC_TRACE if (opt_trace) trace_malloc(ret, size); @@ -5923,6 +5934,29 @@ posix_memalign(void **memptr, size_t alignment, size_t size) if (malloc_init()) result = NULL; else { + if (size == 0) { +#ifdef JEMALLOC_SYSV + if (opt_sysv == false) +#endif + size = 1; +#ifdef JEMALLOC_SYSV + else { +# ifdef JEMALLOC_XMALLOC + if (opt_xmalloc) { + malloc_message("", + ": Error in posix_memalign(): " + "invalid size 0\n", "", ""); + abort(); + } +# endif + result = NULL; + *memptr = NULL; + ret = 0; + goto RETURN; + } +#endif + } + /* Make sure that alignment is a large enough power of 2. */ if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void *)) {