diff --git a/malloc/malloc.c b/malloc/malloc.c index 2ba1fee144..386fb5a4cb 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3416,6 +3416,12 @@ _mid_memalign (size_t alignment, size_t bytes, void *address) mstate ar_ptr; void *p; + if (!powerof2 (alignment)) + { + __set_errno (EINVAL); + return 0; + } + /* If we need less alignment than we give anyway, just relay to malloc. */ if (alignment <= MALLOC_ALIGNMENT) return __libc_malloc (bytes); @@ -3424,24 +3430,6 @@ _mid_memalign (size_t alignment, size_t bytes, void *address) if (alignment < MINSIZE) alignment = MINSIZE; - /* If the alignment is greater than SIZE_MAX / 2 + 1 it cannot be a - power of 2 and will cause overflow in the check below. */ - if (alignment > SIZE_MAX / 2 + 1) - { - __set_errno (EINVAL); - return 0; - } - - - /* Make sure alignment is power of 2. */ - if (!powerof2 (alignment)) - { - size_t a = MALLOC_ALIGNMENT * 2; - while (a < alignment) - a <<= 1; - alignment = a; - } - if (SINGLE_THREAD_P) { p = _int_memalign (&main_arena, alignment, bytes);