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

Laine Stump laine at redhat.com
Fri Feb 12 22:07:13 UTC 2021


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 at redhat.com>
---
 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




More information about the libvir-list mailing list