virResctrlAllocGetUnused: Use g_autoptr for variables of virResctrlAlloc type

Refactor the handling of variables so that the cleanup section can be
sanitized.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-03-23 17:43:47 +01:00
parent 05350e451c
commit be291cc49d

View File

@ -1874,8 +1874,8 @@ virResctrlAllocNewFromInfo(virResctrlInfoPtr info)
virResctrlAllocPtr virResctrlAllocPtr
virResctrlAllocGetUnused(virResctrlInfoPtr resctrl) virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
{ {
virResctrlAllocPtr ret = NULL; g_autoptr(virResctrlAlloc) ret = NULL;
virResctrlAllocPtr alloc = NULL; g_autoptr(virResctrlAlloc) alloc_default = NULL;
struct dirent *ent = NULL; struct dirent *ent = NULL;
g_autoptr(DIR) dirp = NULL; g_autoptr(DIR) dirp = NULL;
int rv = -1; int rv = -1;
@ -1890,17 +1890,18 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
if (!ret) if (!ret)
return NULL; return NULL;
alloc = virResctrlAllocGetDefault(resctrl); alloc_default = virResctrlAllocGetDefault(resctrl);
if (!alloc) if (!alloc_default)
goto error; return NULL;
virResctrlAllocSubtract(ret, alloc); virResctrlAllocSubtract(ret, alloc_default);
virObjectUnref(alloc);
if (virDirOpen(&dirp, SYSFS_RESCTRL_PATH) < 0) if (virDirOpen(&dirp, SYSFS_RESCTRL_PATH) < 0)
goto error; return NULL;
while ((rv = virDirRead(dirp, &ent, SYSFS_RESCTRL_PATH)) > 0) { while ((rv = virDirRead(dirp, &ent, SYSFS_RESCTRL_PATH)) > 0) {
g_autoptr(virResctrlAlloc) alloc = NULL;
if (STREQ(ent->d_name, "info")) if (STREQ(ent->d_name, "info"))
continue; continue;
@ -1912,24 +1913,15 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Could not read schemata file for group %s"), _("Could not read schemata file for group %s"),
ent->d_name); ent->d_name);
goto error; return NULL;
} }
virResctrlAllocSubtract(ret, alloc); virResctrlAllocSubtract(ret, alloc);
virObjectUnref(alloc);
alloc = NULL;
} }
if (rv < 0) if (rv < 0)
goto error; return NULL;
cleanup: return g_steal_pointer(&ret);
virObjectUnref(alloc);
return ret;
error:
virObjectUnref(ret);
ret = NULL;
goto cleanup;
} }