Coverity: Fix resource leak in nodeinfo.c

Error: RESOURCE_LEAK:
/builddir/build/BUILD/libvirt-0.9.10/src/nodeinfo.c:629: alloc_fn: Calling allocation function "fopen".
/builddir/build/BUILD/libvirt-0.9.10/src/nodeinfo.c:629: var_assign: Assigning: "cpuinfo" =  storage returned from "fopen("/proc/cpuinfo", "r")".
/builddir/build/BUILD/libvirt-0.9.10/src/nodeinfo.c:638: leaked_storage: Variable "cpuinfo" going out of scope leaks the storage it points to.
(cherry picked from commit 739cfc3161)
This commit is contained in:
Osier Yang 2012-05-02 23:18:03 +08:00 committed by Cole Robinson
parent 656875281a
commit 707624b3d9

View File

@ -626,8 +626,8 @@ int nodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED, virNodeInfoPtr nodeinfo) {
#ifdef __linux__
{
int ret;
char *sysfs_cpuinfo;
int ret = -1;
char *sysfs_cpuinfo = NULL;
FILE *cpuinfo = fopen(CPUINFO_PATH, "r");
if (!cpuinfo) {
virReportSystemError(errno,
@ -637,20 +637,19 @@ int nodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED, virNodeInfoPtr nodeinfo) {
if (virAsprintf(&sysfs_cpuinfo, CPU_SYS_PATH) < 0) {
virReportOOMError();
return -1;
goto cleanup;
}
ret = linuxNodeInfoCPUPopulate(cpuinfo, sysfs_cpuinfo, nodeinfo);
VIR_FORCE_FCLOSE(cpuinfo);
if (ret < 0) {
VIR_FREE(sysfs_cpuinfo);
return -1;
}
if (ret < 0)
goto cleanup;
VIR_FREE(sysfs_cpuinfo);
/* Convert to KB. */
nodeinfo->memory = physmem_total () / 1024;
cleanup:
VIR_FORCE_FCLOSE(cpuinfo);
VIR_FREE(sysfs_cpuinfo);
return ret;
}
#else