[Spice-devel] [PATCH v2] remove a warning building a test

Frediano Ziglio fziglio at redhat.com
Fri May 20 09:43:39 UTC 2016


> 
> On 05/18/2016 07:14 AM, Christophe Fergeau wrote:
> > On Wed, May 18, 2016 at 12:46:03PM +0300, Uri Lublin wrote:
> >>
> >> In man cmsg examples I see they use a pointer:
> >> int *pi = (int*) CMSG_DATA(cmsg);
> >> *fd = *pi;
> > 
> > It seems to work this way, but I'm not sure why this works ;)
> > 
> 
> 
> I read an excelent post from Thiago Macieira (Qt) about this types of
> warnings a long time ago, maybe it will make things clearer for you too...
> 
> http://blog.qt.io/blog/2011/06/10/type-punning-and-strict-aliasing/
> 

Actually there are some mistake in the article.
Situation is sometimes better sometimes worst.

In this

  int i = 42;
  short s = *(short*)&i;

compiler could rewrite to something like

  int i;
  short s = *(short*)&i;
  i = 42;

so s can be anything.

  short s[2] = { 0, 42 };
  int i = *(int *)s;

still here can be anything and the crash is not due to strict aliasing.

I think the union is kind of a missing specification inside the standard
but really should work. Also gcc has a may_alias attribute which should
have been added to the standard (or similar specification). Considering that
C was introduced to help with stuff like kernels it's a big omission.

The inline of operator>> is a bug in the implementation, the standard clearly
state that there are aliasing in that code (as char* and or similar pointers
are used).

The List* -> Node* cast is a programming mistake, the solution is using
a Node** for the tail.

This C code - happily - don't compile anymore:

    int i;
    char *p1, *p2;
    ...
    *i = i + p1 + p2;

Linux Kernel and other software disable the strict aliasing.

Frediano


More information about the Spice-devel mailing list