[libvirt] [PATCH 2/2] coverity: fd and memory leak in nodeinfo.c

Guannan Ren gren at redhat.com
Thu May 3 15:28:57 UTC 2012


---
 src/nodeinfo.c |   21 ++++++++++-----------
 1 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/nodeinfo.c b/src/nodeinfo.c
index e842474..012f0f8 100644
--- a/src/nodeinfo.c
+++ b/src/nodeinfo.c
@@ -626,32 +626,31 @@ 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,
                              _("cannot open %s"), CPUINFO_PATH);
-        return -1;
+        return ret;
     }
 
     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;
 
-    return ret;
+    cleanup:
+        VIR_FREE(sysfs_cpuinfo);
+        VIR_FORCE_FCLOSE(cpuinfo);
+        return ret;
     }
 #else
     /* XXX Solaris will need an impl later if they port QEMU driver */
-- 
1.7.7.5




More information about the libvir-list mailing list