Strict aliasing rule strikes back

Andrey Pokrovskiy wonder.mice at
Fri May 22 06:43:24 CEST 2015


When I include ev.h in my c++ code (gcc 4.9, -std=c++11 -Wall -Wextra
-Werror -pedantic-errors -O3) I get strict aliasing warning.

But I'm not saying that there is something wrong with libev and that
those warnings should be fixed asap (surprise-surprise).

I'm just curious why libev needs all those casts that produce that
warning in a first place. For example, why not to have it that way:

#define ev_init(ev,cb_) do {                   \
-  ((ev_watcher *)(void *)(ev))->active  =      \
-  ((ev_watcher *)(void *)(ev))->pending = 0;   \
+  (ev)->active  = \
+  (ev)->pending = 0;   \
  ev_set_priority ((ev), 0);                   \
  ev_set_cb ((ev), cb_);                       \
  ev_set_cb ((ev), cb_);                               \
} while (0)

Since ev must be a watcher, it will have "active" and "pending"
members and two-steps cast is not really necessary. Another example:

-# define ev_set_cb(ev,cb_)                   (ev_cb_ (ev) = (cb_), memmove (&
                          ((ev_watcher *)(ev))->cb, &ev_cb_ (ev),
sizeof (ev_cb_ (ev))))
+# define ev_set_cb(ev,cb_)                   (ev)->cb = (cb_)

Why to use memmove when "ev->cb" and "cb_" must have the same type and
are simply assignable? Curiously enough, I noticed that those
memmove's were added exactly to "fix a potential aliasing issue".

I'm sure all those things are done for a reason, and I'm wondering
what those reasons are. Maybe somebody could write a few lines
explaining why libev needs those casts and memmove's?

More information about the libev mailing list