aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHao Zeng <zenghao@kylinos.cn>2023-04-21 14:20:19 +0100
committerMichael S. Tsirkin <mst@redhat.com>2023-05-19 01:36:09 -0400
commit71ba92f3488b64bd5c81e2872c56e88cea21bb95 (patch)
tree517399b4ebe17dd377c8d7b68abbab99be8fcb9a
parent5d410557dea452f6231a7c66155e29a37e168528 (diff)
downloadqemu-71ba92f3488b64bd5c81e2872c56e88cea21bb95.zip
qemu-71ba92f3488b64bd5c81e2872c56e88cea21bb95.tar.gz
qemu-71ba92f3488b64bd5c81e2872c56e88cea21bb95.tar.bz2
hw/cxl: cdat: Fix open file not closed in ct3_load_cdat()
Open file descriptor not closed in error paths. Fix by replace open coded handling of read of whole file into a buffer with g_file_get_contents() Fixes: aba578bdac ("hw/cxl: CDAT Data Object Exchange implementation") Signed-off-by: Zeng Hao <zenghao@kylinos.cn> Suggested-by: Philippe Mathieu-Daudé <philmd@linaro.org> Suggested-by: Peter Maydell <peter.maydell@linaro.org> Suggested-by: Jonathan Cameron via <qemu-devel@nongnu.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> -- Changes since v5: - Drop if guard on g_free() as per checkpatch warning. Message-Id: <20230421132020.7408-2-Jonathan.Cameron@huawei.com> Reviewed-by: Fan Ni <fan.ni@samsung.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
-rw-r--r--hw/cxl/cxl-cdat.c29
1 files changed, 8 insertions, 21 deletions
diff --git a/hw/cxl/cxl-cdat.c b/hw/cxl/cxl-cdat.c
index 137abd0..056711d 100644
--- a/hw/cxl/cxl-cdat.c
+++ b/hw/cxl/cxl-cdat.c
@@ -110,29 +110,18 @@ static void ct3_load_cdat(CDATObject *cdat, Error **errp)
g_autofree CDATEntry *cdat_st = NULL;
uint8_t sum = 0;
int num_ent;
- int i = 0, ent = 1, file_size = 0;
+ int i = 0, ent = 1;
+ gsize file_size = 0;
CDATSubHeader *hdr;
- FILE *fp = NULL;
+ GError *error = NULL;
/* Read CDAT file and create its cache */
- fp = fopen(cdat->filename, "r");
- if (!fp) {
- error_setg(errp, "CDAT: Unable to open file");
+ if (!g_file_get_contents(cdat->filename, (gchar **)&cdat->buf,
+ &file_size, &error)) {
+ error_setg(errp, "CDAT: File read failed: %s", error->message);
+ g_error_free(error);
return;
}
-
- fseek(fp, 0, SEEK_END);
- file_size = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- cdat->buf = g_malloc0(file_size);
-
- if (fread(cdat->buf, file_size, 1, fp) == 0) {
- error_setg(errp, "CDAT: File read failed");
- return;
- }
-
- fclose(fp);
-
if (file_size < sizeof(CDATTableHeader)) {
error_setg(errp, "CDAT: File too short");
return;
@@ -218,7 +207,5 @@ void cxl_doe_cdat_release(CXLComponentState *cxl_cstate)
cdat->free_cdat_table(cdat->built_buf, cdat->built_buf_len,
cdat->private);
}
- if (cdat->buf) {
- free(cdat->buf);
- }
+ g_free(cdat->buf);
}