[libvirt] [PATCH v2 31/42] uml: add default: case to all switch statements

Daniel P. Berrangé berrange at redhat.com
Thu Feb 15 16:43:36 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/uml/uml_conf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/uml/uml_conf.c b/src/uml/uml_conf.c
index 49589b33c9..e93aede797 100644
--- a/src/uml/uml_conf.c
+++ b/src/uml/uml_conf.c
@@ -259,7 +259,10 @@ umlBuildCommandLineNet(virConnectPtr conn,
         goto error;
 
     case VIR_DOMAIN_NET_TYPE_LAST:
-        break;
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected net type %d"), def->type);
+        goto error;
     }
 
     if (def->script) {
-- 
2.14.3




More information about the libvir-list mailing list