virscsihost: use g_autofree more

Remove some obvious uses of VIR_FREE() in favor of automatic
cleanup. This also means, that some variables affected are
brought into the inner most block, so that automatic cleanup is
effective.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2023-03-08 08:48:36 +01:00
parent 6386dd897d
commit 0d1f9e0de0

View File

@ -95,18 +95,19 @@ virSCSIHostFindByPCI(const char *sysfs_prefix,
const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_HOST_PATH; const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_HOST_PATH;
struct dirent *entry = NULL; struct dirent *entry = NULL;
g_autoptr(DIR) dir = NULL; g_autoptr(DIR) dir = NULL;
char *host_link = NULL;
char *host_path = NULL;
char *p = NULL; char *p = NULL;
char *ret = NULL; char *ret = NULL;
char *buf = NULL;
char *unique_path = NULL;
unsigned int read_unique_id; unsigned int read_unique_id;
if (virDirOpen(&dir, prefix) < 0) if (virDirOpen(&dir, prefix) < 0)
return NULL; return NULL;
while (virDirRead(dir, &entry, prefix) > 0) { while (virDirRead(dir, &entry, prefix) > 0) {
g_autofree char *host_link = NULL;
g_autofree char *host_path = NULL;
g_autofree char *unique_path = NULL;
g_autofree char *buf = NULL;
if (!virFileIsLink(entry->d_name)) if (!virFileIsLink(entry->d_name))
continue; continue;
@ -116,17 +117,12 @@ virSCSIHostFindByPCI(const char *sysfs_prefix,
goto cleanup; goto cleanup;
if (!strstr(host_path, parentaddr)) { if (!strstr(host_path, parentaddr)) {
VIR_FREE(host_link);
VIR_FREE(host_path);
continue; continue;
} }
VIR_FREE(host_link);
VIR_FREE(host_path);
unique_path = g_strdup_printf("%s/%s/unique_id", prefix, entry->d_name); unique_path = g_strdup_printf("%s/%s/unique_id", prefix, entry->d_name);
if (!virFileExists(unique_path)) { if (!virFileExists(unique_path)) {
VIR_FREE(unique_path);
continue; continue;
} }
@ -139,10 +135,7 @@ virSCSIHostFindByPCI(const char *sysfs_prefix,
if (virStrToLong_ui(buf, NULL, 10, &read_unique_id) < 0) if (virStrToLong_ui(buf, NULL, 10, &read_unique_id) < 0)
goto cleanup; goto cleanup;
VIR_FREE(buf);
if (read_unique_id != unique_id) { if (read_unique_id != unique_id) {
VIR_FREE(unique_path);
continue; continue;
} }
@ -151,10 +144,6 @@ virSCSIHostFindByPCI(const char *sysfs_prefix,
} }
cleanup: cleanup:
VIR_FREE(unique_path);
VIR_FREE(host_link);
VIR_FREE(host_path);
VIR_FREE(buf);
return ret; return ret;
} }
@ -226,7 +215,7 @@ virSCSIHostGetNameByParentaddr(unsigned int domain,
unsigned int unique_id) unsigned int unique_id)
{ {
char *name = NULL; char *name = NULL;
char *parentaddr = NULL; g_autofree char *parentaddr = NULL;
parentaddr = g_strdup_printf("%04x:%02x:%02x.%01x", domain, bus, slot, parentaddr = g_strdup_printf("%04x:%02x:%02x.%01x", domain, bus, slot,
function); function);
@ -239,7 +228,6 @@ virSCSIHostGetNameByParentaddr(unsigned int domain,
} }
cleanup: cleanup:
VIR_FREE(parentaddr);
return name; return name;
} }