From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D73C93858D28; Mon, 4 Mar 2024 07:54:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D73C93858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709538851; bh=lzQqlb7CkF4zB828hdpIrL1vHyXJUr151y2YThKrLwk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Q+liVmVQf0tvxOgSKEJRHWaLomnGzNP1BptW/BINhkWSNbt1hRvzazVx6fYw/YtvE OAkzaIAWKIm+d7qw1PiLjfOAu4mcLWTlciTTtiUvS9thsOjGrzb0gyCvK6sLkZdI8h gFdXfLmBFfoBnQOUaf3VLf9I0r72qY+nGQrzLTqo= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug sanitizer/114217] -fsanitize=alignment false positive with intended unaligned struct member access Date: Mon, 04 Mar 2024 07:54:11 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: sanitizer X-Bugzilla-Version: 13.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: INVALID X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114217 --- Comment #10 from Jakub Jelinek --- BTW, with compilers from the last decade or so, it would be much better ide= a 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. Su= re, you'd need to still use the GNU ({ ... }) extension #define __get_unaligned_t(type, ptr) ({=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20 \ type __get_unaligned_t_x;=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20 \ memcpy(&__get_unaligned_t_x, (ptr), sizeof (__get_unaligned_t_x));= =20=20=20=20=20 \ __get_unaligned_t_x;=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20 \ }) 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.=