[PATCH 2] fix broken strict aliasing

Zsbán Ambrus ambrus at math.bme.hu
Sat Feb 20 18:20:26 CET 2010


I did look at those strict aliasing warnings a while back.  It seemed
to me that they're not bugs.

They're caused by the header declaring structures (specifically ev_*
and ev_watcher) that start with some common elements, casting a
pointer to one to a pointer top the other, and accessing the common
elements that way.  The compiler warns about that because the C
standard doesn't guarantee that the rest of the elements of the struct
doesn't change how the first elements are laid out in the struct, but
that isn't likely to happen on any real system (which has abi rules so
different compilers can interoperate).  In theory, one could avoid
these warnings by having the larger structures have the smaller
structures as their first member, not just sharing their first
members, but that might make the code uglier and is not worth imo.
(The strange thing is that in some places the ev.h header actually has
code to silence these same warnings, such as in the ev_init macro.)

The warnings are a bit annoying because they appear in user code that
expand macros from the ev.h header, and with gcc they were not
specific enough for me to easily tell that these are indeed caused by
the header doing these casts, not something in my code.

Incidentally, what bothers me more than these aliasing warnings is
that there's both a structure called struct ev_loop and a function
called ev_loop in the same header.  Apparently C++ does allow this --
I don't quite understand the rules, but then I don't understand the
rules that allow the double meaning of std::clog either.

Ambrus



More information about the libev mailing list