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/lxc/lxc_controller.c | 1 +
src/lxc/lxc_driver.c | 2 ++
src/lxc/lxc_process.c | 10 +++++++++-
3 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index f9f26570cd..3620998100 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -2583,6 +2583,7 @@ int main(int argc, char *argv[])
case 'h':
case '?':
+ default:
fprintf(stderr, "\n");
fprintf(stderr, "syntax: %s [OPTIONS]\n", argv[0]);
fprintf(stderr, "\n");
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 7d6568cdf8..07fd1f6ebe 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -1009,6 +1009,8 @@ lxcDomainGetMemoryParameters(virDomainPtr dom,
VIR_TYPED_PARAM_ULLONG, val) < 0)
goto cleanup;
break;
+ default:
+ break;
}
}
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index bc321e360d..82df80acfd 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -510,6 +510,10 @@ static int virLXCProcessSetupNamespaces(virConnectPtr conn,
if ((nsFDs[i] = virLXCProcessSetupNamespaceNet(i, lxcDef->ns_val[i])) <
0)
return -1;
break;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected namespace source %d"),
lxcDef->ns_source[i]);
+ return -1;
}
}
@@ -587,13 +591,17 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn,
case VIR_DOMAIN_NET_TYPE_MCAST:
case VIR_DOMAIN_NET_TYPE_UDP:
case VIR_DOMAIN_NET_TYPE_INTERNAL:
- case VIR_DOMAIN_NET_TYPE_LAST:
case VIR_DOMAIN_NET_TYPE_HOSTDEV:
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unsupported network type %s"),
virDomainNetTypeToString(type));
goto cleanup;
+ case VIR_DOMAIN_NET_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected net type %d"), type);
+ goto cleanup;
}
/* Set bandwidth or warn if requested and not supported. */
--
2.14.3