[libvirt] [PATCH] qemu: eliminate nested switch, simplify code

Laine Stump laine at laine.org
Wed Mar 28 19:33:53 UTC 2012


qemuBuildHostNetStr had a switch-within-a-switch where both were
looking at the same variable. This was apparently to take advantage of
code common to three different cases (while also taking care of some
code that was different). However, there were only 2 lines common to
all, one of those can be eliminated by merging it into the
virAsprintfs that are in each case. On top of that, all the extra
empty cases cause Coverity complaints (because they are unreachable),
but absence of the empty cases causes a compile error due to
"enumeration value not handled in switch".

The solution is to just make each toplevel case independent, folding
in the common code to each.
---
 src/qemu/qemu_command.c |   55 ++++++++++++++++++-----------------------------
 1 files changed, 21 insertions(+), 34 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 3d2bb6b..6b246d6 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -2721,8 +2721,7 @@ qemuBuildHostNetStr(virDomainNetDefPtr net,
     case VIR_DOMAIN_NET_TYPE_NETWORK:
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
     case VIR_DOMAIN_NET_TYPE_DIRECT:
-        virBufferAddLit(&buf, "tap");
-        virBufferAsprintf(&buf, "%cfd=%s", type_sep, tapfd);
+        virBufferAsprintf(&buf, "tap%cfd=%s", type_sep, tapfd);
         type_sep = ',';
         is_tap = true;
         break;
@@ -2742,40 +2741,28 @@ qemuBuildHostNetStr(virDomainNetDefPtr net,
         break;
 
     case VIR_DOMAIN_NET_TYPE_CLIENT:
+       virBufferAsprintf(&buf, "socket%cconnect=%s:%d",
+                         type_sep,
+                         net->data.socket.address,
+                         net->data.socket.port);
+       type_sep = ',';
+       break;
+
     case VIR_DOMAIN_NET_TYPE_SERVER:
+       virBufferAsprintf(&buf, "socket%clisten=%s:%d",
+                         type_sep,
+                         net->data.socket.address,
+                         net->data.socket.port);
+       type_sep = ',';
+       break;
+
     case VIR_DOMAIN_NET_TYPE_MCAST:
-        virBufferAddLit(&buf, "socket");
-        switch (netType) {
-        case VIR_DOMAIN_NET_TYPE_CLIENT:
-            virBufferAsprintf(&buf, "%cconnect=%s:%d",
-                              type_sep,
-                              net->data.socket.address,
-                              net->data.socket.port);
-            break;
-        case VIR_DOMAIN_NET_TYPE_SERVER:
-            virBufferAsprintf(&buf, "%clisten=%s:%d",
-                              type_sep,
-                              net->data.socket.address,
-                              net->data.socket.port);
-            break;
-        case VIR_DOMAIN_NET_TYPE_MCAST:
-            virBufferAsprintf(&buf, "%cmcast=%s:%d",
-                              type_sep,
-                              net->data.socket.address,
-                              net->data.socket.port);
-            break;
-        case VIR_DOMAIN_NET_TYPE_USER:
-        case VIR_DOMAIN_NET_TYPE_ETHERNET:
-        case VIR_DOMAIN_NET_TYPE_NETWORK:
-        case VIR_DOMAIN_NET_TYPE_BRIDGE:
-        case VIR_DOMAIN_NET_TYPE_INTERNAL:
-        case VIR_DOMAIN_NET_TYPE_DIRECT:
-        case VIR_DOMAIN_NET_TYPE_HOSTDEV:
-        case VIR_DOMAIN_NET_TYPE_LAST:
-            break;
-        }
-        type_sep = ',';
-        break;
+       virBufferAsprintf(&buf, "socket%cmcast=%s:%d",
+                         type_sep,
+                         net->data.socket.address,
+                         net->data.socket.port);
+       type_sep = ',';
+       break;
 
     case VIR_DOMAIN_NET_TYPE_USER:
     default:
-- 
1.7.7.6




More information about the libvir-list mailing list