Blame SOURCES/libvirt-qemu-monitor-Make-qemuMonitorAddObject-more-robust-against-programming-errors.patch

51d9a2
From 1d60f6832c8b14c9a2d18441ea5bb2f054d6418f Mon Sep 17 00:00:00 2001
51d9a2
Message-Id: <1d60f6832c8b14c9a2d18441ea5bb2f054d6418f@dist-git>
51d9a2
From: Peter Krempa <pkrempa@redhat.com>
51d9a2
Date: Tue, 10 Jul 2018 17:41:11 +0200
51d9a2
Subject: [PATCH] qemu: monitor: Make qemuMonitorAddObject more robust against
51d9a2
 programming errors
51d9a2
MIME-Version: 1.0
51d9a2
Content-Type: text/plain; charset=UTF-8
51d9a2
Content-Transfer-Encoding: 8bit
51d9a2
51d9a2
Document and check that @props contains a pointer to a json object and
51d9a2
check that both necessary fields are present. Also mark @props as
51d9a2
NONNULL.
51d9a2
51d9a2
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
51d9a2
Reviewed-by: Ján Tomko <jtomko@redhat.com>
51d9a2
(cherry picked from commit fac0dacd54c02b842c995d0999d9450d09d1e7cd)
51d9a2
51d9a2
https: //bugzilla.redhat.com/show_bug.cgi?id=1598015
51d9a2
Reviewed-by: Ján Tomko <jtomko@redhat.com>
51d9a2
---
51d9a2
 src/qemu/qemu_monitor.c | 23 +++++++++++++++++------
51d9a2
 src/qemu/qemu_monitor.h |  3 ++-
51d9a2
 2 files changed, 19 insertions(+), 7 deletions(-)
51d9a2
51d9a2
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
51d9a2
index 5e0e95cc51..8d1c358f67 100644
51d9a2
--- a/src/qemu/qemu_monitor.c
51d9a2
+++ b/src/qemu/qemu_monitor.c
51d9a2
@@ -3075,8 +3075,9 @@ qemuMonitorCreateObjectProps(virJSONValuePtr *propsret,
51d9a2
 /**
51d9a2
  * qemuMonitorAddObject:
51d9a2
  * @mon: Pointer to monitor object
51d9a2
- * @props: Optional arguments for the given type. The object is consumed and
51d9a2
- *         the pointer is cleared.
51d9a2
+ * @props: Pointer to a JSON object holding configuration of the object to add.
51d9a2
+ *         The object must be non-null and contain at least the "qom-type" and
51d9a2
+ *         "id" field. The object is consumed and the pointer is cleared.
51d9a2
  * @alias: If not NULL, returns the alias of the added object if it was added
51d9a2
  *         successfully to qemu. Caller should free the returned pointer.
51d9a2
  *
51d9a2
@@ -3087,18 +3088,28 @@ qemuMonitorAddObject(qemuMonitorPtr mon,
51d9a2
                      virJSONValuePtr *props,
51d9a2
                      char **alias)
51d9a2
 {
51d9a2
-    const char *type = virJSONValueObjectGetString(*props, "qom-type");
51d9a2
-    const char *id = virJSONValueObjectGetString(*props, "id");
51d9a2
+    const char *type = NULL;
51d9a2
+    const char *id = NULL;
51d9a2
     char *tmp = NULL;
51d9a2
     int ret = -1;
51d9a2
 
51d9a2
+    if (!*props) {
51d9a2
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
51d9a2
+                       _("object props can't be NULL"));
51d9a2
+        goto cleanup;
51d9a2
+    }
51d9a2
+
51d9a2
+    type = virJSONValueObjectGetString(*props, "qom-type");
51d9a2
+    id = virJSONValueObjectGetString(*props, "id");
51d9a2
+
51d9a2
     VIR_DEBUG("type=%s id=%s", NULLSTR(type), NULLSTR(id));
51d9a2
 
51d9a2
     QEMU_CHECK_MONITOR_GOTO(mon, cleanup);
51d9a2
 
51d9a2
-    if (!id) {
51d9a2
+    if (!id || !type) {
51d9a2
         virReportError(VIR_ERR_INTERNAL_ERROR,
51d9a2
-                       _("missing alias for qemu object '%s'"), NULLSTR(type));
51d9a2
+                       _("missing alias or qom-type for qemu object '%s'"),
51d9a2
+                       NULLSTR(type));
51d9a2
         goto cleanup;
51d9a2
     }
51d9a2
 
51d9a2
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
51d9a2
index f4d8225ca5..7bfd4b23a2 100644
51d9a2
--- a/src/qemu/qemu_monitor.h
51d9a2
+++ b/src/qemu/qemu_monitor.h
51d9a2
@@ -812,7 +812,8 @@ int qemuMonitorCreateObjectProps(virJSONValuePtr *propsret,
51d9a2
 
51d9a2
 int qemuMonitorAddObject(qemuMonitorPtr mon,
51d9a2
                          virJSONValuePtr *props,
51d9a2
-                         char **alias);
51d9a2
+                         char **alias)
51d9a2
+    ATTRIBUTE_NONNULL(1);
51d9a2
 
51d9a2
 int qemuMonitorDelObject(qemuMonitorPtr mon,
51d9a2
                          const char *objalias);
51d9a2
-- 
51d9a2
2.18.0
51d9a2