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>
---
tests/cputest.c | 31 +++++++++++++++++++------------
tests/domaincapstest.c | 1 +
tests/qemuhotplugtest.c | 4 ++++
tests/storagevolxml2argvtest.c | 2 ++
tests/virusbtest.c | 6 ++++++
5 files changed, 32 insertions(+), 12 deletions(-)
diff --git a/tests/cputest.c b/tests/cputest.c
index 1e79edbef7..f29a6967dd 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -178,14 +178,18 @@ static const char *
cpuTestCompResStr(virCPUCompareResult result)
{
switch (result) {
- case VIR_CPU_COMPARE_ERROR: return "ERROR";
- case VIR_CPU_COMPARE_INCOMPATIBLE: return "INCOMPATIBLE";
- case VIR_CPU_COMPARE_IDENTICAL: return "IDENTICAL";
- case VIR_CPU_COMPARE_SUPERSET: return "SUPERSET";
- case VIR_CPU_COMPARE_LAST: break;
+ case VIR_CPU_COMPARE_ERROR:
+ return "ERROR";
+ case VIR_CPU_COMPARE_INCOMPATIBLE:
+ return "INCOMPATIBLE";
+ case VIR_CPU_COMPARE_IDENTICAL:
+ return "IDENTICAL";
+ case VIR_CPU_COMPARE_SUPERSET:
+ return "SUPERSET";
+ case VIR_CPU_COMPARE_LAST:
+ default:
+ return "unknown";
}
-
- return "unknown";
}
@@ -193,12 +197,15 @@ static const char *
cpuTestBoolWithErrorStr(enum cpuTestBoolWithError result)
{
switch (result) {
- case FAIL: return "FAIL";
- case NO: return "NO";
- case YES: return "YES";
+ case FAIL:
+ return "FAIL";
+ case NO:
+ return "NO";
+ case YES:
+ return "YES";
+ default:
+ return "unknown";
}
-
- return "unknown";
}
diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c
index 533a4b3791..1d9fe0fd0f 100644
--- a/tests/domaincapstest.c
+++ b/tests/domaincapstest.c
@@ -301,6 +301,7 @@ test_virDomainCapsFormat(const void *opaque)
switch (data->capsType) {
case CAPS_NONE:
+ default:
break;
case CAPS_ALL:
diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c
index 63bfe44145..47de86792a 100644
--- a/tests/qemuhotplugtest.c
+++ b/tests/qemuhotplugtest.c
@@ -329,6 +329,10 @@ testQemuHotplug(const void *data)
case UPDATE:
ret = testQemuHotplugUpdate(vm, dev);
+ break;
+
+ default:
+ ret = -1;
}
cleanup:
diff --git a/tests/storagevolxml2argvtest.c b/tests/storagevolxml2argvtest.c
index 5857d5e350..f8330cb4d2 100644
--- a/tests/storagevolxml2argvtest.c
+++ b/tests/storagevolxml2argvtest.c
@@ -29,6 +29,8 @@ testSetVolumeType(virStorageVolDefPtr vol,
case VIR_STORAGE_POOL_LOGICAL:
vol->type = VIR_STORAGE_VOL_BLOCK;
return;
+ default:
+ return;
}
}
diff --git a/tests/virusbtest.c b/tests/virusbtest.c
index 8728fe9092..a5bbbdbf32 100644
--- a/tests/virusbtest.c
+++ b/tests/virusbtest.c
@@ -93,6 +93,9 @@ static int testDeviceFind(const void *opaque)
rv = virUSBDeviceFindByBus(info->bus, info->devno,
info->vroot, info->mandatory, &dev);
break;
+ default:
+ rv = -1;
+ break;
}
if (info->expectFailure) {
@@ -123,6 +126,9 @@ static int testDeviceFind(const void *opaque)
goto cleanup;
}
break;
+ default:
+ ret = -1;
+ goto cleanup;
}
ret = 0;
--
2.14.3