[libvirt] [PATCH 2/5] util: file: Use g_autofree in virFindFileInPath

Peter Krempa pkrempa at redhat.com
Thu Nov 14 10:43:30 UTC 2019


Simplify the final lookup loop by freeing memory automatically and thus
being able to directly return the result.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virfile.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index b1aeaa5851..072a299b39 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1634,10 +1634,9 @@ char *
 virFindFileInPath(const char *file)
 {
     const char *origpath = NULL;
-    char *path = NULL;
+    g_autofree char *path = NULL;
     char *pathiter;
     char *pathseg;
-    char *fullpath = NULL;

     if (file == NULL)
         return NULL;
@@ -1672,14 +1671,12 @@ virFindFileInPath(const char *file)
      */
     pathiter = path;
     while ((pathseg = strsep(&pathiter, ":")) != NULL) {
-        fullpath = g_strdup_printf("%s/%s", pathseg, file);
+        g_autofree char *fullpath = g_strdup_printf("%s/%s", pathseg, file);
         if (virFileIsExecutable(fullpath))
-            break;
-        VIR_FREE(fullpath);
+            return g_steal_pointer(&fullpath);
     }

-    VIR_FREE(path);
-    return fullpath;
+    return NULL;
 }


-- 
2.23.0




More information about the libvir-list mailing list