e_comp_wl_tbm: fix leak of fd in case gbm_bo_get_fd returns new fd 18/295718/1
authorChangyeon Lee <cyeon.lee@samsung.com>
Tue, 11 Jul 2023 07:36:01 +0000 (16:36 +0900)
committerTizen Window System <tizen.windowsystem@gmail.com>
Thu, 13 Jul 2023 01:24:15 +0000 (10:24 +0900)
Change-Id: Ia83ec91584288543ab84406c340b923e6669599e

src/bin/e_comp_wl_tbm.c

index 032f096..8c3a236 100644 (file)
@@ -338,6 +338,35 @@ e_comp_wl_tbm_aligned_width_get(tbm_surface_h tsurface)
    return aligned_width;
 }
 
+static int
+_e_comp_wl_tbm_gbm_bo_fd_get(void *gbo, Eina_Bool *need_close)
+{
+   int fd1 = -1, fd2 = -1;
+
+   fd1 = gbm_bo_get_fd(gbo);
+   if (fd1 < 0)
+      return -1;
+
+   fd2 = gbm_bo_get_fd(gbo);
+   if (fd2 < 0)
+     {
+        close(fd1);
+        return -1;
+     }
+
+   if (fd1 == fd2)
+     {
+        *need_close = EINA_FALSE;
+     }
+   else
+     {
+        *need_close = EINA_TRUE;
+        close(fd2);
+     }
+
+   return fd1;
+}
+
 EINTERN tbm_surface_h
 e_comp_wl_tbm_import_gbm_bo(void *gbo)
 {
@@ -345,6 +374,7 @@ e_comp_wl_tbm_import_gbm_bo(void *gbo)
    tbm_surface_h tsurface = NULL;
    tbm_bo tbo = NULL;
    int fd = -1;
+   Eina_Bool need_close = EINA_FALSE;
    int i;
 
    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp, NULL);
@@ -363,8 +393,7 @@ e_comp_wl_tbm_import_gbm_bo(void *gbo)
         info.planes[i].offset = gbm_bo_get_offset(gbo, i);
      }
 
-   /* do not close fd, gbm backend doesn't return new fd */
-   fd = gbm_bo_get_fd(gbo);
+   fd = _e_comp_wl_tbm_gbm_bo_fd_get(gbo, &need_close);
    EINA_SAFETY_ON_FALSE_GOTO(fd >= 0, end);
 
    tbo = tbm_bo_import_fd(e_comp->e_comp_screen->bufmgr, fd);
@@ -374,6 +403,7 @@ e_comp_wl_tbm_import_gbm_bo(void *gbo)
    EINA_SAFETY_ON_NULL_GOTO(tsurface, end);
 
 end:
+   if ((fd >=0) && (need_close)) close(fd);
    if (tbo) tbm_bo_unref(tbo);
    return tsurface;
 }