surface: use magic number for checking valid of surface 97/251097/1
authorChangyeon Lee <cyeon.lee@samsung.com>
Thu, 7 Jan 2021 08:02:25 +0000 (17:02 +0900)
committerChangyeon Lee <cyeon.lee@samsung.com>
Thu, 7 Jan 2021 11:07:09 +0000 (20:07 +0900)
Change-Id: If6c987d276c97d2a21ba38c76d0a01e66cbdcfe5

src/tbm_bufmgr_int.h
src/tbm_surface_internal.c

index cc789db..da0997d 100644 (file)
@@ -210,6 +210,8 @@ struct _tbm_bufmgr {
  *
  */
 struct _tbm_surface {
+       unsigned int magic;         /* tbm surface magic number */
+
        tbm_bufmgr bufmgr;                      /* tbm buffer manager */
 
        tbm_surface_info_s info;        /* tbm surface information */
index cdafc92..a682fd1 100644 (file)
@@ -41,6 +41,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <png.h>
 #include <pixman.h>
 
+#define TBM_SURFACE_MAGIC 0xBF021234
+
 static tbm_bufmgr g_surface_bufmgr;
 static pthread_mutex_t tbm_surface_lock = PTHREAD_MUTEX_INITIALIZER;
 void _tbm_surface_mutex_unlock(void);
@@ -250,25 +252,30 @@ _deinit_surface_bufmgr(void)
 /* LCOV_EXCL_STOP */
 
 static int
-_tbm_surface_internal_is_valid(tbm_surface_h surface)
+_tbm_surface_internal_magic_check(tbm_surface_h surface)
 {
-       tbm_surface_h old_data = NULL;
+       if (surface->magic != TBM_SURFACE_MAGIC)
+               return 0;
 
-       TBM_RETURN_VAL_IF_FAIL(g_surface_bufmgr, 0);
-       TBM_RETURN_VAL_IF_FAIL(surface, 0);
+       return 1;
+}
 
-       if (!LIST_IS_EMPTY(&g_surface_bufmgr->surf_list)) {
-               LIST_FOR_EACH_ENTRY(old_data, &g_surface_bufmgr->surf_list, item_link) {
-                       if (old_data == surface)
-                               return 1;
-               }
+static int
+_tbm_surface_internal_is_valid(tbm_surface_h surface)
+{
+       if (!surface) {
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
+               TBM_ERR("error: No valid tbm_surface is NULL\n");
+               return 0;
        }
 
-       _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
-
-       TBM_ERR("error: No valid tbm_surface(%p)\n", surface);
+       if (!_tbm_surface_internal_magic_check(surface)) {
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
+               TBM_ERR("error: No valid tbm_surface(%p)\n", surface);
+               return 0;
+       }
 
-       return 0;
+       return 1;
 }
 
 static int
@@ -353,6 +360,7 @@ _tbm_surface_internal_destroy(tbm_surface_h surface)
        }
 
        LIST_DEL(&surface->item_link);
+       surface->magic = 0;
 
        free(surface);
        surface = NULL;
@@ -752,6 +760,7 @@ tbm_surface_internal_create_with_flags(int width, int height,
                /* LCOV_EXCL_STOP */
        }
 
+       surf->magic = TBM_SURFACE_MAGIC;
        surf->bufmgr = bufmgr;
        surf->info.width = width;
        surf->info.height = height;
@@ -998,6 +1007,7 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
                /* LCOV_EXCL_STOP */
        }
 
+       surf->magic = TBM_SURFACE_MAGIC;
        surf->bufmgr = bufmgr;
        surf->info.width = info->width;
        surf->info.height = info->height;