[PATCH] Unix datagram socket messaging

Stefan (metze) Metzmacher metze at samba.org
Thu Apr 17 01:23:50 MDT 2014

Hi Volker,

>> When this breaks, hasn't all the of Samba and all the other software
>> written for BSD sockets also broken?  
> That's why I was asking how this is supposed to work.
> My understanding of the C standard snippets that I pasted
> indicates that all of this depends upon undefined C
> behaviour. In theory, a highly optimized compiler could
> break a lot of socket code in the future. It is unlikely to
> happen, so the void * cast is probably fine for the majority
> of cases right now.
> Apart from that, I would really like to know how this is
> supposed to work without depending on behaviour that
> compilers might break without us knowing. I'm doing C coding
> for more than 20 years now, and I still don't understand the
> corner cases of the language. That's why I am interested in
> this purely academic question :-)

My guess is that the (void *) cast tells the compiler to avoid the

The gcc manpage says (in the -fstrict-aliasing section):

	... In particular, an object of one type is assumed never to reside at the
	same address as an object of a different type, unless the types are
almost the same....

And in this case the structures are forced (by special __SOCKADDR*
defines in the headers)
to be "almost the same".

	... Even with -fstrict-aliasing, type-punning is allowed, provided the
memory is accessed
        through the union type....

So the union solution would also work.


More information about the samba-technical mailing list