[Bug sanitizer/114217] -fsanitize=alignment false positive with intended unaligned struct member access
jakub at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Mon Mar 4 07:54:11 GMT 2024
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114217
--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
BTW, with compilers from the last decade or so, it would be much better idea to
just use standard memcpy for get_unaligned/put_unaligned rather than messing
around with pointers to packed types. The compiler should optimize it
correctly to unaligned loads or stores itself when one operand is cast from
pointer to certain integral type and the size is the size of that type. Sure,
you'd need to still use the GNU ({ ... }) extension
#define __get_unaligned_t(type, ptr) ({
\
type __get_unaligned_t_x;
\
memcpy(&__get_unaligned_t_x, (ptr), sizeof (__get_unaligned_t_x));
\
__get_unaligned_t_x;
\
})
For the get_unaligned from structure element you'd then have something like
get_unaligned_member(type, member, ptr) macro where it would use
typeof (((type *)0)->member) and offsetof (type, member) and memcpy.
More information about the Gcc-bugs
mailing list