]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: use union for sockaddr structs to avoid aliasing
authorDaniel P. Berrange <berrange@redhat.com>
Wed, 31 Jan 2018 17:27:11 +0000 (17:27 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Wed, 31 Jan 2018 17:33:08 +0000 (17:33 +0000)
Some platforms/toolchains will complain about casting
sockaddr_storage to sockaddr_un because it breaks strict
aliasing rule

../../src/util/virutil.c: In function 'virGetUNIXSocketPath':
../../src/util/virutil.c:2005: error: dereferencing pointer 'un' does break strict-aliasing rules [-Wstrict-aliasing]

Change the code to use a union, in the same way that the
virsocketaddr.h header does.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
src/util/virutil.c

index c23fa8f92ea208248c6ad87971f18fb34bb975a8..cd6fbf2f306a36303754a3a8c40f4c6c33c67c13 100644 (file)
@@ -1985,30 +1985,33 @@ virGetListenFDs(void)
 #ifdef HAVE_SYS_UN_H
 char *virGetUNIXSocketPath(int fd)
 {
-    struct sockaddr_storage ss = { 0 };
-    struct sockaddr_un *un = (struct sockaddr_un *)&ss;
-    socklen_t len = sizeof(ss);
+    union {
+        struct sockaddr sa;
+        struct sockaddr_storage ss;
+        struct sockaddr_un un;
+    } addr = { .ss = { 0 } };
+    socklen_t len = sizeof(addr.ss);
     char *path;
 
-    if (getsockname(fd, (struct sockaddr *)&ss, &len) < 0) {
+    if (getsockname(fd, &addr.sa, &len) < 0) {
         virReportSystemError(errno, _("Unable to get address of FD %d"), fd);
         return NULL;
     }
 
-    if (ss.ss_family != AF_UNIX) {
+    if (addr.ss.ss_family != AF_UNIX) {
         virReportSystemError(EINVAL, _("FD %d is not a UNIX socket, has af=%d"),
-                             fd, ss.ss_family);
+                             fd, addr.ss.ss_family);
         return NULL;
     }
 
-    if (un->sun_path[0] == '\0')
-        un->sun_path[0] = '@';
+    if (addr.un.sun_path[0] == '\0')
+        addr.un.sun_path[0] = '@';
 
-    if (VIR_ALLOC_N(path, sizeof(un->sun_path) + 1) < 0)
+    if (VIR_ALLOC_N(path, sizeof(addr.un.sun_path) + 1) < 0)
         return NULL;
 
-    memcpy(path, un->sun_path, sizeof(un->sun_path));
-    path[sizeof(un->sun_path)] = '\0';
+    memcpy(path, addr.un.sun_path, sizeof(addr.un.sun_path));
+    path[sizeof(addr.un.sun_path)] = '\0';
     return path;
 }