[vhostmd PATCH 08/18] libmetrics: Fix potential leak of FILE pointer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>From coverity scan

vhostmd-1.1/libmetrics/libmetrics.c:892: alloc_fn: Storage is returned from allocation function "fopen".
vhostmd-1.1/libmetrics/libmetrics.c:892: var_assign: Assigning: "fp" = storage returned from "fopen(dest_file, "w")".
vhostmd-1.1/libmetrics/libmetrics.c:900: noescape: Resource "fp" is not freed or pointed-to in "fwrite".
vhostmd-1.1/libmetrics/libmetrics.c:909: leaked_storage: Variable "fp" going out of scope leaks the storage it points to.
  907|           free(response);
  908|
  909|->     return 0;
  910|
  911|     error:

Signed-off-by: Jim Fehlig <jfehlig@xxxxxxxx>
---
 libmetrics/libmetrics.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/libmetrics/libmetrics.c b/libmetrics/libmetrics.c
index 0f4cf70..8819074 100644
--- a/libmetrics/libmetrics.c
+++ b/libmetrics/libmetrics.c
@@ -890,10 +890,11 @@ int dump_virtio_metrics(const char *dest_file)
     FILE *fp = stdout;
     char *response = NULL;
     size_t len;
+    int ret = -1;
 
     response = get_virtio_metrics();
     if (response == NULL)
-        goto error;
+        return -1;
 
     len = strlen(response);
 
@@ -902,27 +903,24 @@ int dump_virtio_metrics(const char *dest_file)
         if (fp == NULL) {
             libmsg("%s(), unable to dump metrics: fopen(%s) %s\n",
                    __func__, dest_file, strerror(errno));
-            goto error;
+            goto out;
         }
     }
 
     if (fwrite(response, 1UL, len, fp) != len) {
         libmsg("%s(), unable to export metrics to file:%s %s\n",
                 __func__, dest_file ? dest_file : "stdout", strerror(errno));
-        goto error;
+        goto out;
     }
 
-    if (response)
-        free(response);
+    ret = 0;
 
-    return 0;
-
-  error:
+out:
     if (dest_file && fp)
         fclose(fp);
 
     if (response)
         free(response);
 
-    return -1;
+    return ret;
 }
-- 
2.16.4


_______________________________________________
virt-tools-list mailing list
virt-tools-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/virt-tools-list




[Index of Archives]     [Linux Virtualization]     [KVM Development]     [CentOS Virtualization]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite Forum]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]     [Video 4 Linux]

  Powered by Linux