[PATCH 12/21] qemuMonitorJSON(Add|Remove)Netdev: Refactor cleanup

Peter Krempa posted 21 patches 5 years, 8 months ago
[PATCH 12/21] qemuMonitorJSON(Add|Remove)Netdev: Refactor cleanup
Posted by Peter Krempa 5 years, 8 months ago
Use automatic pointer cleanup for virJSONValuePtrs to get rid of the
cleanup label and ret variable.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/qemu/qemu_monitor_json.c | 52 +++++++++++++++---------------------
 1 file changed, 22 insertions(+), 30 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 66422a8489..1224d1c9cc 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -3984,13 +3984,13 @@ int qemuMonitorJSONCloseFileHandle(qemuMonitorPtr mon,
 }


-int qemuMonitorJSONAddNetdev(qemuMonitorPtr mon,
-                             const char *netdevstr)
+int
+qemuMonitorJSONAddNetdev(qemuMonitorPtr mon,
+                         const char *netdevstr)
 {
-    int ret = -1;
-    virJSONValuePtr cmd = NULL;
-    virJSONValuePtr reply = NULL;
-    virJSONValuePtr args = NULL;
+    g_autoptr(virJSONValue) cmd = NULL;
+    g_autoptr(virJSONValue) reply = NULL;
+    g_autoptr(virJSONValue) args = NULL;

     cmd = qemuMonitorJSONMakeCommand("netdev_add", NULL);
     if (!cmd)
@@ -3998,49 +3998,41 @@ int qemuMonitorJSONAddNetdev(qemuMonitorPtr mon,

     args = qemuMonitorJSONKeywordStringToJSON(netdevstr, "type");
     if (!args)
-        goto cleanup;
+        return -1;

     if (virJSONValueObjectAppend(cmd, "arguments", args) < 0)
-        goto cleanup;
+        return -1;
     args = NULL; /* obj owns reference to args now */

     if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
-        goto cleanup;
+        return -1;

     if (qemuMonitorJSONCheckError(cmd, reply) < 0)
-        goto cleanup;
+        return -1;

-    ret = 0;
- cleanup:
-    virJSONValueFree(args);
-    virJSONValueFree(cmd);
-    virJSONValueFree(reply);
-    return ret;
+    return 0;
 }


-int qemuMonitorJSONRemoveNetdev(qemuMonitorPtr mon,
-                                const char *alias)
+int
+qemuMonitorJSONRemoveNetdev(qemuMonitorPtr mon,
+                            const char *alias)
 {
-    int ret = -1;
-    virJSONValuePtr cmd = qemuMonitorJSONMakeCommand("netdev_del",
-                                                     "s:id", alias,
-                                                     NULL);
-    virJSONValuePtr reply = NULL;
+    g_autoptr(virJSONValue) cmd = qemuMonitorJSONMakeCommand("netdev_del",
+                                                             "s:id", alias,
+                                                             NULL);
+    g_autoptr(virJSONValue) reply = NULL;
+
     if (!cmd)
         return -1;

     if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
-        goto cleanup;
+        return -1;

     if (qemuMonitorJSONCheckError(cmd, reply) < 0)
-        goto cleanup;
+        return -1;

-    ret = 0;
- cleanup:
-    virJSONValueFree(cmd);
-    virJSONValueFree(reply);
-    return ret;
+    return 0;
 }


-- 
2.26.2

Re: [PATCH 12/21] qemuMonitorJSON(Add|Remove)Netdev: Refactor cleanup
Posted by Eric Blake 5 years, 8 months ago
On 5/15/20 10:27 AM, Peter Krempa wrote:
> Use automatic pointer cleanup for virJSONValuePtrs to get rid of the
> cleanup label and ret variable.
> 
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---
>   src/qemu/qemu_monitor_json.c | 52 +++++++++++++++---------------------
>   1 file changed, 22 insertions(+), 30 deletions(-)
> 

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org