[PATCH] qemu_monitor_text.c: Use g_autofree

Seeteena Thoufeek s1seetee at linux.vnet.ibm.com
Tue Mar 24 12:44:46 UTC 2020


Signed-off-by: Seeteena Thoufeek <s1seetee at linux.vnet.ibm.com>
---
 src/qemu/qemu_monitor_text.c | 85 ++++++++++++++++----------------------------
 1 file changed, 30 insertions(+), 55 deletions(-)

diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index 9135a11..5a890af 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -36,27 +36,26 @@ VIR_LOG_INIT("qemu.qemu_monitor_text");
 int qemuMonitorTextAddDrive(qemuMonitorPtr mon,
                             const char *drivestr)
 {
-    char *cmd = NULL;
-    char *reply = NULL;
-    int ret = -1;
+    g_autofree char *cmd = NULL;
+    g_autofree char *reply = NULL;
 
     /* 'dummy' here is just a placeholder since there is no PCI
      * address required when attaching drives to a controller */
     cmd = g_strdup_printf("drive_add dummy %s", drivestr);
 
     if (qemuMonitorJSONHumanCommand(mon, cmd, &reply) < 0)
-        goto cleanup;
+        return -1;
 
     if (strstr(reply, "unknown command:")) {
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("drive hotplug is not supported"));
-        goto cleanup;
+        return -1;
     }
 
     if (strstr(reply, "could not open disk image")) {
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("open disk image file failed"));
-        goto cleanup;
+        return -1;
     }
 
     if (strstr(reply, "Could not open")) {
@@ -66,48 +65,41 @@ int qemuMonitorTextAddDrive(qemuMonitorPtr mon,
 
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        reply);
-        goto cleanup;
+        return -1;
     }
 
     if (strstr(reply, "Image is not in")) {
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("Incorrect disk format"));
-        goto cleanup;
+        return -1;
     }
 
     if (strstr(reply, "IOMMU") ||
         strstr(reply, "VFIO")) {
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        reply);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(cmd);
-    VIR_FREE(reply);
-    return ret;
+    return 0;
 }
 
 
 int qemuMonitorTextDriveDel(qemuMonitorPtr mon,
                             const char *drivestr)
 {
-    char *cmd = NULL;
-    char *reply = NULL;
-    int ret = -1;
+    g_autofree char *cmd = NULL;
+    g_autofree char *reply = NULL;
 
     cmd = g_strdup_printf("drive_del %s", drivestr);
 
     if (qemuMonitorJSONHumanCommand(mon, cmd, &reply) < 0)
-        goto cleanup;
+        return -1;
 
     if (strstr(reply, "unknown command:")) {
         VIR_ERROR(_("deleting drive is not supported.  "
                     "This may leak data if disk is reassigned"));
-        ret = 1;
-        goto cleanup;
+        return 1;
 
     /* (qemu) drive_del wark
      * Device 'wark' not found */
@@ -117,15 +109,10 @@ int qemuMonitorTextDriveDel(qemuMonitorPtr mon,
     } else if (STRNEQ(reply, "")) {
         virReportError(VIR_ERR_OPERATION_FAILED,
                        _("deleting %s drive failed: %s"), drivestr, reply);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(cmd);
-    VIR_FREE(reply);
-    return ret;
+    return 0;
 }
 
 int
@@ -159,28 +146,27 @@ qemuMonitorTextCreateSnapshot(qemuMonitorPtr mon,
 
 int qemuMonitorTextLoadSnapshot(qemuMonitorPtr mon, const char *name)
 {
-    char *cmd = NULL;
-    char *reply = NULL;
-    int ret = -1;
+    g_autofree char *cmd = NULL;
+    g_autofree char *reply = NULL;
 
     cmd = g_strdup_printf("loadvm \"%s\"", name);
 
     if (qemuMonitorJSONHumanCommand(mon, cmd, &reply))
-        goto cleanup;
+        return -1;
 
     if (strstr(reply, "No block device supports snapshots")) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("this domain does not have a device to load snapshots"));
-        goto cleanup;
+        return -1;
     } else if (strstr(reply, "Could not find snapshot")) {
         virReportError(VIR_ERR_OPERATION_INVALID,
                        _("the snapshot '%s' does not exist, and was not loaded"),
                        name);
-        goto cleanup;
+        return -1;
     } else if (strstr(reply, "Snapshots not supported on device")) {
         virReportError(VIR_ERR_OPERATION_INVALID,
                        _("Failed to load snapshot: %s"), reply);
-        goto cleanup;
+        return -1;
     } else if (strstr(reply, "Could not open VM state file") ||
                strstr(reply, "Error: ") ||
                (strstr(reply, "Error") &&
@@ -188,46 +174,35 @@ int qemuMonitorTextLoadSnapshot(qemuMonitorPtr mon, const char *name)
                  strstr(reply, "while activating snapshot on")))) {
         virReportError(VIR_ERR_OPERATION_FAILED,
                        _("Failed to load snapshot: %s"), reply);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(cmd);
-    VIR_FREE(reply);
-    return ret;
+    return 0;
 }
 
 int qemuMonitorTextDeleteSnapshot(qemuMonitorPtr mon, const char *name)
 {
-    char *cmd = NULL;
-    char *reply = NULL;
-    int ret = -1;
+    g_autofree char *cmd = NULL;
+    g_autofree char *reply = NULL;
 
     cmd = g_strdup_printf("delvm \"%s\"", name);
     if (qemuMonitorJSONHumanCommand(mon, cmd, &reply))
-        goto cleanup;
+        return -1;
 
     if (strstr(reply, "No block device supports snapshots")) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("this domain does not have a device to delete snapshots"));
-        goto cleanup;
+        return -1;
     } else if (strstr(reply, "Snapshots not supported on device")) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s", reply);
-        goto cleanup;
+        return -1;
     } else if (strstr(reply, "Error: ") ||
                (strstr(reply, "Error") &&
                 strstr(reply, "while deleting snapshot"))) {
         virReportError(VIR_ERR_OPERATION_FAILED,
                        _("Failed to delete snapshot: %s"), reply);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(cmd);
-    VIR_FREE(reply);
-    return ret;
+    return 0;
 }
-- 
1.8.3.1





More information about the libvir-list mailing list