diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 484127dccd..22cdb5a117 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -643,21 +643,18 @@ pub unsafe extern "C" fn posix_memalign( alignment: size_t, size: size_t, ) -> c_int { - const void_ptr_size: usize = mem::size_of::<*mut c_void>(); + const VOID_PTR_SIZE: usize = mem::size_of::<*mut c_void>(); - /* Check that alignment is: - * a) a multiple of sizeof(void *) - * b) a power-of-two multiple of sizeof(void *). Integer division as - * below gives the correct result once a) is ensured. */ - if alignment % void_ptr_size == 0 && (alignment / void_ptr_size).is_power_of_two() { + if alignment % VOID_PTR_SIZE == 0 && alignment.is_power_of_two() { let ptr = platform::alloc_align(size, alignment); - if !ptr.is_null() { - *memptr = ptr; - 0 - } else { + *memptr = ptr; + if ptr.is_null() { ENOMEM + } else { + 0 } } else { + *memptr = ptr::null_mut(); EINVAL } }