c1c534
From 6e8cb0e122f7c269d1252ed7d55a7c82ae0062eb Mon Sep 17 00:00:00 2001
c1c534
Message-Id: <6e8cb0e122f7c269d1252ed7d55a7c82ae0062eb@dist-git>
c1c534
From: Andrea Bolognani <abologna@redhat.com>
c1c534
Date: Wed, 29 Nov 2017 16:23:03 +0100
c1c534
Subject: [PATCH] conf: Improve virDomainChrTargetDefFormat()
c1c534
c1c534
Make the switch statement type-aware, avoid calling
c1c534
virDomainChrTargetTypeToString() more than once and check its
c1c534
return value before using it.
c1c534
c1c534
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
c1c534
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
c1c534
(cherry picked from commit 46084f2aa1e9188709fcd7abc5c41fe165b2b19b)
c1c534
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1449265
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1511421
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1512929
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1512934
c1c534
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c1c534
---
c1c534
 src/conf/domain_conf.c | 29 ++++++++++++++++++++++-------
c1c534
 1 file changed, 22 insertions(+), 7 deletions(-)
c1c534
c1c534
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
c1c534
index 346edaa6bd..2489705d6b 100644
c1c534
--- a/src/conf/domain_conf.c
c1c534
+++ b/src/conf/domain_conf.c
c1c534
@@ -23945,7 +23945,7 @@ virDomainChrTargetDefFormat(virBufferPtr buf,
c1c534
     const char *targetType = virDomainChrTargetTypeToString(def->deviceType,
c1c534
                                                             def->targetType);
c1c534
 
c1c534
-    switch (def->deviceType) {
c1c534
+    switch ((virDomainChrDeviceType) def->deviceType) {
c1c534
     case VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL: {
c1c534
         if (!targetType) {
c1c534
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c1c534
@@ -23992,28 +23992,43 @@ virDomainChrTargetDefFormat(virBufferPtr buf,
c1c534
     }
c1c534
 
c1c534
     case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE:
c1c534
+        if (!targetType) {
c1c534
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c1c534
+                           _("Could not format console target type"));
c1c534
+            return -1;
c1c534
+        }
c1c534
+
c1c534
         virBufferAsprintf(buf,
c1c534
                           "<target type='%s' port='%d'/>\n",
c1c534
-                          virDomainChrTargetTypeToString(def->deviceType,
c1c534
-                                                         def->targetType),
c1c534
-                          def->target.port);
c1c534
+                          targetType, def->target.port);
c1c534
         break;
c1c534
 
c1c534
     case VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL:
c1c534
+        if (!targetType) {
c1c534
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c1c534
+                           _("Could not format serial target type"));
c1c534
+            return -1;
c1c534
+        }
c1c534
+
c1c534
         if (def->targetType != VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_NONE) {
c1c534
             virBufferAsprintf(buf,
c1c534
                               "<target type='%s' port='%d'/>\n",
c1c534
-                              virDomainChrTargetTypeToString(def->deviceType,
c1c534
-                                                             def->targetType),
c1c534
+                              targetType,
c1c534
                               def->target.port);
c1c534
             break;
c1c534
         }
c1c534
         ATTRIBUTE_FALLTHROUGH;
c1c534
 
c1c534
-    default:
c1c534
+    case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL:
c1c534
         virBufferAsprintf(buf, "<target port='%d'/>\n",
c1c534
                           def->target.port);
c1c534
         break;
c1c534
+
c1c534
+    case VIR_DOMAIN_CHR_DEVICE_TYPE_LAST:
c1c534
+        virReportError(VIR_ERR_INTERNAL_ERROR,
c1c534
+                       _("unexpected char device type %d"),
c1c534
+                       def->deviceType);
c1c534
+        return -1;
c1c534
     }
c1c534
 
c1c534
     return 0;
c1c534
-- 
c1c534
2.15.1
c1c534