From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id ECD7B385AC3B; Mon, 25 Oct 2021 21:52:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ECD7B385AC3B From: "pinskia at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/102937] Miscompilation with -O3 and aliasing of char* and size_t Date: Mon, 25 Oct 2021 21:52:41 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: pinskia 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: resolution 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Oct 2021 21:52:42 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102937 Andrew Pinski changed: What |Removed |Added ---------------------------------------------------------------------------- Resolution|FIXED |INVALID --- Comment #4 from Andrew Pinski --- (In reply to David Rohr from comment #3) > thanks, makes me feel pretty stupid... > I thought it is not a problem since char* may alias, but apparently size_= t* > may not alias char**... if I understood correctly... No, you are still misunderstand aliasing rules. Basically you can access any type via a character type. That is: char *a =3D (char*)t; a[n] =3D ...; access *t since you are doing basically: char *a; size_t *t =3D (size_t)&a; *t =3D .... access a size_t only alias size_t and the signed version of what the type size_t was typedef of (size_t is unsigned). In the above case, you access "char*" as size_t which is undefined. if you instead accessed it as char rather than size_t then the code would be well defined. That is: char *a; char *b =3D (char*)&a; b[0] =3D ... b[1] =3D ... b[2] =3D ... .... access a There are other ways fixing the issue by using memcpy (or an union but an u= nion in this case is harder to use correctly) than extra assignments.=