public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/97321] New: add warning for pointer casts that may lead to aliasing violation when dereferenced
@ 2020-10-07 14:17 nsz at gcc dot gnu.org
  2020-10-07 14:35 ` [Bug c/97321] " rguenth at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: nsz at gcc dot gnu.org @ 2020-10-07 14:17 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97321
           Summary: add warning for pointer casts that may lead to
                    aliasing violation when dereferenced
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nsz at gcc dot gnu.org
  Target Milestone: ---

consider:

int f(unsigned char **);

int g(char *p)
{
    return f((unsigned char **)&p);
}

such code is almost surely wrong (if f dereferences its
argument) this is a common mistake and it seems gcc-11
will optimize such code more aggressively which can lead
to broken behavior, see bug 97264.

so it would be useful to simply warn about casts between
pointer types that cannot alias. e.g.:

"warning: dangerous cast from `char **` to `unsigned char **` can lead to
aliasing violation [-Wpointer-cast]"

does not have to be in -Wall, but the current aliasing
warnings are too weak to catch bugs like in the example.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c/97321] add warning for pointer casts that may lead to aliasing violation when dereferenced
  2020-10-07 14:17 [Bug c/97321] New: add warning for pointer casts that may lead to aliasing violation when dereferenced nsz at gcc dot gnu.org
@ 2020-10-07 14:35 ` rguenth at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-10-07 14:35 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |WORKSFORME

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
> gcc t5.c -O2 -Wstrict-aliasing=2
t5.c: In function 'g':
t5.c:5:32: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
    5 |     return f((unsigned char **)&p);
      |                                ^~

so it already works.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-10-07 14:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-07 14:17 [Bug c/97321] New: add warning for pointer casts that may lead to aliasing violation when dereferenced nsz at gcc dot gnu.org
2020-10-07 14:35 ` [Bug c/97321] " rguenth at gcc dot gnu.org

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).