From nobody Fri May 17 01:44:06 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 1681377699117322.2411531094268; Thu, 13 Apr 2023 02:21:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pmt8N-0002H8-9M; Thu, 13 Apr 2023 05:20:51 -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 1pmt8I-0002Gv-FE for qemu-devel@nongnu.org; Thu, 13 Apr 2023 05:20:46 -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 1pmt8C-0006Sj-59 for qemu-devel@nongnu.org; Thu, 13 Apr 2023 05:20:46 -0400 Received: from zdzh5-qitianm428-a376.. [(116.128.244.169)] by mailgw (envelope-from ) (Generic MTA) with ESMTP id 1821462700; Thu, 13 Apr 2023 17:20:20 +0800 X-UUID: 7b9531da4e8e43448e88accc9332deb2-20230413 X-CID-O-RULE: Release_Ham X-CID-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22, REQID:7fe7ef7a-5fa6-4f1f-b68e-57af7a23af34, IP:5, U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:-9,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:-4 X-CID-INFO: VERSION:1.1.22, REQID:7fe7ef7a-5fa6-4f1f-b68e-57af7a23af34, IP:5, URL :0,TC:0,Content:0,EDM:0,RT:0,SF:-9,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:-4 X-CID-META: VersionHash:120426c, CLOUDID:12e0d6ea-db6f-41fe-8b83-13fe7ed1ef52, B ulkID:230413172022Z67OJRQ1,BulkQuantity:0,Recheck:0,SF:38|24|17|19|42|102, TC:nil,Content:0,EDM:-3,IP:-2,URL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0 ,OSI:0,OSA:0,AV:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-UUID: 7b9531da4e8e43448e88accc9332deb2-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 v3] cxl-cdat:Fix open file not closed in ct3_load_cdat Date: Thu, 13 Apr 2023 17:20:19 +0800 Message-Id: <20230413092019.3663115-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: 1681377702408100001 --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") ChangeLog: 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 Signed-off-by: Zeng Hao Suggested-by: Philippe Mathieu-Daud\ufffd\ufffd Suggested-by: Peter Maydell --- 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--