[ovs-dev] [PATCH v3 1/8] Fix strict aliasing violations with GCC 4.1 and 4.4.

Jarno Rajahalme jrajahalme at nicira.com
Thu Jul 31 22:21:47 UTC 2014


The typical use of struct sockaddr_storage is flagged as a strict
aliasing violation by GCC 4.4.7.  Using an explicit union lets the
compiler know that accessing the same location via different types is
not an error.

GCC 4.1.2 had a similar complaint about a cast of ukey's key_buf to
nlattr.  After this patch there are no further warnings with the
XenServer build, so we could start treating warnings as errors in the
builds.

Signed-off-by: Jarno Rajahalme <jrajahalme at nicira.com>
---
 ofproto/connmgr.c             |   12 ++++++++----
 ofproto/ofproto-dpif-sflow.c  |   12 +++++++-----
 ofproto/ofproto-dpif-upcall.c |    7 +++++--
 vswitchd/bridge.c             |   11 +++++++----
 4 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/ofproto/connmgr.c b/ofproto/connmgr.c
index 89af6b6..5e2c9c9 100644
--- a/ofproto/connmgr.c
+++ b/ofproto/connmgr.c
@@ -734,12 +734,16 @@ update_in_band_remotes(struct connmgr *mgr)
     /* Add all the remotes. */
     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
         const char *target = rconn_get_target(ofconn->rconn);
-        struct sockaddr_storage ss;
+        union {
+            struct sockaddr_storage ss;
+            struct sockaddr_in in;
+        } sa;
 
         if (ofconn->band == OFPROTO_IN_BAND
-            && stream_parse_target_with_default_port(target, OFP_OLD_PORT, &ss)
-            && ss.ss_family == AF_INET) {
-            addrs[n_addrs++] = *(struct sockaddr_in *) &ss;
+            && stream_parse_target_with_default_port(target, OFP_OLD_PORT,
+                                                     &sa.ss)
+            && sa.ss.ss_family == AF_INET) {
+            addrs[n_addrs++] = sa.in;
         }
     }
     for (i = 0; i < mgr->n_extra_remotes; i++) {
diff --git a/ofproto/ofproto-dpif-sflow.c b/ofproto/ofproto-dpif-sflow.c
index c7e092a..69dfc03 100644
--- a/ofproto/ofproto-dpif-sflow.c
+++ b/ofproto/ofproto-dpif-sflow.c
@@ -253,13 +253,15 @@ sflow_choose_agent_address(const char *agent_device,
     }
 
     SSET_FOR_EACH (target, targets) {
-        struct sockaddr_storage ss;
+        union {
+            struct sockaddr_storage ss;
+            struct sockaddr_in sin;
+        } sa;
         char name[IFNAMSIZ];
 
-        if (inet_parse_active(target, SFL_DEFAULT_COLLECTOR_PORT, &ss)
-            && ss.ss_family == AF_INET) {
-            struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
-            if (route_table_get_name(sin->sin_addr.s_addr, name)
+        if (inet_parse_active(target, SFL_DEFAULT_COLLECTOR_PORT, &sa.ss)
+            && sa.ss.ss_family == AF_INET) {
+            if (route_table_get_name(sa.sin.sin_addr.s_addr, name)
                 && !netdev_get_in4_by_name(name, &in4)) {
                 goto success;
             }
diff --git a/ofproto/ofproto-dpif-upcall.c b/ofproto/ofproto-dpif-upcall.c
index 654fbd3..72e31bb 100644
--- a/ofproto/ofproto-dpif-upcall.c
+++ b/ofproto/ofproto-dpif-upcall.c
@@ -193,7 +193,10 @@ struct udpif_key {
     struct xlate_cache *xcache OVS_GUARDED;   /* Cache for xlate entries that
                                                * are affected by this ukey.
                                                * Used for stats and learning.*/
-    struct odputil_keybuf key_buf;            /* Memory for 'key'. */
+    union {
+        struct odputil_keybuf key_buf;        /* Memory for 'key'. */
+        struct nlattr key_buf_nla;
+    };
 };
 
 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
@@ -1108,7 +1111,7 @@ ukey_create(const struct nlattr *key, size_t key_len, long long int used)
     struct udpif_key *ukey = xmalloc(sizeof *ukey);
 
     ovs_mutex_init(&ukey->mutex);
-    ukey->key = (struct nlattr *) &ukey->key_buf;
+    ukey->key = &ukey->key_buf_nla;
     memcpy(&ukey->key_buf, key, key_len);
     ukey->key_len = key_len;
 
diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c
index 6dcc2b8..d8ea4dd 100644
--- a/vswitchd/bridge.c
+++ b/vswitchd/bridge.c
@@ -495,12 +495,15 @@ collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
 
         managers = xmalloc(sset_count(&targets) * sizeof *managers);
         SSET_FOR_EACH (target, &targets) {
-            struct sockaddr_storage ss;
+            union {
+                struct sockaddr_storage ss;
+                struct sockaddr_in in;
+            } sa;
 
             if (stream_parse_target_with_default_port(target, OVSDB_OLD_PORT,
-                                                      &ss)
-                && ss.ss_family == AF_INET) {
-                managers[n_managers++] = *(struct sockaddr_in *) &ss;
+                                                      &sa.ss)
+                && sa.ss.ss_family == AF_INET) {
+                managers[n_managers++] = sa.in;
             }
         }
     }
-- 
1.7.10.4




More information about the dev mailing list