public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pinskia at gcc dot gnu.org" <gcc-bugzilla@gcc.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	[thread overview]
Message-ID: <bug-102937-4-MoWhpsqMkf@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-102937-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102937

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|FIXED                       |INVALID

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(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 = (char*)t;
a[n] = ...;
access *t

since you are doing basically:
char *a;
size_t *t = (size_t)&a;
*t = ....
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 = (char*)&a;
b[0] = ...
b[1] = ...
b[2] = ...
....
access a

There are other ways fixing the issue by using memcpy (or an union but an union
in this case is harder to use correctly) than extra assignments.

      parent reply	other threads:[~2021-10-25 21:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-25 21:02 [Bug c++/102937] New: " drohr at jwdt dot org
2021-10-25 21:17 ` [Bug c++/102937] " pinskia at gcc dot gnu.org
2021-10-25 21:19 ` pinskia at gcc dot gnu.org
2021-10-25 21:44 ` drohr at jwdt dot org
2021-10-25 21:52 ` pinskia at gcc dot gnu.org [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-102937-4-MoWhpsqMkf@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).