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