[zeromq-dev] Strict aliasing problem

Bjorn Reese breese at mail1.stofanet.dk
Thu May 17 11:14:38 CEST 2012

I cannot compile the master branch with g++ 4.4 because of this commit:


I get errors like this:

options.cpp: In member function ‘int zmq::options_t::setsockopt(int, 
const void*, size_t)’:
options.cpp:328:52: error: dereferencing type-punned pointer will break 
strict-aliasing rules

Actually, these kind of errors is what the abovementioned commit is
trying to fix, but it did not get it right.

I can see three possible solutions:

1. As the only standards-compliant way "casting" between
    pointer-to-object and pointer-to-function is through a union, the
    zmq_monitor_fn type could be changed to something like:

    typedef union
      void *object;
      void (*function)(void *s, int event, zmq_event_data_t *data);
    } zmq_monitor_fn;

    and the various places where the monitor function is used, must be
    changed appropriately.

    This is not a good solution because it creates an awkward API for
    callback functions.

2. Add a separate zmq_setsockopt() that takes a pointer-to-function
    instead of pointer-to-object.

    It might be even better to add explicit getters and setters for the
    various options, rather than having to rely on va_args to create a
    "fake" overloading in C.

3. The problem with strict aliasining is more or less theoretical. Posix
    requires that pointer-to-object and pointer-to-function must have the
    same representation. We could therefore lower the strictness of the
    aliasing checking for g++. I have attached a patch just in case.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: aliasing.diff
Type: text/x-patch
Size: 665 bytes
Desc: not available
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120517/6fde00c6/attachment.bin>

More information about the zeromq-dev mailing list