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 <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2019-11-14 10:05:06 +01:00
parent 1a288c7e8a
commit 6eac0c5436

View 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;
}