From e1128b3a4c58e0c56658e64f441f08cbee277321 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Wed, 13 Apr 2016 17:38:16 +0200 Subject: [PATCH] event-test: Use functions with typecasted switch to convert enums 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 3ab2b64a2c..68a571b8ed 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; } -- GitLab