diff mbox series

[v2,1/2] cxl-cdat:Fix open file not closed in ct3_load_cdat

Message ID 20230412071633.2660412-2-zenghao@kylinos.cn
State New
Headers show
Series cxl-cdat:Fix two problems of ct3_load_cdat | expand

Commit Message

Hao Zeng April 12, 2023, 7:16 a.m. UTC
opened file processor not closed,May cause file processor leaks
Fixes:aba578bdace5303a441f8a37aad781b5cb06f38c

Signed-off-by: Zeng Hao <zenghao@kylinos.cn>
Suggested-by: Xie Ming <xieming@kylinos.cn>
---
 hw/cxl/cxl-cdat.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Philippe Mathieu-Daudé April 12, 2023, 9:58 a.m. UTC | #1
On 12/4/23 09:16, Hao Zeng wrote:
> opened file processor not closed,May cause file processor leaks
> Fixes:aba578bdace5303a441f8a37aad781b5cb06f38c

Fixes: aba578bdac ("hw/cxl: CDAT Data Object Exchange implementation")
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>

> Signed-off-by: Zeng Hao <zenghao@kylinos.cn>
> Suggested-by: Xie Ming <xieming@kylinos.cn>
> ---
>   hw/cxl/cxl-cdat.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/hw/cxl/cxl-cdat.c b/hw/cxl/cxl-cdat.c
> index 137abd0992..ba7ed1aafd 100644
> --- a/hw/cxl/cxl-cdat.c
> +++ b/hw/cxl/cxl-cdat.c
> @@ -128,6 +128,7 @@ static void ct3_load_cdat(CDATObject *cdat, Error **errp)
>   
>       if (fread(cdat->buf, file_size, 1, fp) == 0) {
>           error_setg(errp, "CDAT: File read failed");
> +        fclose(fp);
>           return;
>       }

Alternatively:

-- >8 --
diff --git a/hw/cxl/cxl-cdat.c b/hw/cxl/cxl-cdat.c
index 137abd0992..e014b51714 100644
--- a/hw/cxl/cxl-cdat.c
+++ b/hw/cxl/cxl-cdat.c
@@ -110,7 +110,7 @@ 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, ent = 1, file_size = 0;
      CDATSubHeader *hdr;
      FILE *fp = NULL;

@@ -126,13 +126,13 @@ static void ct3_load_cdat(CDATObject *cdat, Error 
**errp)
      fseek(fp, 0, SEEK_SET);
      cdat->buf = g_malloc0(file_size);

-    if (fread(cdat->buf, file_size, 1, fp) == 0) {
+    i = fread(cdat->buf, file_size, 1, fp);
+    fclose(fp);
+    if (i == 0) {
          error_setg(errp, "CDAT: File read failed");
          return;
      }

-    fclose(fp);
-
      if (file_size < sizeof(CDATTableHeader)) {
          error_setg(errp, "CDAT: File too short");
          return;
---
diff mbox series

Patch

diff --git a/hw/cxl/cxl-cdat.c b/hw/cxl/cxl-cdat.c
index 137abd0992..ba7ed1aafd 100644
--- a/hw/cxl/cxl-cdat.c
+++ b/hw/cxl/cxl-cdat.c
@@ -128,6 +128,7 @@  static void ct3_load_cdat(CDATObject *cdat, Error **errp)
 
     if (fread(cdat->buf, file_size, 1, fp) == 0) {
         error_setg(errp, "CDAT: File read failed");
+        fclose(fp);
         return;
     }