]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: file: Use g_autofree in virFindFileInPath
authorPeter Krempa <pkrempa@redhat.com>
Thu, 14 Nov 2019 09:05:06 +0000 (10:05 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 14 Nov 2019 14:50:43 +0000 (15:50 +0100)
Simplify the final lookup loop by freeing memory automatically and thus
being able to directly return the result.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/virfile.c

index b1aeaa585157d6e5af6ccf8dca58aba938ff26c2..072a299b3932b2c5ff6bb38e2fe25e14f466687b 100644 (file)
@@ -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;
 }