This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


> 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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]