[libvirt PATCH 5/9] qemu: use virJSONValueObjectGetStringArray

marcandre.lureau at redhat.com marcandre.lureau at redhat.com
Fri Nov 20 18:09:44 UTC 2020


From: Marc-André Lureau <marcandre.lureau at redhat.com>

There might be more potential users around, I haven't looked thoroughly.

Signed-off-by: Marc-André Lureau <marcandre.lureau at redhat.com>
---
 src/qemu/qemu_monitor_json.c | 34 ++++++----------------------------
 1 file changed, 6 insertions(+), 28 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 723bdb4426..3588a0c426 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -7533,10 +7533,7 @@ qemuMonitorJSONGetStringArray(qemuMonitorPtr mon, const char *qmpCmd,
     int ret = -1;
     virJSONValuePtr cmd;
     virJSONValuePtr reply = NULL;
-    virJSONValuePtr data;
-    char **list = NULL;
-    size_t n = 0;
-    size_t i;
+    g_auto(GStrv) list = NULL;
 
     *array = NULL;
 
@@ -7554,32 +7551,13 @@ qemuMonitorJSONGetStringArray(qemuMonitorPtr mon, const char *qmpCmd,
     if (qemuMonitorJSONCheckReply(cmd, reply, VIR_JSON_TYPE_ARRAY) < 0)
         goto cleanup;
 
-    data = virJSONValueObjectGetArray(reply, "return");
-    n = virJSONValueArraySize(data);
-
-    /* null-terminated list */
-    list = g_new0(char *, n + 1);
-
-    for (i = 0; i < n; i++) {
-        virJSONValuePtr child = virJSONValueArrayGet(data, i);
-        const char *tmp;
-
-        if (!(tmp = virJSONValueGetString(child))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("%s array element does not contain data"),
-                           qmpCmd);
-            goto cleanup;
-        }
-
-        list[i] = g_strdup(tmp);
-    }
-
-    ret = n;
-    *array = list;
-    list = NULL;
+    list = virJSONValueObjectGetStringArray(reply, "return");
+    if (!list)
+        goto cleanup;
+    ret = g_strv_length(list);
+    *array = g_steal_pointer(&list);
 
  cleanup:
-    g_strfreev(list);
     virJSONValueFree(cmd);
     virJSONValueFree(reply);
     return ret;
-- 
2.29.0




More information about the libvir-list mailing list