[libvirt] [PATCH v2 34/42] interface: add default: case to all switch statements

Daniel P. Berrangé berrange at redhat.com
Thu Feb 15 16:43:39 UTC 2018


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 at redhat.com>
---
 src/interface/interface_backend_udev.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/interface/interface_backend_udev.c b/src/interface/interface_backend_udev.c
index 47e850bc2a..0153a46a16 100644
--- a/src/interface/interface_backend_udev.c
+++ b/src/interface/interface_backend_udev.c
@@ -64,9 +64,9 @@ virUdevStatusString(virUdevStatus status)
             return "inactive";
         case VIR_UDEV_IFACE_ALL:
             return "all";
+        default:
+            return "";
     }
-
-    return "";
 }
 
 /*
@@ -125,6 +125,7 @@ udevGetDevices(struct udev *udev, virUdevStatus status)
             break;
 
         case VIR_UDEV_IFACE_ALL:
+        default:
             break;
     }
 
@@ -1061,7 +1062,7 @@ udevGetIfaceDef(struct udev *udev, const char *name)
             ifacedef->type = VIR_INTERFACE_TYPE_BOND;
     }
 
-    switch (ifacedef->type) {
+    switch ((virInterfaceType)ifacedef->type) {
     case VIR_INTERFACE_TYPE_VLAN:
         if (udevGetIfaceDefVlan(udev, dev, name, ifacedef) < 0)
             goto error;
@@ -1076,6 +1077,11 @@ udevGetIfaceDef(struct udev *udev, const char *name)
         break;
     case VIR_INTERFACE_TYPE_ETHERNET:
         break;
+    case VIR_INTERFACE_TYPE_LAST:
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected interface type %d"), ifacedef->type);
+        goto error;
     }
 
     udev_device_unref(dev);
-- 
2.14.3




More information about the libvir-list mailing list