6ae9ed
From a61eeb7afe4694defd6a6bd46f917802fd88703c Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <a61eeb7afe4694defd6a6bd46f917802fd88703c@dist-git>
6ae9ed
From: =?UTF-8?q?J=C3=A1n=20Tomko?= <jtomko@redhat.com>
6ae9ed
Date: Tue, 16 Aug 2016 12:49:45 +0200
6ae9ed
Subject: [PATCH] conf: report an error message for non-existing USB hubs
6ae9ed
MIME-Version: 1.0
6ae9ed
Content-Type: text/plain; charset=UTF-8
6ae9ed
Content-Transfer-Encoding: 8bit
6ae9ed
6ae9ed
If any of the devices referenced a USB hub that does not exist,
6ae9ed
defining the domain would either fail with:
6ae9ed
error: An error occurred, but the cause is unknown
6ae9ed
(if only the last hub in the path is missing)
6ae9ed
or crash.
6ae9ed
6ae9ed
Return a proper error instead of crashing.
6ae9ed
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1367130
6ae9ed
(cherry picked from commit ef66bd5df8af93db5a77f0c1f33182139c9b63b3)
6ae9ed
Signed-off-by: Ján Tomko <jtomko@redhat.com>
6ae9ed
---
6ae9ed
 src/conf/domain_addr.c                                |  8 ++++++++
6ae9ed
 .../qemuxml2argv-usb-hub-nonexistent.xml              | 19 +++++++++++++++++++
6ae9ed
 tests/qemuxml2argvtest.c                              |  3 +++
6ae9ed
 3 files changed, 30 insertions(+)
6ae9ed
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-usb-hub-nonexistent.xml
6ae9ed
6ae9ed
diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c
6ae9ed
index 14baef7..6567093 100644
6ae9ed
--- a/src/conf/domain_addr.c
6ae9ed
+++ b/src/conf/domain_addr.c
6ae9ed
@@ -1501,6 +1501,14 @@ virDomainUSBAddressFindPort(virDomainUSBAddressSetPtr addrs,
6ae9ed
             return NULL;
6ae9ed
         }
6ae9ed
         hub = hub->ports[portIdx];
6ae9ed
+        if (!hub) {
6ae9ed
+            virReportError(VIR_ERR_XML_ERROR,
6ae9ed
+                           _("there is no hub at port %u in USB address bus: %u port: %s"),
6ae9ed
+                           info->addr.usb.port[i],
6ae9ed
+                           info->addr.usb.bus,
6ae9ed
+                           portStr);
6ae9ed
+            return NULL;
6ae9ed
+        }
6ae9ed
     }
6ae9ed
 
6ae9ed
     *targetIdx = info->addr.usb.port[lastIdx] - 1;
6ae9ed
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-usb-hub-nonexistent.xml b/tests/qemuxml2argvdata/qemuxml2argv-usb-hub-nonexistent.xml
6ae9ed
new file mode 100644
6ae9ed
index 0000000..2090319
6ae9ed
--- /dev/null
6ae9ed
+++ b/tests/qemuxml2argvdata/qemuxml2argv-usb-hub-nonexistent.xml
6ae9ed
@@ -0,0 +1,19 @@
6ae9ed
+<domain type='qemu'>
6ae9ed
+  <name>QEMUGuest1</name>
6ae9ed
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
6ae9ed
+  <memory unit='KiB'>219136</memory>
6ae9ed
+  <currentMemory unit='KiB'>219136</currentMemory>
6ae9ed
+  <vcpu placement='static'>1</vcpu>
6ae9ed
+  <os>
6ae9ed
+    <type arch='i686' machine='pc'>hvm</type>
6ae9ed
+    <boot dev='hd'/>
6ae9ed
+  </os>
6ae9ed
+  <devices>
6ae9ed
+    <emulator>/usr/bin/qemu</emulator>
6ae9ed
+    <controller type='usb' index='0'/>
6ae9ed
+    <memballoon model='virtio'/>
6ae9ed
+    <input type='mouse' bus='usb'>
6ae9ed
+      <address type='usb' bus='0' port='1.2.3'/>
6ae9ed
+    </input>
6ae9ed
+  </devices>
6ae9ed
+</domain>
6ae9ed
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
6ae9ed
index b86a8a1..b1ff684 100644
6ae9ed
--- a/tests/qemuxml2argvtest.c
6ae9ed
+++ b/tests/qemuxml2argvtest.c
6ae9ed
@@ -1202,6 +1202,9 @@ mymain(void)
6ae9ed
     DO_TEST_PARSE_ERROR("usb-hub-conflict",
6ae9ed
             QEMU_CAPS_CHARDEV, QEMU_CAPS_USB_HUB,
6ae9ed
             QEMU_CAPS_NODEFCONFIG);
6ae9ed
+    DO_TEST_PARSE_ERROR("usb-hub-nonexistent",
6ae9ed
+            QEMU_CAPS_CHARDEV, QEMU_CAPS_USB_HUB,
6ae9ed
+            QEMU_CAPS_NODEFCONFIG);
6ae9ed
     DO_TEST("usb-port-missing",
6ae9ed
             QEMU_CAPS_CHARDEV, QEMU_CAPS_USB_HUB,
6ae9ed
             QEMU_CAPS_NODEFCONFIG);
6ae9ed
-- 
6ae9ed
2.9.2
6ae9ed