You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
vhostmd/SOURCES/0009-libmetrics-Fix-potenti...

76 lines
2.1 KiB

From cba4dddebc56886034038f907085da3c6b50baab Mon Sep 17 00:00:00 2001
From: Jim Fehlig <jfehlig@suse.com>
Date: Mon, 6 Jan 2020 18:59:18 -0700
Subject: [PATCH 09/19] libmetrics: Fix potential leak of FILE pointer
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@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.32.0