From 707624b3d956c69dd3cb7b26ad4569898a8bb499 Mon Sep 17 00:00:00 2001 From: Osier Yang Date: Wed, 2 May 2012 23:18:03 +0800 Subject: [PATCH] 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 739cfc31619e9ba8ee450e8762261285093f1f47) --- src/nodeinfo.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/nodeinfo.c b/src/nodeinfo.c index e842474afd..56b9f54cdb 100644 --- a/src/nodeinfo.c +++ b/src/nodeinfo.c @@ -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