Blob Blame History Raw
From 1d60f6832c8b14c9a2d18441ea5bb2f054d6418f Mon Sep 17 00:00:00 2001
Message-Id: <1d60f6832c8b14c9a2d18441ea5bb2f054d6418f@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Tue, 10 Jul 2018 17:41:11 +0200
Subject: [PATCH] qemu: monitor: Make qemuMonitorAddObject more robust against
 programming errors
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Document and check that @props contains a pointer to a json object and
check that both necessary fields are present. Also mark @props as
NONNULL.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
(cherry picked from commit fac0dacd54c02b842c995d0999d9450d09d1e7cd)

https: //bugzilla.redhat.com/show_bug.cgi?id=1598015
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/qemu/qemu_monitor.c | 23 +++++++++++++++++------
 src/qemu/qemu_monitor.h |  3 ++-
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 5e0e95cc51..8d1c358f67 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3075,8 +3075,9 @@ qemuMonitorCreateObjectProps(virJSONValuePtr *propsret,
 /**
  * qemuMonitorAddObject:
  * @mon: Pointer to monitor object
- * @props: Optional arguments for the given type. The object is consumed and
- *         the pointer is cleared.
+ * @props: Pointer to a JSON object holding configuration of the object to add.
+ *         The object must be non-null and contain at least the "qom-type" and
+ *         "id" field. The object is consumed and the pointer is cleared.
  * @alias: If not NULL, returns the alias of the added object if it was added
  *         successfully to qemu. Caller should free the returned pointer.
  *
@@ -3087,18 +3088,28 @@ qemuMonitorAddObject(qemuMonitorPtr mon,
                      virJSONValuePtr *props,
                      char **alias)
 {
-    const char *type = virJSONValueObjectGetString(*props, "qom-type");
-    const char *id = virJSONValueObjectGetString(*props, "id");
+    const char *type = NULL;
+    const char *id = NULL;
     char *tmp = NULL;
     int ret = -1;
 
+    if (!*props) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("object props can't be NULL"));
+        goto cleanup;
+    }
+
+    type = virJSONValueObjectGetString(*props, "qom-type");
+    id = virJSONValueObjectGetString(*props, "id");
+
     VIR_DEBUG("type=%s id=%s", NULLSTR(type), NULLSTR(id));
 
     QEMU_CHECK_MONITOR_GOTO(mon, cleanup);
 
-    if (!id) {
+    if (!id || !type) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("missing alias for qemu object '%s'"), NULLSTR(type));
+                       _("missing alias or qom-type for qemu object '%s'"),
+                       NULLSTR(type));
         goto cleanup;
     }
 
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index f4d8225ca5..7bfd4b23a2 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -812,7 +812,8 @@ int qemuMonitorCreateObjectProps(virJSONValuePtr *propsret,
 
 int qemuMonitorAddObject(qemuMonitorPtr mon,
                          virJSONValuePtr *props,
-                         char **alias);
+                         char **alias)
+    ATTRIBUTE_NONNULL(1);
 
 int qemuMonitorDelObject(qemuMonitorPtr mon,
                          const char *objalias);
-- 
2.18.0