https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89133
Bug ID: 89133 Summary: bogus -Wcast-align=strict for a member of an aligned struct or union Product: gcc Version: 9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- Assigning the address of an overaligned member of a struct or union that is of an otherwise less aligned type to a pointer to a type with the same alignment triggers a -Wcast-align=strict false positive. The alignment of the member must be the same as that of the pointed-to type regardless of the target in order to ensure the alignment of the next member which of the pointed-to type. Note that because the member whose address is cast is the first member, it's necessarily at the same address and thus has the same alignment as the struct itself for which the warning is not issued. Clang issues the same false positive here. $ cat u.c && gcc -S -Wall -Wextra -Wcast-align=strict u.c union { char c; long long x; } u; long long *p0 = (long long*)&u; long long *q0 = (long long*)&u.x; long long *r0 = (long long*)&u.c; struct { char c; long long x; } s; long long *p1 = (long long*)&s; long long *q1 = (long long*)&s.x; long long *r1 = (long long*)&s.c; u.c:9:17: warning: cast increases required alignment of target type [-Wcast-align] 9 | long long *r0 = (long long*)&u.c; | ^ u.c:19:17: warning: cast increases required alignment of target type [-Wcast-align] 19 | long long *r1 = (long long*)&s.c; | ^