[libvirt] [PATCH v2 21/42] locking: add default: case to all switch statements

Daniel P. Berrangé berrange at redhat.com
Thu Feb 15 16:43:26 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/locking/lock_driver_sanlock.c | 6 +++++-
 src/locking/lock_manager.c        | 2 ++
 src/locking/sanlock_helper.c      | 5 ++++-
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/locking/lock_driver_sanlock.c b/src/locking/lock_driver_sanlock.c
index 345cf0a772..4a9971ac9f 100644
--- a/src/locking/lock_driver_sanlock.c
+++ b/src/locking/lock_driver_sanlock.c
@@ -859,11 +859,15 @@ virLockManagerSanlockRegisterKillscript(int sock,
 
     case VIR_DOMAIN_LOCK_FAILURE_RESTART:
     case VIR_DOMAIN_LOCK_FAILURE_IGNORE:
-    case VIR_DOMAIN_LOCK_FAILURE_LAST:
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("Failure action %s is not supported by sanlock"),
                        virDomainLockFailureTypeToString(action));
         goto cleanup;
+    case VIR_DOMAIN_LOCK_FAILURE_LAST:
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected lock failure action %d"), action);
+        goto cleanup;
     }
 
     virBufferEscape(&buf, '\\', "\\ ", "%s", vmuri);
diff --git a/src/locking/lock_manager.c b/src/locking/lock_manager.c
index 4ef9f9e692..03e28f1159 100644
--- a/src/locking/lock_manager.c
+++ b/src/locking/lock_manager.c
@@ -97,6 +97,8 @@ static void virLockManagerLogParams(size_t nparams,
             virUUIDFormat(params[i].value.uuid, uuidstr);
             VIR_DEBUG("  key=%s type=uuid value=%s", params[i].key, uuidstr);
             break;
+        default:
+            break;
         }
     }
 }
diff --git a/src/locking/sanlock_helper.c b/src/locking/sanlock_helper.c
index 57e1cfb031..6a2f59e866 100644
--- a/src/locking/sanlock_helper.c
+++ b/src/locking/sanlock_helper.c
@@ -94,10 +94,13 @@ main(int argc, char **argv)
     case VIR_DOMAIN_LOCK_FAILURE_DEFAULT:
     case VIR_DOMAIN_LOCK_FAILURE_RESTART:
     case VIR_DOMAIN_LOCK_FAILURE_IGNORE:
-    case VIR_DOMAIN_LOCK_FAILURE_LAST:
         fprintf(stderr, _("unsupported failure action: '%s'\n"),
                 virDomainLockFailureTypeToString(action));
         break;
+    case VIR_DOMAIN_LOCK_FAILURE_LAST:
+    default:
+        fprintf(stderr, _("Unexpected failure action: %d\n"), action);
+        break;
     }
 
  cleanup:
-- 
2.14.3




More information about the libvir-list mailing list