render / rpms / libvirt

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