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/libvirt_private.syms | 2 ++
src/xen/xen_driver.c | 17 ++++++++++++++++-
src/xenconfig/xen_common.c | 6 +++++-
src/xenconfig/xen_sxpr.c | 47 +++++++++++++++++++++++++++++++++++-----------
src/xenconfig/xen_xl.c | 20 +++++++++++++++++---
5 files changed, 76 insertions(+), 16 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 3b14d7d158..87a9c85f09 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -402,6 +402,8 @@ virDomainInputBusTypeToString;
virDomainInputDefFind;
virDomainInputDefFree;
virDomainInputDefGetPath;
+virDomainInputTypeFromString;
+virDomainInputTypeToString;
virDomainIOMMUModelTypeFromString;
virDomainIOMMUModelTypeToString;
virDomainIOThreadIDAdd;
diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c
index f521fd1f2c..0dee96d21a 100644
--- a/src/xen/xen_driver.c
+++ b/src/xen/xen_driver.c
@@ -344,7 +344,7 @@ xenDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
}
if (dev->type == VIR_DOMAIN_DEVICE_VIDEO && dev->data.video->vram ==
0) {
- switch (dev->data.video->type) {
+ switch ((virDomainVideoType)dev->data.video->type) {
case VIR_DOMAIN_VIDEO_TYPE_VGA:
case VIR_DOMAIN_VIDEO_TYPE_CIRRUS:
case VIR_DOMAIN_VIDEO_TYPE_VMVGA:
@@ -359,6 +359,21 @@ xenDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
case VIR_DOMAIN_VIDEO_TYPE_QXL:
/* Use 64M as the minimal video video memory for qxl device */
return 64 * 1024;
+ case VIR_DOMAIN_VIDEO_TYPE_DEFAULT:
+ return 0;
+ case VIR_DOMAIN_VIDEO_TYPE_VBOX:
+ case VIR_DOMAIN_VIDEO_TYPE_PARALLELS:
+ case VIR_DOMAIN_VIDEO_TYPE_VIRTIO:
+ case VIR_DOMAIN_VIDEO_TYPE_GOP:
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("Unsupported video type %s"),
+ virDomainVideoTypeToString(dev->data.video->type));
+ return -1;
+ case VIR_DOMAIN_VIDEO_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected video type %d"),
dev->data.video->type);
+ return -1;
}
}
diff --git a/src/xenconfig/xen_common.c b/src/xenconfig/xen_common.c
index c3fe5d39dc..900c2dd5f0 100644
--- a/src/xenconfig/xen_common.c
+++ b/src/xenconfig/xen_common.c
@@ -1721,7 +1721,11 @@ xenFormatCPUFeatures(virConfPtr conf, virDomainDefPtr def)
return -1;
case VIR_DOMAIN_TIMER_NAME_LAST:
- break;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected timer name %d"),
+ def->clock.timers[i]->name);
+ return -1;
}
}
diff --git a/src/xenconfig/xen_sxpr.c b/src/xenconfig/xen_sxpr.c
index e868c05695..146947364c 100644
--- a/src/xenconfig/xen_sxpr.c
+++ b/src/xenconfig/xen_sxpr.c
@@ -212,7 +212,7 @@ xenParseSxprChar(const char *value,
}
}
- switch (def->source->type) {
+ switch ((virDomainChrType)def->source->type) {
case VIR_DOMAIN_CHR_TYPE_PTY:
if (VIR_STRDUP(def->source->data.file.path, tty) < 0)
goto error;
@@ -304,6 +304,25 @@ xenParseSxprChar(const char *value,
def->source->data.nix.listen = true;
}
break;
+
+ case VIR_DOMAIN_CHR_TYPE_NULL:
+ case VIR_DOMAIN_CHR_TYPE_STDIO:
+ case VIR_DOMAIN_CHR_TYPE_DEV:
+ break;
+ case VIR_DOMAIN_CHR_TYPE_VC:
+ case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
+ case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
+ case VIR_DOMAIN_CHR_TYPE_NMDM:
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("Unsupported chardev type %s"),
+ virDomainChrTypeToString(def->source->type));
+ goto error;
+
+ case VIR_DOMAIN_CHR_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected chardev type %d"),
def->source->type);
+ goto error;
}
return def;
@@ -1931,8 +1950,12 @@ xenFormatSxprNet(virConnectPtr conn,
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;
+ case VIR_DOMAIN_NET_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected net type %d"), def->type);
+ return -1;
}
if (def->ifname != NULL &&
@@ -2122,15 +2145,7 @@ xenFormatSxprInput(virDomainInputDefPtr input,
if (input->bus != VIR_DOMAIN_INPUT_BUS_USB)
return 0;
- if (input->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
- input->type != VIR_DOMAIN_INPUT_TYPE_TABLET &&
- input->type != VIR_DOMAIN_INPUT_TYPE_KBD) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("unexpected input type %d"), input->type);
- return -1;
- }
-
- switch (input->type) {
+ switch ((virDomainInputType)input->type) {
case VIR_DOMAIN_INPUT_TYPE_MOUSE:
virBufferAsprintf(buf, "(usbdevice %s)", "mouse");
break;
@@ -2140,6 +2155,16 @@ xenFormatSxprInput(virDomainInputDefPtr input,
case VIR_DOMAIN_INPUT_TYPE_KBD:
virBufferAsprintf(buf, "(usbdevice %s)", "keyboard");
break;
+ case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("Unsupported input type %s"),
+ virDomainInputTypeToString(input->type));
+ return -1;
+ case VIR_DOMAIN_INPUT_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected input type %d"), input->type);
+ return -1;
}
return 0;
diff --git a/src/xenconfig/xen_xl.c b/src/xenconfig/xen_xl.c
index e61784aff2..c098b0b4ee 100644
--- a/src/xenconfig/xen_xl.c
+++ b/src/xenconfig/xen_xl.c
@@ -1173,8 +1173,13 @@ xenFormatXLOS(virConfPtr conf, virDomainDefPtr def)
case VIR_CPU_FEATURE_FORCE:
case VIR_CPU_FEATURE_REQUIRE:
case VIR_CPU_FEATURE_OPTIONAL:
- case VIR_CPU_FEATURE_LAST:
break;
+ case VIR_CPU_FEATURE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected CPU feature policy
%d"),
+ def->cpu->features[i].policy);
+ return -1;
}
}
}
@@ -1357,7 +1362,6 @@ xenFormatXLDiskSrcNet(virStorageSourcePtr src)
case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
case VIR_STORAGE_NET_PROTOCOL_SSH:
case VIR_STORAGE_NET_PROTOCOL_VXHS:
- case VIR_STORAGE_NET_PROTOCOL_LAST:
case VIR_STORAGE_NET_PROTOCOL_NONE:
virReportError(VIR_ERR_NO_SUPPORT,
_("Unsupported network block protocol '%s'"),
@@ -1399,6 +1403,12 @@ xenFormatXLDiskSrcNet(virStorageSourcePtr src)
ret = virBufferContentAndReset(&buf);
break;
+ case VIR_STORAGE_NET_PROTOCOL_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected storage protocol %d"),
+ src->protocol);
+ goto cleanup;
}
cleanup:
@@ -1433,8 +1443,12 @@ xenFormatXLDiskSrc(virStorageSourcePtr src, char **srcstr)
case VIR_STORAGE_TYPE_VOLUME:
case VIR_STORAGE_TYPE_NONE:
- case VIR_STORAGE_TYPE_LAST:
break;
+ default:
+ case VIR_STORAGE_TYPE_LAST:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected storage type %d"),
+ actualType);
}
return 0;
--
2.14.3