[libvirt] [PATCH v2 40/42] tools: add default: case to all switch statements

Daniel P. Berrangé berrange at redhat.com
Thu Feb 15 16:43:45 UTC 2018


Even if the compiler has validated that all enum constants have case
statements in a switch, it is not safe to omit a default: case
statement. When assigning a value to a variable / struct field that is
defined with an enum type, nothing prevents an invalid value being
assigned. So defensive code must assume existance of invalid values and
thus all switches should have a default: case.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 tools/virsh-domain-monitor.c |  4 ++++
 tools/virsh-domain.c         | 14 ++++++++++----
 tools/virsh-nodedev.c        |  1 +
 tools/virsh-pool.c           |  1 +
 tools/virsh.c                |  1 +
 tools/virt-admin.c           |  1 +
 tools/vsh.c                  |  5 +++++
 7 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 32a42707eb..4903acb235 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -258,6 +258,7 @@ virshDomainStateReasonToString(int state, int reason)
         str = virshDomainPMSuspendedReasonTypeToString(reason);
         break;
     case VIR_DOMAIN_LAST:
+    default:
         ;
     }
 
@@ -2232,6 +2233,9 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
             case VIR_IP_ADDR_TYPE_IPV6:
                 type = "ipv6";
                 break;
+            default:
+                type = "unknown";
+                break;
             }
 
             virBufferAsprintf(&buf, "%-12s %s/%d",
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 5a0e0c1b21..1ad4cf38eb 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -938,11 +938,13 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd)
     case VIR_DOMAIN_NET_TYPE_MCAST:
     case VIR_DOMAIN_NET_TYPE_UDP:
     case VIR_DOMAIN_NET_TYPE_INTERNAL:
-    case VIR_DOMAIN_NET_TYPE_LAST:
         vshError(ctl, _("No support for %s in command 'attach-interface'"),
                  type);
         goto cleanup;
-        break;
+    case VIR_DOMAIN_NET_TYPE_LAST:
+    default:
+        vshError(ctl, _("Unexpected net type %d"), typ);
+        goto cleanup;
     }
 
     if (target != NULL)
@@ -2048,17 +2050,16 @@ cmdBlockCommit(vshControl *ctl, const vshCmd *cmd)
     /* Execution continues here only if --wait or friends were specified */
     switch (virshBlockJobWait(bjWait)) {
         case -1:
+        default:
             goto cleanup;
 
         case VIR_DOMAIN_BLOCK_JOB_CANCELED:
             vshPrintExtra(ctl, "\n%s", _("Commit aborted"));
             goto cleanup;
-            break;
 
         case VIR_DOMAIN_BLOCK_JOB_FAILED:
             vshError(ctl, "\n%s", _("Commit failed"));
             goto cleanup;
-            break;
 
         case VIR_DOMAIN_BLOCK_JOB_READY:
         case VIR_DOMAIN_BLOCK_JOB_COMPLETED:
@@ -2366,6 +2367,7 @@ cmdBlockCopy(vshControl *ctl, const vshCmd *cmd)
     /* Execution continues here only if --wait or friends were specified */
     switch (virshBlockJobWait(bjWait)) {
         case -1:
+        default:
             goto cleanup;
 
         case VIR_DOMAIN_BLOCK_JOB_CANCELED:
@@ -2775,6 +2777,7 @@ cmdBlockPull(vshControl *ctl, const vshCmd *cmd)
     /* Execution continues here only if --wait or friends were specified */
     switch (virshBlockJobWait(bjWait)) {
         case -1:
+        default:
             goto cleanup;
 
         case VIR_DOMAIN_BLOCK_JOB_CANCELED:
@@ -10838,6 +10841,8 @@ virshMigrateTimeout(vshControl *ctl,
         if (virDomainMigrateStartPostCopy(dom, 0) < 0)
             vshDebug(ctl, VSH_ERR_INFO, "switching to post-copy failed\n");
         break;
+    default:
+        break;
     }
 }
 
@@ -12707,6 +12712,7 @@ virshDomainEventDetailToString(int event, int detail)
         str = virshDomainEventCrashedTypeToString(detail);
         break;
     case VIR_DOMAIN_EVENT_LAST:
+    default:
         break;
     }
     return str ? _(str) : _("unknown");
diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c
index d25fe0e09b..c355c8a013 100644
--- a/tools/virsh-nodedev.c
+++ b/tools/virsh-nodedev.c
@@ -465,6 +465,7 @@ cmdNodeListDevices(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
             flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_CCW_DEV;
             break;
         case VIR_NODE_DEV_CAP_LAST:
+        default:
             break;
         }
     }
diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c
index 56b6cfc73d..f827925349 100644
--- a/tools/virsh-pool.c
+++ b/tools/virsh-pool.c
@@ -1199,6 +1199,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
                 flags |= VIR_CONNECT_LIST_STORAGE_POOLS_VSTORAGE;
                 break;
             case VIR_STORAGE_POOL_LAST:
+            default:
                 break;
             }
         }
diff --git a/tools/virsh.c b/tools/virsh.c
index 5f8352e861..96cf902e25 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -120,6 +120,7 @@ virshCatchDisconnect(virConnectPtr conn,
         /* coverity[dead_error_condition] */
         case VIR_CONNECT_CLOSE_REASON_CLIENT:
         case VIR_CONNECT_CLOSE_REASON_LAST:
+        default:
             break;
         }
         vshError(ctl, _(str), NULLSTR(uri));
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index c86b5763a7..40dfd89046 100644
--- a/tools/virt-admin.c
+++ b/tools/virt-admin.c
@@ -142,6 +142,7 @@ vshAdmCatchDisconnect(virAdmConnectPtr conn ATTRIBUTE_UNUSED,
         /* coverity[dead_error_condition] */
     case VIR_CONNECT_CLOSE_REASON_CLIENT:
     case VIR_CONNECT_CLOSE_REASON_LAST:
+    default:
         break;
     }
 
diff --git a/tools/vsh.c b/tools/vsh.c
index 37c292a03d..304209e200 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -408,6 +408,9 @@ vshCmddefCheckInternals(const vshCmdDef *cmd)
 
         case VSH_OT_INT:
             break;
+
+        default:
+            return -1;
         }
     }
     return 0;
@@ -705,6 +708,7 @@ vshCmddefHelp(vshControl *ctl, const vshCmdDef *def)
                 }
                 break;
             case VSH_OT_ALIAS:
+            default:
                 /* aliases are intentionally undocumented */
                 continue;
             }
@@ -747,6 +751,7 @@ vshCmddefHelp(vshControl *ctl, const vshCmdDef *def)
                          opt->name);
                 break;
             case VSH_OT_ALIAS:
+            default:
                 continue;
             }
 
-- 
2.14.3




More information about the libvir-list mailing list