From nobody Fri May 17 01:44:07 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1681378496455451.55526787366296; Thu, 13 Apr 2023 02:34:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pmtLO-0007TA-R6; Thu, 13 Apr 2023 05:34:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pmtLL-0007RQ-Mj for qemu-devel@nongnu.org; Thu, 13 Apr 2023 05:34:15 -0400 Received: from mailgw.kylinos.cn ([124.126.103.232]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pmtLF-0000mj-PV for qemu-devel@nongnu.org; Thu, 13 Apr 2023 05:34:15 -0400 Received: from zdzh5-qitianm428-a376.. [(116.128.244.169)] by mailgw (envelope-from ) (Generic MTA) with ESMTP id 723320184; Thu, 13 Apr 2023 17:33:30 +0800 X-UUID: 8afc7cb4b33647a1b06df1de553f79dd-20230413 X-CID-O-RULE: Release_Ham X-CID-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22, REQID:bbbfd944-00f1-48cf-b9bb-a449c78ca08e, IP:-32 768,URL:-32768,TC:-32768,Content:-32768,EDM:-32768,RT:-32768,SF:-32768,FIL E:-32768,BULK:-32768,RULE:Release_Ham,ACTION:release,TS:0 X-CID-INFO: VERSION:1.1.22, REQID:bbbfd944-00f1-48cf-b9bb-a449c78ca08e, IP:-3276 8,URL:-32768,TC:-32768,Content:-32768,EDM:-32768,RT:-32768,SF:-32768,FILE: -32768,BULK:-32768,RULE:Release_Ham,ACTION:release,TS:0 X-CID-META: VersionHash:120426c, CLOUDID:nil, BulkID:nil, BulkQuantity:0, Recheck: 0,SF:nil,TC:nil,Content:nil,EDM:nil,IP:nil,URL:nil,File:nil,Bulk:nil,QS:ni l,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-UUID: 8afc7cb4b33647a1b06df1de553f79dd-20230413 X-User: zenghao@kylinos.cn From: Hao Zeng To: jonathan.cameron@huawei.com, fan.ni@samsung.com, qemu-devel@nongnu.org, philmd@linaro.org Cc: Hao Zeng , Peter Maydell Subject: [PATCH v4] cxl-cdat:Fix open file not closed in ct3_load_cdat Date: Thu, 13 Apr 2023 17:33:28 +0800 Message-Id: <20230413093328.3689564-1-zenghao@kylinos.cn> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Add_By_Label_Mail_Nextpart_001" Content-Transfer-Encoding: 8bit Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=124.126.103.232; envelope-from=zenghao@kylinos.cn; helo=mailgw.kylinos.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, PP_MIME_FAKE_ASCII_TEXT=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_TVD_MIME_NO_HEADERS=0.01, UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1681378500522100001 --Add_By_Label_Mail_Nextpart_001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain;; charset="utf-8" opened file processor not closed,May cause file processor leaks Fixes: aba578bdac ("hw/cxl: CDAT Data Object Exchange implementation") Signed-off-by: Zeng Hao Suggested-by: Philippe Mathieu-Daud\ufffd\ufffd Suggested-by: Peter Maydell --- ChangeLog: v3-v4: Modify commit information,No code change. v2->v3: Submission of v3 on the basis of v2, based on Philippe Mathieu-Daud= \ufffd\ufffd's suggestion "Pointless bzero in g_malloc0, however this code would be simplified using g_file_get_contents()." v1->v2: - Patch 1: No change in patch v1 - Patch 2: Fix the check on the return value of fread() in ct3_load= _cdat --- hw/cxl/cxl-cdat.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/hw/cxl/cxl-cdat.c b/hw/cxl/cxl-cdat.c index 137abd0992..42c7c2031c 100644 --- a/hw/cxl/cxl-cdat.c +++ b/hw/cxl/cxl-cdat.c @@ -110,29 +110,17 @@ static void ct3_load_cdat(CDATObject *cdat, Error **e= rrp) g_autofree CDATEntry *cdat_st =3D NULL; uint8_t sum =3D 0; int num_ent; - int i =3D 0, ent =3D 1, file_size =3D 0; + int i =3D 0, ent =3D 1; + gsize file_size =3D 0; CDATSubHeader *hdr; - FILE *fp =3D NULL; - + GError *error =3D NULL; /* Read CDAT file and create its cache */ - fp =3D fopen(cdat->filename, "r"); - if (!fp) { - error_setg(errp, "CDAT: Unable to open file"); - return; - } - - fseek(fp, 0, SEEK_END); - file_size =3D ftell(fp); - fseek(fp, 0, SEEK_SET); - cdat->buf =3D g_malloc0(file_size); - - if (fread(cdat->buf, file_size, 1, fp) =3D=3D 0) { - error_setg(errp, "CDAT: File read failed"); + 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; } - - fclose(fp); - if (file_size < sizeof(CDATTableHeader)) { error_setg(errp, "CDAT: File too short"); return; @@ -218,7 +206,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); } --=20 2.37.2 --Add_By_Label_Mail_Nextpart_001 Content-type: Text/plain No virus found Checked by Hillstone Network AntiVirus --Add_By_Label_Mail_Nextpart_001--