From: Peter Krempa Date: Thu, 14 Nov 2019 09:05:06 +0000 (+0100) Subject: util: file: Use g_autofree in virFindFileInPath X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=6eac0c54365f45ce69f3928afeb7b7362545f5d2;p=libvirt.git util: file: Use g_autofree in virFindFileInPath 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 --- 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; }