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/security/security_apparmor.c | 12 +++++--
src/security/security_dac.c | 59 ++++++++++++++++++++++++++++------
src/security/security_selinux.c | 69 ++++++++++++++++++++++++++++++++--------
3 files changed, 115 insertions(+), 25 deletions(-)
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index a9899923ac..7943743494 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -919,7 +919,10 @@ AppArmorSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
}
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
- ret = 0;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected hostdev subsystem type %d"),
+ dev->source.subsys.type);
break;
}
@@ -990,9 +993,14 @@ AppArmorSetChardevLabel(virSecurityManagerPtr mgr,
case VIR_DOMAIN_CHR_TYPE_TCP:
case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
case VIR_DOMAIN_CHR_TYPE_NMDM:
- case VIR_DOMAIN_CHR_TYPE_LAST:
ret = 0;
break;
+ case VIR_DOMAIN_CHR_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected chardev type %d"),
+ dev_source->type);
+ break;
}
done:
diff --git a/src/security/security_dac.c b/src/security/security_dac.c
index 74446d6644..7142b802f9 100644
--- a/src/security/security_dac.c
+++ b/src/security/security_dac.c
@@ -1009,7 +1009,10 @@ virSecurityDACSetHostdevLabel(virSecurityManagerPtr mgr,
}
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
- ret = 0;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected hostdev subsys type %d"),
+ dev->source.subsys.type);
break;
}
@@ -1177,7 +1180,10 @@ virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
}
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
- ret = 0;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected hostdev subsys type %d"),
+ dev->source.subsys.type);
break;
}
@@ -1265,9 +1271,13 @@ virSecurityDACSetChardevLabel(virSecurityManagerPtr mgr,
case VIR_DOMAIN_CHR_TYPE_TCP:
case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
case VIR_DOMAIN_CHR_TYPE_NMDM:
- case VIR_DOMAIN_CHR_TYPE_LAST:
ret = 0;
break;
+ case VIR_DOMAIN_CHR_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected chardev type %d"), dev_source->type);
+ break;
}
done:
@@ -1328,9 +1338,13 @@ virSecurityDACRestoreChardevLabel(virSecurityManagerPtr mgr,
case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
case VIR_DOMAIN_CHR_TYPE_NMDM:
- case VIR_DOMAIN_CHR_TYPE_LAST:
ret = 0;
break;
+ case VIR_DOMAIN_CHR_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected chardev type %d"), dev_source->type);
+ break;
}
done:
@@ -1372,6 +1386,10 @@ virSecurityDACSetTPMFileLabel(virSecurityManagerPtr mgr,
false);
break;
case VIR_DOMAIN_TPM_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected TPM type %d"), tpm->type);
+ ret = -1;
break;
}
@@ -1393,6 +1411,10 @@ virSecurityDACRestoreTPMFileLabel(virSecurityManagerPtr mgr,
false);
break;
case VIR_DOMAIN_TPM_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected TPM type %d"), tpm->type);
+ ret = -1;
break;
}
@@ -1475,9 +1497,13 @@ virSecurityDACSetInputLabel(virSecurityManagerPtr mgr,
case VIR_DOMAIN_INPUT_TYPE_MOUSE:
case VIR_DOMAIN_INPUT_TYPE_TABLET:
case VIR_DOMAIN_INPUT_TYPE_KBD:
- case VIR_DOMAIN_INPUT_TYPE_LAST:
ret = 0;
break;
+ case VIR_DOMAIN_INPUT_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected input type %d"), input->type);
+ break;
}
return ret;
@@ -1499,9 +1525,13 @@ virSecurityDACRestoreInputLabel(virSecurityManagerPtr mgr,
case VIR_DOMAIN_INPUT_TYPE_MOUSE:
case VIR_DOMAIN_INPUT_TYPE_TABLET:
case VIR_DOMAIN_INPUT_TYPE_KBD:
- case VIR_DOMAIN_INPUT_TYPE_LAST:
ret = 0;
break;
+ case VIR_DOMAIN_INPUT_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected input type %d"), input->type);
+ break;
}
return ret;
@@ -1522,10 +1552,14 @@ virSecurityDACRestoreMemoryLabel(virSecurityManagerPtr mgr,
break;
case VIR_DOMAIN_MEMORY_MODEL_DIMM:
- case VIR_DOMAIN_MEMORY_MODEL_LAST:
case VIR_DOMAIN_MEMORY_MODEL_NONE:
ret = 0;
break;
+ case VIR_DOMAIN_MEMORY_MODEL_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected memory model %d"), mem->model);
+ break;
}
return ret;
@@ -1647,10 +1681,14 @@ virSecurityDACSetMemoryLabel(virSecurityManagerPtr mgr,
break;
case VIR_DOMAIN_MEMORY_MODEL_DIMM:
- case VIR_DOMAIN_MEMORY_MODEL_LAST:
case VIR_DOMAIN_MEMORY_MODEL_NONE:
ret = 0;
break;
+ case VIR_DOMAIN_MEMORY_MODEL_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected memory model %d"), mem->model);
+ break;
}
return ret;
@@ -1903,9 +1941,10 @@ virSecurityDACGenLabel(virSecurityManagerPtr mgr,
return 0;
case VIR_DOMAIN_SECLABEL_DEFAULT:
case VIR_DOMAIN_SECLABEL_LAST:
+ default:
virReportError(VIR_ERR_INTERNAL_ERROR,
- _("unexpected security label type '%s'"),
- virDomainSeclabelTypeToString(seclabel->type));
+ _("unexpected security label type %d"),
+ seclabel->type);
return rc;
}
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index c26cdacd9f..a33d07b6ab 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -763,7 +763,7 @@ virSecuritySELinuxGenLabel(virSecurityManagerPtr mgr,
VIR_DEBUG("type=%d", seclabel->type);
- switch (seclabel->type) {
+ switch ((virDomainSeclabelType)seclabel->type) {
case VIR_DOMAIN_SECLABEL_STATIC:
if (!(ctx = context_new(seclabel->label))) {
virReportSystemError(errno,
@@ -832,10 +832,12 @@ virSecuritySELinuxGenLabel(virSecurityManagerPtr mgr,
break;
+ case VIR_DOMAIN_SECLABEL_DEFAULT:
+ case VIR_DOMAIN_SECLABEL_LAST:
default:
virReportError(VIR_ERR_INTERNAL_ERROR,
- _("unexpected security label type '%s'"),
- virDomainSeclabelTypeToString(seclabel->type));
+ _("unexpected security label type '%d'"),
+ seclabel->type);
goto cleanup;
}
@@ -1346,8 +1348,12 @@ virSecuritySELinuxSetInputLabel(virSecurityManagerPtr mgr,
case VIR_DOMAIN_INPUT_TYPE_MOUSE:
case VIR_DOMAIN_INPUT_TYPE_TABLET:
case VIR_DOMAIN_INPUT_TYPE_KBD:
- case VIR_DOMAIN_INPUT_TYPE_LAST:
break;
+ case VIR_DOMAIN_INPUT_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected input type %d"), input->type);
+ return -1;
}
return 0;
@@ -1374,8 +1380,12 @@ virSecuritySELinuxRestoreInputLabel(virSecurityManagerPtr mgr,
case VIR_DOMAIN_INPUT_TYPE_MOUSE:
case VIR_DOMAIN_INPUT_TYPE_TABLET:
case VIR_DOMAIN_INPUT_TYPE_KBD:
- case VIR_DOMAIN_INPUT_TYPE_LAST:
break;
+ case VIR_DOMAIN_INPUT_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected input type %d"), input->type);
+ return -1;
}
return rc;
@@ -1402,8 +1412,12 @@ virSecuritySELinuxSetMemoryLabel(virSecurityManagerPtr mgr,
case VIR_DOMAIN_MEMORY_MODEL_NONE:
case VIR_DOMAIN_MEMORY_MODEL_DIMM:
- case VIR_DOMAIN_MEMORY_MODEL_LAST:
break;
+ case VIR_DOMAIN_MEMORY_MODEL_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected memory model %d"), mem->model);
+ return -1;
}
return 0;
@@ -1429,9 +1443,13 @@ virSecuritySELinuxRestoreMemoryLabel(virSecurityManagerPtr mgr,
case VIR_DOMAIN_MEMORY_MODEL_DIMM:
case VIR_DOMAIN_MEMORY_MODEL_NONE:
- case VIR_DOMAIN_MEMORY_MODEL_LAST:
ret = 0;
break;
+ case VIR_DOMAIN_MEMORY_MODEL_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected memory model %d"), mem->model);
+ return -1;
}
return ret;
@@ -1473,6 +1491,9 @@ virSecuritySELinuxSetTPMFileLabel(virSecurityManagerPtr mgr,
}
break;
case VIR_DOMAIN_TPM_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected TPM type %d"), tpm->type);
break;
}
@@ -1506,6 +1527,9 @@ virSecuritySELinuxRestoreTPMFileLabelInt(virSecurityManagerPtr mgr,
}
break;
case VIR_DOMAIN_TPM_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected TPM type %d"), tpm->type);
break;
}
@@ -1860,7 +1884,10 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManagerPtr mgr,
}
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
- ret = 0;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected hostdev subsystem type %d"),
+ dev->source.subsys.type);
break;
}
@@ -1883,7 +1910,7 @@ virSecuritySELinuxSetHostdevCapsLabel(virSecurityManagerPtr mgr,
if (secdef == NULL)
return 0;
- switch (dev->source.caps.type) {
+ switch ((virDomainHostdevCapsType)dev->source.caps.type) {
case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: {
if (vroot) {
if (virAsprintf(&path, "%s/%s", vroot,
@@ -1912,9 +1939,15 @@ virSecuritySELinuxSetHostdevCapsLabel(virSecurityManagerPtr mgr,
break;
}
- default:
+ case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET:
ret = 0;
break;
+ case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected hostdev caps type %d"),
+ dev->source.caps.type);
+ break;
}
return ret;
@@ -2101,7 +2134,10 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManagerPtr
mgr,
}
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
- ret = 0;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected hostdev subsystem type %d"),
+ dev->source.subsys.type);
break;
}
@@ -2118,7 +2154,7 @@ virSecuritySELinuxRestoreHostdevCapsLabel(virSecurityManagerPtr
mgr,
int ret = -1;
char *path;
- switch (dev->source.caps.type) {
+ switch ((virDomainHostdevCapsType)dev->source.caps.type) {
case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: {
if (vroot) {
if (virAsprintf(&path, "%s/%s", vroot,
@@ -2147,9 +2183,16 @@ virSecuritySELinuxRestoreHostdevCapsLabel(virSecurityManagerPtr
mgr,
break;
}
- default:
+ case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET:
ret = 0;
break;
+
+ case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected hostdev subsystem type %d"),
+ dev->source.subsys.type);
+ break;
}
return ret;
--
2.14.3