]> xenbits.xensource.com Git - people/dariof/libvirt.git/commitdiff
util: fix bug found by Coverity
authorLaine Stump <laine@laine.org>
Tue, 25 Jun 2013 15:42:13 +0000 (11:42 -0400)
committerLaine Stump <laine@laine.org>
Tue, 25 Jun 2013 18:57:24 +0000 (14:57 -0400)
Commit 861d40565 added code (my personal change to "clean up" the
submitter's code, *not* the fault of the submitter) that dereferenced
virtVlan without first checking for NULL. This patch fixes that and,
as part of the fix, cleans up some unnecessary obtuseness.

src/util/virnetdevopenvswitch.c

index 5834e4e413404df47ca125ee5a75b51027e807e9..75b196c9fce8310d05fe40db66d974fe5ee99b92 100644 (file)
@@ -81,9 +81,27 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
             goto out_of_memory;
     }
 
+    cmd = virCommandNew(OVSVSCTL);
+
+    virCommandAddArgList(cmd, "--timeout=5", "--", "--may-exist", "add-port",
+                        brname, ifname, NULL);
+
     if (virtVlan && virtVlan->nTags > 0) {
 
-        /* Trunk port first */
+        switch (virtVlan->nativeMode) {
+        case VIR_NATIVE_VLAN_MODE_TAGGED:
+            virCommandAddArg(cmd, "vlan_mode=native-tagged");
+            virCommandAddArgFormat(cmd, "tag=%d", virtVlan->nativeTag);
+            break;
+        case VIR_NATIVE_VLAN_MODE_UNTAGGED:
+            virCommandAddArg(cmd, "vlan_mode=native-untagged");
+            virCommandAddArgFormat(cmd, "tag=%d", virtVlan->nativeTag);
+            break;
+        case VIR_NATIVE_VLAN_MODE_DEFAULT:
+        default:
+            break;
+        }
+
         if (virtVlan->trunk) {
             virBufferAddLit(&buf, "trunk=");
 
@@ -99,33 +117,15 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
                 virBufferAddLit(&buf, ",");
                 virBufferAsprintf(&buf, "%d", virtVlan->tag[i]);
             }
+
+            if (virBufferError(&buf))
+                goto out_of_memory;
+            virCommandAddArg(cmd, virBufferCurrentContent(&buf));
         } else if (virtVlan->nTags) {
-            virBufferAsprintf(&buf, "tag=%d", virtVlan->tag[0]);
+            virCommandAddArgFormat(cmd, "tag=%d", virtVlan->tag[0]);
         }
     }
 
-    cmd = virCommandNew(OVSVSCTL);
-
-    virCommandAddArgList(cmd, "--timeout=5", "--", "--may-exist", "add-port",
-                        brname, ifname, NULL);
-
-    switch (virtVlan->nativeMode) {
-    case VIR_NATIVE_VLAN_MODE_TAGGED:
-        virCommandAddArg(cmd, "vlan_mode=native-tagged");
-        virCommandAddArgFormat(cmd, "tag=%d", virtVlan->nativeTag);
-        break;
-    case VIR_NATIVE_VLAN_MODE_UNTAGGED:
-        virCommandAddArg(cmd, "vlan_mode=native-untagged");
-        virCommandAddArgFormat(cmd, "tag=%d", virtVlan->nativeTag);
-        break;
-    case VIR_NATIVE_VLAN_MODE_DEFAULT:
-    default:
-        break;
-    }
-
-    if (virBufferUse(&buf) != 0)
-        virCommandAddArgList(cmd, virBufferCurrentContent(&buf), NULL);
-
     if (ovsport->profileID[0] == '\0') {
         virCommandAddArgList(cmd,
                         "--", "set", "Interface", ifname, attachedmac_ex_id,