[libvirt] [PATCH v2 23/42] remote: add default: case to all switch statements

Daniel P. Berrangé berrange at redhat.com
Thu Feb 15 16:43:28 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/remote/remote_driver.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 9ea726dc45..bc4842fca6 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -1131,6 +1131,11 @@ doRemoteOpen(virConnectPtr conn,
         goto failed;
 
 #endif /* WIN32 */
+
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected remote transport type %d"), transport);
+        goto failed;
     } /* switch (transport) */
 
 
@@ -3742,7 +3747,7 @@ remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
 #if WITH_SASL
 static int remoteAuthCredVir2SASL(int vircred)
 {
-    switch (vircred) {
+    switch ((virConnectCredentialType)vircred) {
     case VIR_CRED_USERNAME:
         return SASL_CB_USER;
 
@@ -3766,9 +3771,12 @@ static int remoteAuthCredVir2SASL(int vircred)
 
     case VIR_CRED_REALM:
         return SASL_CB_GETREALM;
-    }
 
-    return 0;
+    case VIR_CRED_EXTERNAL:
+    case VIR_CRED_LAST:
+    default:
+        return 0;
+    }
 }
 
 static int remoteAuthCredSASL2Vir(int vircred)
@@ -3797,9 +3805,10 @@ static int remoteAuthCredSASL2Vir(int vircred)
 
     case SASL_CB_GETREALM:
         return VIR_CRED_REALM;
-    }
 
-    return 0;
+    default:
+        return 0;
+    }
 }
 
 /*
-- 
2.14.3




More information about the libvir-list mailing list