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/vbox/vbox_common.c | 29 +++++++++++++++++++++++++----
1 file changed, 25 insertions(+), 4 deletions(-)
diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c
index 07f4308784..b7b8ddb2be 100644
--- a/src/vbox/vbox_common.c
+++ b/src/vbox/vbox_common.c
@@ -331,7 +331,7 @@ vboxGenerateMediumName(PRUint32 storageBus,
break;
case StorageBus_Null:
-
+ default:
return NULL;
}
@@ -380,11 +380,16 @@ vboxSetStorageController(virDomainControllerDefPtr controller,
case VIR_DOMAIN_CONTROLLER_TYPE_CCID:
case VIR_DOMAIN_CONTROLLER_TYPE_USB:
case VIR_DOMAIN_CONTROLLER_TYPE_PCI:
- case VIR_DOMAIN_CONTROLLER_TYPE_LAST:
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("The vbox driver does not support %s controller
type"),
virDomainControllerTypeToString(controller->type));
return -1;
+ case VIR_DOMAIN_CONTROLLER_TYPE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected controller type %d"),
+ controller->type);
+ return -1;
}
/* libvirt scsi model => vbox scsi model */
@@ -417,6 +422,7 @@ vboxSetStorageController(virDomainControllerDefPtr controller,
goto cleanup;
case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_DEFAULT:
case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LAST:
+ default:
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unexpected SCSI controller model %d"),
controller->model);
@@ -439,6 +445,7 @@ vboxSetStorageController(virDomainControllerDefPtr controller,
break;
case VIR_DOMAIN_CONTROLLER_MODEL_IDE_LAST:
case VIR_DOMAIN_CONTROLLER_MODEL_IDE_DEFAULT:
+ default:
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Unexpected IDE controller model %d"),
controller->model);
@@ -1110,12 +1117,17 @@ vboxAttachDrives(virDomainDefPtr def, vboxDriverPtr data, IMachine
*machine)
break;
case VIR_DOMAIN_DISK_DEVICE_LUN:
- case VIR_DOMAIN_DISK_DEVICE_LAST:
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("The vbox driver does not support %s disk
device"),
virDomainDiskDeviceTypeToString(disk->device));
ret = -1;
goto cleanup;
+ case VIR_DOMAIN_DISK_DEVICE_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected disk device type %d"),
disk->device);
+ ret = -1;
+ goto cleanup;
}
switch ((virDomainDiskBus) disk->bus) {
@@ -1150,12 +1162,17 @@ vboxAttachDrives(virDomainDefPtr def, vboxDriverPtr data, IMachine
*machine)
case VIR_DOMAIN_DISK_BUS_USB:
case VIR_DOMAIN_DISK_BUS_UML:
case VIR_DOMAIN_DISK_BUS_SD:
- case VIR_DOMAIN_DISK_BUS_LAST:
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("The vbox driver does not support %s bus type"),
virDomainDiskBusTypeToString(disk->bus));
ret = -1;
goto cleanup;
+ case VIR_DOMAIN_DISK_BUS_LAST:
+ default:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unexpected disk device type %d"),
disk->device);
+ ret = -1;
+ goto cleanup;
}
/* If disk source is specified, lookup IMedium - removable drives don't
@@ -3158,6 +3175,7 @@ vboxDumpStorageControllers(virDomainDefPtr def, IMachine *machine)
case StorageControllerType_IntelAhci:
case StorageControllerType_I82078:
case StorageControllerType_Null:
+ default:
model = -1;
break;
@@ -3183,6 +3201,7 @@ vboxDumpStorageControllers(virDomainDefPtr def, IMachine *machine)
break;
case StorageBus_Null:
+ default:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unsupported null storage bus"));
@@ -3415,6 +3434,7 @@ vboxDumpDisks(virDomainDefPtr def, vboxDriverPtr data, IMachine
*machine)
break;
case StorageBus_Null:
+ default:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unsupported null storage bus"));
goto cleanup;
@@ -3437,6 +3457,7 @@ vboxDumpDisks(virDomainDefPtr def, vboxDriverPtr data, IMachine
*machine)
case DeviceType_USB:
case DeviceType_SharedFolder:
case DeviceType_Null:
+ default:
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unsupported vbox device type: %d"), deviceType);
goto cleanup;
--
2.14.3