[pushed] Fix struct sockaddr/sockaddr_in/sockaddr_un strict aliasing violations

Eli Zaretskii eliz@gnu.org
Sat Mar 7 18:53:00 GMT 2015


> Date: Sat, 07 Mar 2015 18:20:18 +0000
> From: Pedro Alves <palves@redhat.com>
> CC: gdb-patches@sourceware.org
> 
> Those are BSD socket types, they've been this way ever since BSD
> invented them.  The structs are not type compatible, even though
> they have some common fields that are are put at the same offsets,
> by design.
> 
> See e.g.:
> 
>   http://stackoverflow.com/questions/1429645/how-to-cast-sockaddr-storage-and-avoid-breaking-strict-aliasing-rules

IMO, the right way to handle this is to have our own struct (NOT
union!) with the data, and fill the correct struct from it, field by
field, when we need to pass it to a library function.

A union is just a type-cast in disguise; if using it is wrong, we
shouldn't try.  (And what if one of these days GCC will acquire a
capability to see through them?)

> It's not a C++ restriction.  The old code was invalid C code.

I'm talking about the new code.



More information about the Gdb-patches mailing list