[libvirt] [PATCH 6/8] qemu: Don't lookup invalid domains unless specified otherwise

Martin Kletzander mkletzan at redhat.com
Thu Nov 26 13:21:51 UTC 2015


Change qemuDomObjFromDomain() to qemuDomObjFromDomainInternal() with
additional parameter that controls how to deal with invalid domains.
New qemuDomObjFromDomain() then follows the safe path wo we don't have
to change its callers from all APIs and qemuDomObjFromDomainInvalid() is
added as a new function that can be called from APIs that are prepared
to handle invalid definitions as well.

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
 src/qemu/qemu_driver.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 0323a451e4a0..ec369719799e 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -210,15 +210,22 @@ struct qemuAutostartData {
 /**
  * qemuDomObjFromDomain:
  * @domain: Domain pointer that has to be looked up
+ * @invalid: Whether to also return invalid definitions
  *
  * This function looks up @domain and returns the appropriate virDomainObjPtr
  * that has to be released by calling virDomainObjEndAPI().
  *
+ * If @invalid is true, this function can return domain definition with only
+ * name and uuid set, so beware as that can lead to NULL pointer dereference.
+ * This function should be called with @invalid == true only from APIs that are
+ * needed to fix the domain definition (e.g. those needed for looking it up or
+ * providing a new definition.
+ *
  * Returns the domain object with incremented reference counter which is locked
  * on success, NULL otherwise.
  */
 static virDomainObjPtr
-qemuDomObjFromDomain(virDomainPtr domain)
+qemuDomObjFromDomainInternal(virDomainPtr domain, bool invalid)
 {
     virDomainObjPtr vm;
     virQEMUDriverPtr driver = domain->conn->privateData;
@@ -232,10 +239,29 @@ qemuDomObjFromDomain(virDomainPtr domain)
                        uuidstr, domain->name);
         return NULL;
     }
+    if (!invalid && vm->def->parseError) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("domain '%s' was not loaded due to an XML error "
+                         "(%s), please redefine it"),
+                       vm->def->name, vm->def->parseError);
+        virDomainObjEndAPI(&vm);
+    }

     return vm;
 }

+static inline virDomainObjPtr
+qemuDomObjFromDomain(virDomainPtr domain)
+{
+    return qemuDomObjFromDomainInternal(domain, false);
+}
+
+static inline virDomainObjPtr
+qemuDomObjFromDomainInvalid(virDomainPtr domain)
+{
+    return qemuDomObjFromDomainInternal(domain, true);
+}
+
 /* Looks up the domain object from snapshot and unlocks the
  * driver. The returned domain object is locked and ref'd and the
  * caller must call virDomainObjEndAPI() on it. */
-- 
2.6.3




More information about the libvir-list mailing list