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

Peter Krempa posted 5 patches 6 years, 2 months ago
[libvirt] [PATCH 2/5] util: file: Use g_autofree in virFindFileInPath
Posted by Peter Krempa 6 years, 2 months ago
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>
---
 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

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH 2/5] util: file: Use g_autofree in virFindFileInPath
Posted by Ján Tomko 6 years, 2 months ago
On Thu, Nov 14, 2019 at 11:43:30AM +0100, Peter Krempa wrote:
>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>
>---
> src/util/virfile.c | 11 ++++-------
> 1 file changed, 4 insertions(+), 7 deletions(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 2/5] util: file: Use g_autofree in virFindFileInPath
Posted by Pavel Hrdina 6 years, 2 months ago
On Thu, Nov 14, 2019 at 11:43:30AM +0100, Peter Krempa wrote:
> 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>
> ---
>  src/util/virfile.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list