options/ansi: return nullptr for bad align in alloc_aligned
@@ -185,7 +185,11 @@ void srand(unsigned int s) {
void *aligned_alloc(size_t alignment, size_t size) {
void *ptr;
- // posix_memalign requires that the alignment is a multiple of sizeof(void *).
+ // alignment must be a power of two, and size % alignment must be 0
+ if (alignment & (alignment - 1) || size & (alignment - 1))
+ return nullptr;
+
+ // posix_memalign requires that the alignment is a multiple of sizeof(void *)
if (alignment < sizeof(void *))
alignment = sizeof(void *);
