Re: [PATCH] Fix cast errors with clang

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



  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

Attachment: pgpRyngJ2WlHl.pgp
Description: PGP signature

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]