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/libxl/libxl_conf.c | 57 ++++++++++++++++++++++++++++++++++++++++--------
src/libxl/libxl_domain.c | 23 ++++++++++++++++++-
src/libxl/libxl_driver.c | 14 ++++++++++++
src/libxl/libxl_logger.c | 2 ++
4 files changed, 86 insertions(+), 10 deletions(-)
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c
index 970cff205b..e2136a8065 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -118,10 +118,9 @@ libxlActionFromVirLifecycle(virDomainLifecycleAction action)
return LIBXL_ACTION_ON_SHUTDOWN_COREDUMP_RESTART;
case VIR_DOMAIN_LIFECYCLE_ACTION_LAST:
- break;
+ default:
+ return 0;
}
-
- return 0;
}
@@ -147,6 +146,7 @@ libxlMakeDomCreateInfo(libxl_ctx *ctx,
case VIR_TRISTATE_SWITCH_ABSENT:
case VIR_TRISTATE_SWITCH_LAST:
+ default:
break;
}
} else {
@@ -333,6 +333,10 @@ libxlMakeDomBuildInfo(virDomainDefPtr def,
return -1;
case VIR_DOMAIN_TIMER_NAME_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected timer name %d"),
+ def->clock.timers[i]->name);
break;
}
}
@@ -379,8 +383,13 @@ libxlMakeDomBuildInfo(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;
}
}
}
@@ -716,7 +725,6 @@ libxlDiskSetDiscard(libxl_device_disk *x_disk, int discard)
#if defined(LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_ENABLE)
switch ((virDomainDiskDiscard)discard) {
case VIR_DOMAIN_DISK_DISCARD_DEFAULT:
- case VIR_DOMAIN_DISK_DISCARD_LAST:
break;
case VIR_DOMAIN_DISK_DISCARD_UNMAP:
libxl_defbool_set(&x_disk->discard_enable, true);
@@ -724,6 +732,11 @@ libxlDiskSetDiscard(libxl_device_disk *x_disk, int discard)
case VIR_DOMAIN_DISK_DISCARD_IGNORE:
libxl_defbool_set(&x_disk->discard_enable, false);
break;
+ case VIR_DOMAIN_DISK_DISCARD_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected disk discard mode %d"), discard);
+ return -1;
}
return 0;
#else
@@ -757,7 +770,6 @@ libxlMakeNetworkDiskSrcStr(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'"),
@@ -809,6 +821,11 @@ libxlMakeNetworkDiskSrcStr(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:
@@ -1062,6 +1079,7 @@ libxlUpdateDiskDef(virDomainDiskDefPtr l_disk, libxl_device_disk
*x_disk)
driver = "phy";
break;
case LIBXL_DISK_BACKEND_UNKNOWN:
+ default:
break;
}
if (driver)
@@ -1179,11 +1197,15 @@ libxlMakeNic(virDomainDefPtr def,
case VIR_DOMAIN_NET_TYPE_INTERNAL:
case VIR_DOMAIN_NET_TYPE_DIRECT:
case VIR_DOMAIN_NET_TYPE_HOSTDEV:
- case VIR_DOMAIN_NET_TYPE_LAST:
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unsupported interface type %s"),
virDomainNetTypeToString(l_nic->type));
goto cleanup;
+ case VIR_DOMAIN_NET_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected net type %d"), l_nic->type);
+ goto cleanup;
}
if (l_nic->domain_name) {
@@ -1340,8 +1362,16 @@ libxlMakeVfb(virPortAllocatorPtr graphicsports,
case VIR_DOMAIN_GRAPHICS_TYPE_RDP:
case VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP:
case VIR_DOMAIN_GRAPHICS_TYPE_SPICE:
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("Unsupported graphics type %s"),
+ virDomainGraphicsTypeToString(l_vfb->type));
+ return -1;
case VIR_DOMAIN_GRAPHICS_TYPE_LAST:
- break;
+ default:
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("Unexpected graphics type %d"),
+ l_vfb->type);
+ return -1;
}
return 0;
@@ -1465,7 +1495,11 @@ libxlMakeBuildInfoVfb(virPortAllocatorPtr graphicsports,
libxl_defbool_set(&b_info->u.hvm.spice.agent_mouse, false);
break;
case VIR_DOMAIN_GRAPHICS_SPICE_MOUSE_MODE_LAST:
- break;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected mouse mode %d"),
+ l_vfb->data.spice.mousemode);
+ return -1;
}
#ifdef LIBXL_HAVE_SPICE_VDAGENT
@@ -1754,6 +1788,11 @@ libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg,
case 'T':
multiplier = 1024 * 1024 * 1024;
break;
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected memory suffix
'%c'"), *p);
+ goto cleanup;
+
}
}
*maxmem = *maxmem * multiplier;
diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c
index feb092ba48..73941ca2d3 100644
--- a/src/libxl/libxl_domain.c
+++ b/src/libxl/libxl_domain.c
@@ -324,7 +324,7 @@ libxlDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
if (dev->type == VIR_DOMAIN_DEVICE_VIDEO && def->os.type ==
VIR_DOMAIN_OSTYPE_HVM) {
int dm_type = libxlDomainGetEmulatorType(def);
- switch (dev->data.video->type) {
+ switch ((virDomainVideoType)dev->data.video->type) {
case VIR_DOMAIN_VIDEO_TYPE_VGA:
case VIR_DOMAIN_VIDEO_TYPE_XEN:
if (dev->data.video->vram == 0) {
@@ -346,6 +346,24 @@ libxlDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
if (dev->data.video->vram == 0)
dev->data.video->vram = 128 * 1024;
break;
+
+ case VIR_DOMAIN_VIDEO_TYPE_DEFAULT:
+ break;
+ case VIR_DOMAIN_VIDEO_TYPE_VMVGA:
+ 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;
}
}
@@ -469,6 +487,7 @@ libxlDomainShutdownThread(void *opaque)
case VIR_DOMAIN_LIFECYCLE_ACTION_COREDUMP_DESTROY:
case VIR_DOMAIN_LIFECYCLE_ACTION_COREDUMP_RESTART:
case VIR_DOMAIN_LIFECYCLE_ACTION_LAST:
+ default:
goto endjob;
}
} else if (xl_reason == LIBXL_SHUTDOWN_REASON_CRASH) {
@@ -486,6 +505,7 @@ libxlDomainShutdownThread(void *opaque)
goto restart;
case VIR_DOMAIN_LIFECYCLE_ACTION_PRESERVE:
case VIR_DOMAIN_LIFECYCLE_ACTION_LAST:
+ default:
goto endjob;
case VIR_DOMAIN_LIFECYCLE_ACTION_COREDUMP_DESTROY:
libxlDomainAutoCoreDump(driver, vm);
@@ -511,6 +531,7 @@ libxlDomainShutdownThread(void *opaque)
case VIR_DOMAIN_LIFECYCLE_ACTION_COREDUMP_DESTROY:
case VIR_DOMAIN_LIFECYCLE_ACTION_COREDUMP_RESTART:
case VIR_DOMAIN_LIFECYCLE_ACTION_LAST:
+ default:
goto endjob;
}
} else {
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 4b52de36f5..68164d1b91 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -2266,6 +2266,9 @@ libxlDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
virDomainDefSetVcpus(def, nvcpus) < 0)
goto endjob;
break;
+
+ default:
+ break;
}
ret = 0;
@@ -3510,7 +3513,15 @@ libxlDomainAttachDeviceConfig(virDomainDefPtr vmdef,
virDomainDeviceDefPtr dev)
#ifndef LIBXL_HAVE_PVUSB
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
#endif
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("Unsupported hostdev subsystem type %s"),
+
virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type));
+ return -1;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("Unexpected hostdev subsystem type %d"),
+ hostdev->source.subsys.type);
return -1;
}
@@ -4876,6 +4887,9 @@ libxlDomainGetNumaParameters(virDomainPtr dom,
nodeset = NULL;
break;
+
+ default:
+ break;
}
}
diff --git a/src/libxl/libxl_logger.c b/src/libxl/libxl_logger.c
index 40404826fc..39f9c724b1 100644
--- a/src/libxl/libxl_logger.c
+++ b/src/libxl/libxl_logger.c
@@ -156,6 +156,8 @@ libxlLoggerNew(const char *logDir, virLogPriority minLevel)
case VIR_LOG_ERROR:
logger.minLevel = XTL_ERROR;
break;
+ default:
+ break;
}
logger.logDir = logDir;
--
2.14.3