https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104262
Bug ID: 104262 Summary: -fsanitize=address false alarm with aligned_alloc Product: gcc Version: 11.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: sanitizer Assignee: unassigned at gcc dot gnu.org Reporter: eggert at cs dot ucla.edu CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org, jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at gcc dot gnu.org Target Milestone: --- Created attachment 52304 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52304&action=edit Patch for -fsanitize=address aligned_alloc bug The following program (adapted from an experimental version of GNU Coreutils) strictly conforms to C11 and shows a useful pattern of allocating a struct with a flexible array member: #include <stdalign.h> #include <stddef.h> #include <stdlib.h> struct namelist { struct namelist *next; char name[]; } *p; int main (void) { p = aligned_alloc (alignof (struct namelist), offsetof (struct namelist, name) + 3); if (!p) return 1; p->next = 0; p->name[0] = 'a'; p->name[1] = 'b'; p->name[2] = 0; return 0; } However, when I compile it with gcc 11.2.1 20211203 (Red Hat 11.2.1-7) using the command 'gcc -fsanitize=address' it crashes, with the following diagnostic: ================================================================= ==6049==ERROR: AddressSanitizer: invalid alignment requested in aligned_alloc: 8, alignment must be a power of two and the requested size 0xb must be a multiple of alignment (thread T0) #0 0x7fbf0f10b32f in aligned_alloc (/lib64/libasan.so.6+0xaf32f) #1 0x401198 in main (/home/eggert/junk/a.out+0x401198) #2 0x7fbf0ee7f55f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 ==6049==HINT: if you don't care about these errors you may set allocator_may_return_null=1 SUMMARY: AddressSanitizer: invalid-aligned-alloc-alignment (/lib64/libasan.so.6+0xaf32f) in aligned_alloc ==6049==ABORTING Proposed (untested) patch attached. Unfortunately I'm not plugged into the relationship between GCC and Clang compiler-rt, so I don't know how you manage patches to this part of the GCC source code or do testcases or cleanups etc. However, please install something like this patch into GCC's copy of the AddressSanitizer library. Thanks.