From 2e838bf84da94760fa49e132108e954a62815573 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Thu, 30 Jul 2026 10:29:38 -0700 Subject: [PATCH] [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 ce62aab97e58c52e6bb0972c49d6fd1aaa6a08a0, since hash-table bitmask implementation uses SSE2 intrinsics with 16-byte alignment. --- libc/test/UnitTest/HermeticTestUtils.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp index bcc42bed98eb..84b56e8252ea 100644 --- a/libc/test/UnitTest/HermeticTestUtils.cpp +++ b/libc/test/UnitTest/HermeticTestUtils.cpp @@ -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(ptr); + uintptr_t aligned_ptr_val = ((ptr_val + align - 1) / align) * align; + ptr = reinterpret_cast(aligned_ptr_val); void *mem = ptr; ptr += s; return static_cast(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;