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é <berra...@redhat.com>
---
 src/cpu/cpu.c       |  6 +++++-
 src/cpu/cpu_ppc64.c | 16 ++++++++++++----
 src/cpu/cpu_x86.c   | 13 +++++++++----
 3 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
index 047e3b1112..6191840634 100644
--- a/src/cpu/cpu.c
+++ b/src/cpu/cpu.c
@@ -428,11 +428,15 @@ virCPUGetHost(virArch arch,
         break;
 
     case VIR_CPU_TYPE_AUTO:
-    case VIR_CPU_TYPE_LAST:
         virReportError(VIR_ERR_INVALID_ARG,
                        _("unsupported CPU type: %s"),
                        virCPUTypeToString(type));
         goto error;
+    case VIR_CPU_TYPE_LAST:
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected CPU type %d"), type);
+        goto error;
     }
 
     if (nodeInfo) {
diff --git a/src/cpu/cpu_ppc64.c b/src/cpu/cpu_ppc64.c
index 76582d4083..5234bc927f 100644
--- a/src/cpu/cpu_ppc64.c
+++ b/src/cpu/cpu_ppc64.c
@@ -460,11 +460,13 @@ ppc64MapLoadCallback(cpuMapElement element,
     case CPU_MAP_ELEMENT_MODEL:
         return ppc64ModelsLoad(map, ctxt, nodes, n);
     case CPU_MAP_ELEMENT_FEATURE:
+        return 0;
     case CPU_MAP_ELEMENT_LAST:
-        break;
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected CPU map element %d"), element);
+        return -1;
     }
-
-    return 0;
 }
 
 static struct ppc64_map *
@@ -573,7 +575,7 @@ ppc64Compute(virCPUDefPtr host,
     if (cpu->type == VIR_CPU_TYPE_GUEST) {
         /* Guest CPU information */
         virCPUCompareResult tmp;
-        switch (cpu->mode) {
+        switch ((virCPUMode)cpu->mode) {
         case VIR_CPU_MODE_HOST_MODEL:
             /* host-model only:
              * we need to take compatibility modes into account */
@@ -595,6 +597,12 @@ ppc64Compute(virCPUDefPtr host,
              * look up guest CPU information */
             guest_model = ppc64ModelFromCPU(cpu, map);
             break;
+
+        case VIR_CPU_MODE_LAST:
+        default:
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Unexpected CPU mode %d"), cpu->mode);
+            goto cleanup;
         }
     } else {
         /* Other host CPU information */
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
index b2398c5ad2..1aaf83e109 100644
--- a/src/cpu/cpu_x86.c
+++ b/src/cpu/cpu_x86.c
@@ -1122,8 +1122,12 @@ x86ModelFromCPU(const virCPUDef *cpu,
 
             /* coverity[dead_error_condition] */
             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"), fpol);
+                goto error;
             }
         } else if (x86DataAdd(&model->data, &feature->data) < 0) {
             goto error;
@@ -1381,10 +1385,11 @@ x86MapLoadCallback(cpuMapElement element,
     case CPU_MAP_ELEMENT_MODEL:
         return x86ModelsLoad(map, ctxt, nodes, n);
     case CPU_MAP_ELEMENT_LAST:
-        break;
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected CPU map element %d"), element);
+        return -1;
     }
-
-    return 0;
 }
 
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to