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