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.
---
 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;
     bool ret = false;
 
     if (virSocketAddrParseInternal(&res, addr, AF_UNSPEC, false) < 0)
         return ret;
 
+    sa.addr = res->ai_addr;
+
     switch (res->ai_addr->sa_family) {
     case AF_INET:
-        inet4 = (struct sockaddr_in*) res->ai_addr;
-        ret = memcmp(&inet4->sin_addr.s_addr, &tmp.s_addr,
-                     sizeof(inet4->sin_addr.s_addr)) == 0;
+        ret = memcmp(&sa.inet4->sin_addr.s_addr, &tmp.s_addr,
+                     sizeof(sa.inet4->sin_addr.s_addr)) == 0;
         break;
     case AF_INET6:
-        inet6 = (struct sockaddr_in6*) res->ai_addr;
-        ret = IN6_IS_ADDR_LOOPBACK(&(inet6->sin6_addr));
+        ret = IN6_IS_ADDR_LOOPBACK(&(sa.inet6->sin6_addr));
         break;
     }
 
-- 
2.0.2

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

Reply via email to