[PATCH 05/18] event-test: Use functions with typecasted switch to convert enums

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Arrays would induce crash if a new value was introduced without adding
it here. This could happen for
VIR_DOMAIN_EVENT_DISK_DROP_MISSING_ON_START
---
 examples/object-events/event-test.c | 37 ++++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 9 deletions(-)

diff --git a/examples/object-events/event-test.c b/examples/object-events/event-test.c
index 3ab2b64..68a571b 100644
--- a/examples/object-events/event-test.c
+++ b/examples/object-events/event-test.c
@@ -437,11 +437,21 @@ myDomainEventControlErrorCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
     return 0;
 }

+static const char *
+diskChangeReasonToStr(int reason)
+{
+    switch ((virConnectDomainEventDiskChangeReason) reason) {
+    case VIR_DOMAIN_EVENT_DISK_CHANGE_MISSING_ON_START:
+        return "disk empty due to startupPolicy";
+
+    case VIR_DOMAIN_EVENT_DISK_DROP_MISSING_ON_START:
+        return "disk dropped due to startupPolicy";
+    }
+
+    return "unknown";
+}
+

-const char *diskChangeReasonStrings[] = {
-    "startupPolicy", /* 0 */
-    /* add new reason here */
-};
 static int
 myDomainEventDiskChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 virDomainPtr dom,
@@ -454,13 +464,22 @@ myDomainEventDiskChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
     printf("%s EVENT: Domain %s(%d) disk change oldSrcPath: %s newSrcPath: %s "
            "devAlias: %s reason: %s\n",
            __func__, virDomainGetName(dom), virDomainGetID(dom),
-           oldSrcPath, newSrcPath, devAlias, diskChangeReasonStrings[reason]);
+           oldSrcPath, newSrcPath, devAlias, diskChangeReasonToStr(reason));
     return 0;
 }

-const char *trayChangeReasonStrings[] = {
-    "open",
-    "close",
+static const char *
+trayChangeReasonToStr(int reason)
+{
+    switch ((virDomainEventTrayChangeReason) reason) {
+    case VIR_DOMAIN_EVENT_TRAY_CHANGE_OPEN:
+        return "open";
+
+    case VIR_DOMAIN_EVENT_TRAY_CHANGE_CLOSE:
+        return "close";
+    }
+
+    return "unknown";
 };


@@ -474,7 +493,7 @@ myDomainEventTrayChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
     printf("%s EVENT: Domain %s(%d) removable disk's tray change devAlias: %s "
            "reason: %s\n",
            __func__, virDomainGetName(dom), virDomainGetID(dom),
-           devAlias, trayChangeReasonStrings[reason]);
+           devAlias, trayChangeReasonToStr(reason));
     return 0;
 }

-- 
2.8.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]