ofproto-dpif: Avoid potential undefined behavior in type_run().
[sliver-openvswitch.git] / ofproto / ofproto-dpif.c
index ca0a065..95fd54e 100644 (file)
@@ -794,6 +794,20 @@ port_open_type(const char *datapath_type, const char *port_type)
 
 /* Type functions. */
 
+static struct ofproto_dpif *
+lookup_ofproto_dpif_by_port_name(const char *name)
+{
+    struct ofproto_dpif *ofproto;
+
+    HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
+        if (sset_contains(&ofproto->ports, name)) {
+            return ofproto;
+        }
+    }
+
+    return NULL;
+}
+
 static int
 type_run(const char *type)
 {
@@ -817,7 +831,7 @@ type_run(const char *type)
 
     /* Check for port changes in the dpif. */
     while ((error = dpif_port_poll(backer->dpif, &devname)) == 0) {
-        struct ofproto_dpif *ofproto = NULL;
+        struct ofproto_dpif *ofproto;
         struct dpif_port port;
 
         /* Don't report on the datapath's device. */
@@ -825,13 +839,7 @@ type_run(const char *type)
             continue;
         }
 
-        HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
-                       &all_ofproto_dpifs) {
-            if (sset_contains(&ofproto->ports, devname)) {
-                break;
-            }
-        }
-
+        ofproto = lookup_ofproto_dpif_by_port_name(devname);
         if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
             /* The port was removed.  If we know the datapath,
              * report it through poll_set().  If we don't, it may be