]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: monitor: Make qemuMonitorAddObject more robust against programming errors
authorPeter Krempa <pkrempa@redhat.com>
Wed, 4 Jul 2018 14:36:37 +0000 (16:36 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 10 Jul 2018 15:32:58 +0000 (17:32 +0200)
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>
src/qemu/qemu_monitor.c
src/qemu/qemu_monitor.h

index 3514e9f8a19e6f0c4362c388016613e2f08c099f..bc116e4e2d9df55280df5cca20fdb0c3d9c3b6f8 100644 (file)
@@ -3088,8 +3088,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.
  *
@@ -3100,18 +3101,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;
     }
 
index e588d736786fdd61444b5ce8acf4f0f939bad504..e8ed2d044c11cbd7aa98857da386954015a77ead 100644 (file)
@@ -823,7 +823,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);