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(a)redhat.com>
---
src/network/bridge_driver.c | 46 ++++++++++++++++++++++++++++++++++++++++-----
src/network/leaseshelper.c | 11 +++++++++--
2 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index dd6e3402ea..ce79a8ec7b 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -445,7 +445,7 @@ networkUpdateState(virNetworkObjPtr obj,
}
def = virNetworkObjGetDef(obj);
- switch (def->forward.type) {
+ switch ((virNetworkForwardType)def->forward.type) {
case VIR_NETWORK_FORWARD_NONE:
case VIR_NETWORK_FORWARD_NAT:
case VIR_NETWORK_FORWARD_ROUTE:
@@ -484,6 +484,13 @@ networkUpdateState(virNetworkObjPtr obj,
case VIR_NETWORK_FORWARD_HOSTDEV:
/* so far no extra checks */
break;
+
+ case VIR_NETWORK_FORWARD_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected network forward type %d"),
+ def->forward.type);
+ goto cleanup;
}
/* Try and read dnsmasq/radvd pids of active networks */
@@ -2731,9 +2738,14 @@ networkCreateInterfacePool(virNetworkDefPtr netdef)
case VIR_NETWORK_FORWARD_NAT:
case VIR_NETWORK_FORWARD_ROUTE:
case VIR_NETWORK_FORWARD_OPEN:
- case VIR_NETWORK_FORWARD_LAST:
/* by definition these will never be encountered here */
break;
+ case VIR_NETWORK_FORWARD_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected network forward type %d"),
+ netdef->forward.type);
+ goto cleanup;
}
}
@@ -2823,7 +2835,7 @@ networkStartNetwork(virNetworkDriverStatePtr driver,
VIR_HOOK_SUBOP_BEGIN) < 0)
goto cleanup;
- switch (def->forward.type) {
+ switch ((virNetworkForwardType)def->forward.type) {
case VIR_NETWORK_FORWARD_NONE:
case VIR_NETWORK_FORWARD_NAT:
@@ -2852,6 +2864,13 @@ networkStartNetwork(virNetworkDriverStatePtr driver,
if (networkStartNetworkExternal(obj) < 0)
goto cleanup;
break;
+
+ case VIR_NETWORK_FORWARD_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected network forward type %d"),
+ def->forward.type);
+ goto cleanup;
}
/* finally we can call the 'started' hook script if any */
@@ -2905,7 +2924,7 @@ networkShutdownNetwork(virNetworkDriverStatePtr driver,
unlink(stateFile);
VIR_FREE(stateFile);
- switch (def->forward.type) {
+ switch ((virNetworkForwardType)def->forward.type) {
case VIR_NETWORK_FORWARD_NONE:
case VIR_NETWORK_FORWARD_NAT:
@@ -2931,6 +2950,12 @@ networkShutdownNetwork(virNetworkDriverStatePtr driver,
case VIR_NETWORK_FORWARD_HOSTDEV:
ret = networkShutdownNetworkExternal(obj);
break;
+ case VIR_NETWORK_FORWARD_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected network forward type %d"),
+ def->forward.type);
+ ret = -1;
}
/* now that we know it's stopped call the hook if present */
@@ -3408,8 +3433,14 @@ networkValidate(virNetworkDriverStatePtr driver,
}
case VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NONE:
- case VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_LAST:
break;
+
+ case VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected network forward hostdev type %d"),
+ iface->type);
+ return -1;
}
}
if ((def->forward.npfs > 0) + usesInterface + usesAddress > 1) {
@@ -4616,6 +4647,11 @@ networkAllocateActualDevice(virDomainDefPtr dom,
case VIR_NETWORK_FORWARD_PASSTHROUGH:
iface->data.network.actual->data.direct.mode =
VIR_NETDEV_MACVLAN_MODE_PASSTHRU;
break;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected network forward type %d"),
+ netdef->forward.type);
+ goto error;
}
/* merge virtualports from interface, network, and portgroup to
diff --git a/src/network/leaseshelper.c b/src/network/leaseshelper.c
index b4d87ee651..95cd2ee910 100644
--- a/src/network/leaseshelper.c
+++ b/src/network/leaseshelper.c
@@ -202,8 +202,12 @@ main(int argc, char **argv)
break;
case VIR_LEASE_ACTION_INIT:
- case VIR_LEASE_ACTION_LAST:
break;
+ case VIR_LEASE_ACTION_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected lease action %d"), action);
+ goto cleanup;
}
if (!(leases_array_new = virJSONValueNewArray())) {
@@ -246,7 +250,10 @@ main(int argc, char **argv)
break;
case VIR_LEASE_ACTION_LAST:
- break;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected lease action %d"), action);
+ goto cleanup;
}
rv = EXIT_SUCCESS;
--
2.14.3