Blob Blame History Raw
From 6e8cb0e122f7c269d1252ed7d55a7c82ae0062eb Mon Sep 17 00:00:00 2001
Message-Id: <6e8cb0e122f7c269d1252ed7d55a7c82ae0062eb@dist-git>
From: Andrea Bolognani <abologna@redhat.com>
Date: Wed, 29 Nov 2017 16:23:03 +0100
Subject: [PATCH] conf: Improve virDomainChrTargetDefFormat()

Make the switch statement type-aware, avoid calling
virDomainChrTargetTypeToString() more than once and check its
return value before using it.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
(cherry picked from commit 46084f2aa1e9188709fcd7abc5c41fe165b2b19b)

https://bugzilla.redhat.com/show_bug.cgi?id=1449265
https://bugzilla.redhat.com/show_bug.cgi?id=1511421
https://bugzilla.redhat.com/show_bug.cgi?id=1512929
https://bugzilla.redhat.com/show_bug.cgi?id=1512934
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/conf/domain_conf.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 346edaa6bd..2489705d6b 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -23945,7 +23945,7 @@ virDomainChrTargetDefFormat(virBufferPtr buf,
     const char *targetType = virDomainChrTargetTypeToString(def->deviceType,
                                                             def->targetType);
 
-    switch (def->deviceType) {
+    switch ((virDomainChrDeviceType) def->deviceType) {
     case VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL: {
         if (!targetType) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -23992,28 +23992,43 @@ virDomainChrTargetDefFormat(virBufferPtr buf,
     }
 
     case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE:
+        if (!targetType) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("Could not format console target type"));
+            return -1;
+        }
+
         virBufferAsprintf(buf,
                           "<target type='%s' port='%d'/>\n",
-                          virDomainChrTargetTypeToString(def->deviceType,
-                                                         def->targetType),
-                          def->target.port);
+                          targetType, def->target.port);
         break;
 
     case VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL:
+        if (!targetType) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("Could not format serial target type"));
+            return -1;
+        }
+
         if (def->targetType != VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_NONE) {
             virBufferAsprintf(buf,
                               "<target type='%s' port='%d'/>\n",
-                              virDomainChrTargetTypeToString(def->deviceType,
-                                                             def->targetType),
+                              targetType,
                               def->target.port);
             break;
         }
         ATTRIBUTE_FALLTHROUGH;
 
-    default:
+    case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL:
         virBufferAsprintf(buf, "<target port='%d'/>\n",
                           def->target.port);
         break;
+
+    case VIR_DOMAIN_CHR_DEVICE_TYPE_LAST:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("unexpected char device type %d"),
+                       def->deviceType);
+        return -1;
     }
 
     return 0;
-- 
2.15.1