[libvirt] [PATCH] Fix cast errors with clang
Roman Bogorodskiy
bogorodskiy at gmail.com
Tue Oct 21 14:25:29 UTC 2014
Eric Blake wrote:
> On 10/18/2014 10:41 AM, Roman Bogorodskiy wrote:
> > Build with clang fails with:
> >
> > CC util/libvirt_util_la-virsocketaddr.lo
> > util/virsocketaddr.c:904:17: error: cast from 'struct sockaddr *' to
> > 'struct sockaddr_in *' increases required alignment from 1 to 4
> > [-Werror,-Wcast-align]
> > inet4 = (struct sockaddr_in*) res->ai_addr;
> > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > util/virsocketaddr.c:909:17: error: cast from 'struct sockaddr *' to
> > 'struct sockaddr_in6 *' increases required alignment from 1 to 4
> > [-Werror,-Wcast-align]
> > inet6 = (struct sockaddr_in6*) res->ai_addr;
> > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 2 errors generated.
> >
> > Fix by introducing a union of the appropriate sturcts.
>
> s/sturcts/structs/
>
> > ---
> > src/util/virsocketaddr.c | 17 ++++++++++-------
> > 1 file changed, 10 insertions(+), 7 deletions(-)
> >
> > diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
> > index 5f54e68..162108c 100644
> > --- a/src/util/virsocketaddr.c
> > +++ b/src/util/virsocketaddr.c
> > @@ -892,22 +892,25 @@ virSocketAddrIsNumericLocalhost(const char *addr)
> > {
> > struct addrinfo *res;
> > struct in_addr tmp = { .s_addr = htonl(INADDR_LOOPBACK) };
> > - struct sockaddr_in *inet4;
> > - struct sockaddr_in6 *inet6;
> > + union {
> > + struct sockaddr *addr;
> > + struct sockaddr_in *inet4;
> > + struct sockaddr_in6 *inet6;
> > + } sa;
>
> Close, but not quite. The POSIX solution for this is the
> sockaddr_storage type, in <sys/socket.h>. You shouldn't need to create
> your own union, but instead reuse sockaddr_storage.
Casting from sockaddr to sockaddr_storage still produces an error with
clang. However, I was reading the related code and noticed a similar
union in the virSocketAddr type, so I decided to use that instead; that
also allowed to reduce size of the code a little.
Roman Bogorodskiy
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20141021/543059dc/attachment-0001.sig>
More information about the libvir-list
mailing list