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.

Reply via email to