[virt-tools-list] [vhostmd PATCH 08/18] libmetrics: Fix potential leak of FILE pointer

Jim Fehlig jfehlig at suse.com
Wed Jan 15 22:07:43 UTC 2020


>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 at suse.com>
---
 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





More information about the virt-tools-list mailing list