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

--- Comment #11 from Pedro Alves <palves at redhat dot com> ---
> That is certainly not fine from aliasing perspective, aliasing is not just 
> about the type of the field you access, but the whole access path, so if you
> use ((struct b *)ip)->j then ip should point to an object with effective type 
> of struct b.

Sorry, bad choice of words (and I'm not that familiar with gcc internals, as
you'll have guessed).  I meant "pointer type conversion" perspective.  IOW,
this particular particular ip can never point to a struct b, but not because 
the pointers' static types are incompatible.

> use ((struct b *)ip)->j then ip should point to an object with effective type 
> of struct b.

> But, I'm afraid we can't warn about this for -Wstrict-aliasing=3, because that
> would lead to too many false positives.

I see three distinct cases:

#1 - 'ip' has dynamic type 'int *' (can also be some "struct *" that has int as
first field)

 extern int *ip;
 ((struct b *)ip)->j;

#2 - 'ip' has dynamic type "final" 'int *'

 static struct b b;
 struct a *ap=(struct a *)&b;
 int *ip = &ap->k;
 ((struct b *)ip)->j;

#3 - 'ip' has dynamic type "final" 'struct b *'.

 static struct b b;
 struct a *ap=(struct a *)&b;
 int *ip = &ap->i;
 ((struct b *)ip)->j;

Sounds like gcc doesn't distinguish #1 from #2.  IOW, it's missing some kind of
tracking whether the dynamic type is "final"?

Reply via email to