From ef02104fd318c1fe071754c630e33da8ebcdfb43 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sun, 23 Jun 2019 10:59:47 -0700 Subject: [PATCH] fix posix_memalign according to issue #27 --- src/alloc-override.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/alloc-override.c b/src/alloc-override.c index 38e3bfe1..2e1723ab 100644 --- a/src/alloc-override.c +++ b/src/alloc-override.c @@ -128,9 +128,14 @@ size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p) void cfree(void* p) MI_FORWARD0(mi_free, p) int posix_memalign(void** p, size_t alignment, size_t size) { - if (alignment % sizeof(void*) != 0) { *p = NULL; return EINVAL; }; - *p = mi_malloc_aligned(size, alignment); - return (*p == NULL ? ENOMEM : 0); + // TODO: the spec says we should return EINVAL also if alignment is not a power of 2. + // The spec also dictates we should not modify `*p` on an error. (issue#27) + // + if (alignment % sizeof(void*) != 0 || p==NULL) return EINVAL; + void* q = mi_malloc_aligned(size, alignment); + if (q==NULL && size != 0) return ENOMEM; + *p = q; + return 0; } void* memalign(size_t alignment, size_t size) {