From 6eac0c54365f45ce69f3928afeb7b7362545f5d2 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 14 Nov 2019 10:05:06 +0100 Subject: [PATCH] util: file: Use g_autofree in virFindFileInPath MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify the final lookup loop by freeing memory automatically and thus being able to directly return the result. Signed-off-by: Peter Krempa Reviewed-by: Pavel Hrdina Reviewed-by: Ján Tomko --- 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; }