This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: -Wstrict-aliasing in 2.95/6?


> When it comes to type based aliasing, the dynamic object doesn't really
> matter.  If you access the wrong kind of dynamic object, that's a bug
> but not due to type based aliasing.

This is exactly the kind of bug that are exposed by type-based alias
analysis. These bugs are also the source of all the discussion about
-fstrict-aliasing.

In the original example in the Linux kernel, an array of short was
accessed as an array of int (for more efficient copying).

> Would these criteria be enough for useful static warnings?

No. See below.

>           - Any conversion from pointer-to-integral
>             to pointer-to-non-integral type, and vice versa.

The following code is incorrect, but not detected by this warning:

void f1()
{
  int i = 4;
  short *pi = (short*)&i;
  *pi = 1;
}

OTOH, the following code is correct, but gives a warning:

void f2{
{
  int i = 4;
  void *pi = &i;
  *(int*)pi = 1;
}

>           - Any conversion from pointer-to-struct-A to
>             pointer-to-struct-B, where A's member list is not the
>             initial part of B's, and vice versa.

The examples are similar: Conversion from and to void* is not warned
about. Sometimes it is wrong, sometimes it is right. Type-based
analysis cannot tell which case is which.

Direct conversion between incompatible structures is always an
error. But then, it never happens in real programs, IMHO, so a warning
for it would rarely trigger.

Regards,
Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]