[libc] Fix aligned_alloc used in hermetic tests. (#213041)

Use the provided alignment for the pointer to the returned
memory chunk instead of the size.

This fixes occasional segfaults in `hsearch` tests after
ce62aab97e, since
hash-table bitmask implementation uses SSE2 intrinsics with 16-byte
alignment.
This commit is contained in:
Alexey Samsonov 2026-07-30 10:29:38 -07:00 committed by GitHub
parent 57a38f251b
commit 2e838bf84d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -73,7 +73,9 @@ int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
void *aligned_alloc(size_t align, size_t s) {
if (align & (align - 1)) // Must be power of 2
return nullptr;
s = ((s + align - 1) / align) * align;
uintptr_t ptr_val = reinterpret_cast<uintptr_t>(ptr);
uintptr_t aligned_ptr_val = ((ptr_val + align - 1) / align) * align;
ptr = reinterpret_cast<uint8_t *>(aligned_ptr_val);
void *mem = ptr;
ptr += s;
return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;