[libvirt PATCH 5/9] esx: use g_steal_pointer+g_autofree on return value

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If we put the potential return string into the g_autofreed tmpResult,
and the move it to the returned "result" only as a final step ater, we
can avoid the need to explicitly VIR_FREE (or g_free) on failure.

Signed-off-by: Laine Stump <laine@xxxxxxxxxx>
---
 src/esx/esx_driver.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index 952e769376..47873c0d54 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -275,7 +275,7 @@ esxParseVMXFileName(const char *fileName,
 static char *
 esxFormatVMXFileName(const char *fileName, void *opaque)
 {
-    bool success = false;
+    g_autofree char *tmpResult = NULL;
     char *result = NULL;
     esxVMX_Data *data = opaque;
     g_autofree char *datastoreName = NULL;
@@ -329,10 +329,10 @@ esxFormatVMXFileName(const char *fileName, void *opaque)
         virBufferAddChar(&buffer, separator);
         virBufferAdd(&buffer, directoryAndFileName, -1);
 
-        result = virBufferContentAndReset(&buffer);
+        tmpResult = virBufferContentAndReset(&buffer);
     } else if (*fileName == '/') {
         /* FIXME: need to deal with Windows paths here too */
-        result = g_strdup(fileName);
+        tmpResult = g_strdup(fileName);
     } else {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Could not handle file name '%s'"), fileName);
@@ -341,15 +341,11 @@ esxFormatVMXFileName(const char *fileName, void *opaque)
 
     /* FIXME: Check if referenced path/file really exists */
 
-    success = true;
+    result = g_steal_pointer(&tmpResult);
 
  cleanup:
-    if (! success)
-        VIR_FREE(result);
-
     esxVI_ObjectContent_Free(&datastore);
     esxVI_DatastoreHostMount_Free(&hostMount);
-
     return result;
 }
 
-- 
2.29.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux