Blob Blame History Raw
From 3e5618223a9e21597ed9e95edfc79139563be301 Mon Sep 17 00:00:00 2001
Message-Id: <3e5618223a9e21597ed9e95edfc79139563be301@dist-git>
From: Andrea Bolognani <abologna@redhat.com>
Date: Mon, 21 Aug 2017 14:46:38 +0200
Subject: [PATCH] conf: Move target index validation

Validation should happen after parsing, so the proper
location for it is virDomainControllerDefValidate()
rather than virDomainControllerDefParseXML().

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
(cherry picked from commit c9d75d655ae73693a08123aca75677caf579f9e9)

Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1479647

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/conf/domain_conf.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 7ba2bc01ca..7889f5335c 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -5046,12 +5046,34 @@ static int
 virDomainControllerDefValidate(const virDomainControllerDef *controller)
 {
     if (controller->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
+        const virDomainPCIControllerOpts *opts = &controller->opts.pciopts;
+
         if (controller->idx > 255) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("PCI controller index %d too high, maximum is 255"),
                            controller->idx);
             return -1;
         }
+
+        /* Only validate the target index if it's been set */
+        if (opts->targetIndex != -1) {
+
+            if (opts->targetIndex < 0 || opts->targetIndex > 31) {
+                virReportError(VIR_ERR_XML_ERROR,
+                               _("PCI controller target index '%d' out of "
+                                 "range - must be 0-31"),
+                               opts->targetIndex);
+                return -1;
+            }
+
+            if ((controller->idx == 0 && opts->targetIndex != 0) ||
+                (controller->idx != 0 && opts->targetIndex == 0)) {
+                virReportError(VIR_ERR_XML_ERROR, "%s",
+                               _("Only the PCI controller with index 0 can "
+                                 "have target index 0, and vice versa"));
+                return -1;
+            }
+        }
     }
     return 0;
 }
@@ -9292,27 +9314,13 @@ virDomainControllerDefParseXML(xmlNodePtr node,
         }
         if (targetIndex) {
             if (virStrToLong_i(targetIndex, NULL, 0,
-                               &def->opts.pciopts.targetIndex) < 0) {
+                               &def->opts.pciopts.targetIndex) < 0 ||
+                def->opts.pciopts.targetIndex == -1) {
                 virReportError(VIR_ERR_XML_ERROR,
                                _("Invalid target index '%s' in PCI controller"),
                                targetIndex);
                 goto error;
             }
-            if (def->opts.pciopts.targetIndex < 0 ||
-                def->opts.pciopts.targetIndex > 31) {
-                virReportError(VIR_ERR_XML_ERROR,
-                               _("PCI controller target index '%s' out of "
-                                 "range - must be 0-31"),
-                               targetIndex);
-                goto error;
-            }
-            if ((def->idx == 0 && def->opts.pciopts.targetIndex != 0) ||
-                (def->idx != 0 && def->opts.pciopts.targetIndex == 0)) {
-                virReportError(VIR_ERR_XML_ERROR, "%s",
-                               _("Only the PCI controller with index 0 can "
-                                 "have target index 0, and vice versa"));
-                goto error;
-            }
         }
         if (numaNode >= 0) {
             if (def->idx == 0) {
-- 
2.14.1