This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [pushed] Fix struct sockaddr/sockaddr_in/sockaddr_un strict aliasing violations
- From: Eli Zaretskii <eliz at gnu dot org>
- To: Pedro Alves <palves at redhat dot com>
- Cc: gdb-patches at sourceware dot org
- Date: Sat, 07 Mar 2015 20:52:47 +0200
- Subject: Re: [pushed] Fix struct sockaddr/sockaddr_in/sockaddr_un strict aliasing violations
- Authentication-results: sourceware.org; auth=none
- References: <1425750266-14385-1-git-send-email-palves at redhat dot com> <83r3t0lmb9 dot fsf at gnu dot org> <54FB4162 dot 5090601 at redhat dot com>
- Reply-to: Eli Zaretskii <eliz at gnu dot org>
> 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.