From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9FA383858C33; Sun, 3 Mar 2024 19:01:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9FA383858C33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709492502; bh=pLfW9Rm5PSIGCVx9jOjSzZQbix2tpZHYGO6hi9yLB3M=; h=From:To:Subject:Date:In-Reply-To:References:From; b=T/Q3pA17QZ8Ey8ICCoNdBmHRMyOKM+KVG6q02fDl5APjdf5iPFu2PBa2BwDshkFg6 dGuAOv6YK6yva618hL5UtWc4j73fGfJvw9U9zl9pOlB6WiUwgJYeIQhylMUs/55ThM NdJCUs3ZGHkI1F7BY7Z/NgKlVu9cT7Ux6S/KjtIc= 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: Sun, 03 Mar 2024 19:01:41 +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 #7 from Jakub Jelinek --- GCC actually doesn't diagnose on mere pointer assignment, but what triggers= the alignment check is &entry->offset even when the code later on just takes its address, entry must be sufficien= tly aligned, otherwise entry->offset is invalid. Under standard C rules, already forming the pointer would be UB, so somewhe= re in the caller when you prepare what to pass to the f function. If you want something that will still be invalid C, but will not trigger UBSAN errors, then e.g. unsigned long long h(struct dir_entry *entry) { return get_unaligned((unsigned long long *) (((char *) entry) + offsetof (struct dir_entry, offset))); } will do. If you want something that will be valid even in C, don't pass struct dir_e= ntry *entry argument, but void *entry instead, and use e.g. __get_unaligned_t(__typeof(((struct dir_entry *)0)->offset), ((char *)entry)+offsetof(struct dir_entry, offset))) You can surely hide that all under some macro.=