[libvirt] [PATCH v2 36/42] nodedev: add default: case to all switch statements

Daniel P. Berrangé berrange at redhat.com
Thu Feb 15 16:43:41 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/node_device/node_device_udev.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index e87eb32a85..bcd943d272 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -1267,8 +1267,13 @@ udevGetDeviceDetails(struct udev_device *device,
     case VIR_NODE_DEV_CAP_SYSTEM:
     case VIR_NODE_DEV_CAP_FC_HOST:
     case VIR_NODE_DEV_CAP_VPORTS:
-    case VIR_NODE_DEV_CAP_LAST:
         break;
+    case VIR_NODE_DEV_CAP_LAST:
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected nodedve capability %d"),
+                       def->caps->data.type);
+        return -1;
     }
 
     return 0;
-- 
2.14.3




More information about the libvir-list mailing list