From 1363cd757a8dcdc9adeaf47f781ad038446b2014 Mon Sep 17 00:00:00 2001 From: Pankaj Mittal Date: Thu, 10 Dec 2015 16:26:56 +0900 Subject: [PATCH] evas_eet: Add Type Safety check Summary: The Function _evas_canvas3d_eet_file_free(void) is referenced in evas_model_load_file_eet()(file:evas_model_load_eet.c at line 122). This call is under condition if ((eet_file->mesh == NULL) || (eet_file->header == NULL)). when either eet_file->mesh or eet_file->header are NULL, dereference of the corresponding pointer in function "_evas_canvas3d_eet_file_free()" will generate Segmentation Fault. @fix Reviewers: raster, Hermet, tasn, wonsik, spacegrapher, cedric, jpeg Subscribers: singh.amitesh, sachin.dev, alok25, yashu21985, mvsovani, cedric Differential Revision: https://phab.enlightenment.org/D3369 --- src/lib/evas/canvas/evas_canvas3d_eet.c | 75 ++++++++++++++-------- src/lib/evas/include/evas_common_private.h | 3 + src/lib/evas/include/evas_private.h | 8 ++- .../evas/model_loaders/eet/evas_model_load_eet.c | 13 ++-- .../evas/model_savers/eet/evas_model_save_eet.c | 12 ++-- 5 files changed, 68 insertions(+), 43 deletions(-) diff --git a/src/lib/evas/canvas/evas_canvas3d_eet.c b/src/lib/evas/canvas/evas_canvas3d_eet.c index 281f69d..aed6512 100644 --- a/src/lib/evas/canvas/evas_canvas3d_eet.c +++ b/src/lib/evas/canvas/evas_canvas3d_eet.c @@ -2,18 +2,16 @@ #include "evas_common_private.h" #include "evas_private.h" -Evas_Canvas3D_File_Eet* eet_file; -const char EVAS_CANVAS3D_FILE_CACHE_FILE_ENTRY[] = "evas_3d file"; -Eet_Data_Descriptor *_vec2_descriptor; -Eet_Data_Descriptor *_vec3_descriptor; -Eet_Data_Descriptor *_vertex_descriptor; -Eet_Data_Descriptor *_geometry_descriptor; -Eet_Data_Descriptor *_color_descriptor; -Eet_Data_Descriptor *_material_descriptor; -Eet_Data_Descriptor *_frame_descriptor; -Eet_Data_Descriptor *_mesh_descriptor; -Eet_Data_Descriptor *_header_descriptor; -Eet_Data_Descriptor *_file_descriptor; +static Eet_Data_Descriptor *_vec2_descriptor = NULL; +static Eet_Data_Descriptor *_vec3_descriptor = NULL; +static Eet_Data_Descriptor *_vertex_descriptor = NULL; +static Eet_Data_Descriptor *_geometry_descriptor = NULL; +static Eet_Data_Descriptor *_color_descriptor = NULL; +static Eet_Data_Descriptor *_material_descriptor = NULL; +static Eet_Data_Descriptor *_frame_descriptor = NULL; +static Eet_Data_Descriptor *_mesh_descriptor = NULL; +static Eet_Data_Descriptor *_header_descriptor = NULL; +static Eet_Data_Descriptor *_file_descriptor = NULL; Evas_Canvas3D_File_Eet * _evas_canvas3d_eet_file_new(void) @@ -29,8 +27,16 @@ _evas_canvas3d_eet_file_new(void) return creating_file; } +Eet_Data_Descriptor* +_evas_canvas3d_eet_file_get() +{ + if(_file_descriptor == NULL) + _evas_canvas3d_eet_file_init(); + + return _file_descriptor; +} void -_evas_canvas3d_eet_file_init(void) +_evas_canvas3d_eet_file_init() { eina_init(); eet_init(); @@ -153,38 +159,55 @@ _evas_canvas3d_eet_file_init(void) "mesh", mesh, _mesh_descriptor); EET_DATA_DESCRIPTOR_ADD_SUB(_file_descriptor, Evas_Canvas3D_File_Eet, "header", header, _header_descriptor); - } void -_evas_canvas3d_eet_descriptor_shutdown(void) +_evas_canvas3d_eet_descriptor_shutdown() { eet_data_descriptor_free(_geometry_descriptor); + _geometry_descriptor = NULL; eet_data_descriptor_free(_vertex_descriptor); + _vertex_descriptor = NULL; eet_data_descriptor_free(_vec2_descriptor); + _vec2_descriptor = NULL; eet_data_descriptor_free(_vec3_descriptor); + _vec3_descriptor = NULL; eet_data_descriptor_free(_color_descriptor); + _color_descriptor = NULL; eet_data_descriptor_free(_material_descriptor); + _material_descriptor = NULL; eet_data_descriptor_free(_frame_descriptor); + _frame_descriptor = NULL; eet_data_descriptor_free(_mesh_descriptor); + _mesh_descriptor = NULL; eet_data_descriptor_free(_header_descriptor); + _header_descriptor = NULL; eet_data_descriptor_free(_file_descriptor); + _file_descriptor = NULL; } void -_evas_canvas3d_eet_file_free(void) +_evas_canvas3d_eet_file_free(Evas_Canvas3D_File_Eet* eet_file) { - free(eet_file->mesh->geometries[0].vertices); - free(eet_file->mesh->geometries); - free(eet_file->mesh->frames); - free(eet_file->mesh->materials[0].colors); - free(eet_file->mesh->materials); - free(eet_file->mesh); - free(eet_file->header->materials); - free(eet_file->header->geometries); - free(eet_file->header); - free(eet_file); + if (eet_file->mesh) + { + free(eet_file->mesh->geometries[0].vertices); + free(eet_file->mesh->geometries); + free(eet_file->mesh->frames); + free(eet_file->mesh->materials[0].colors); + free(eet_file->mesh->materials); + free(eet_file->mesh); + } + if (eet_file->header) + { + free(eet_file->header->materials); + free(eet_file->header->geometries); + free(eet_file->header); + } + + free(eet_file); + eet_file = NULL; _evas_canvas3d_eet_descriptor_shutdown(); eet_shutdown(); eina_shutdown(); diff --git a/src/lib/evas/include/evas_common_private.h b/src/lib/evas/include/evas_common_private.h index f7deaa7..cf29f2d 100644 --- a/src/lib/evas/include/evas_common_private.h +++ b/src/lib/evas/include/evas_common_private.h @@ -90,6 +90,9 @@ #define lround(x) (((x) < 0) ? (long int)ceil((x) - 0.5) : (long int)floor((x) + 0.5)) #endif +/*macro to be used in eet loader/saver*/ +#define EVAS_CANVAS3D_FILE_CACHE_FILE_ENTRY "evas_3d file" + /* macros needed to log message through eina_log */ extern EAPI int _evas_log_dom_global; #ifdef _EVAS_DEFAULT_LOG_DOM diff --git a/src/lib/evas/include/evas_private.h b/src/lib/evas/include/evas_private.h index 5ef176f..b6f8c89 100644 --- a/src/lib/evas/include/evas_private.h +++ b/src/lib/evas/include/evas_private.h @@ -1784,9 +1784,11 @@ void evas_canvas3d_scene_data_fini(Evas_Canvas3D_Scene_Public_Data *data); /* Eet saver/loader functions */ Evas_Canvas3D_File_Eet *_evas_canvas3d_eet_file_new(void); -void _evas_canvas3d_eet_file_init(void); -void _evas_canvas3d_eet_descriptor_shutdown(void); -void _evas_canvas3d_eet_file_free(void); +Eet_Data_Descriptor* _evas_canvas3d_eet_file_get(); +void _evas_canvas3d_eet_file_init(); +void _evas_canvas3d_eet_descriptor_shutdown(); +void _evas_canvas3d_eet_file_free(Evas_Canvas3D_File_Eet* eet_file); + /* Filters */ void evas_filter_init(void); diff --git a/src/modules/evas/model_loaders/eet/evas_model_load_eet.c b/src/modules/evas/model_loaders/eet/evas_model_load_eet.c index a36dff4..f5b1b6b 100644 --- a/src/modules/evas/model_loaders/eet/evas_model_load_eet.c +++ b/src/modules/evas/model_loaders/eet/evas_model_load_eet.c @@ -7,10 +7,6 @@ #include "evas_common_private.h" #include "evas_private.h" -extern Evas_Canvas3D_File_Eet* eet_file; -extern const char EVAS_CANVAS3D_FILE_CACHE_FILE_ENTRY[]; -extern Eet_Data_Descriptor *_file_descriptor; - void _set_geometry_to_mesh_from_eet_file(Evas_Canvas3D_Mesh *mesh, Evas_Canvas3D_Mesh_Eet *eet_mesh) @@ -107,8 +103,11 @@ void evas_model_load_file_eet(Evas_Canvas3D_Mesh *mesh, Eina_File *file) { Eet_File *ef; + Evas_Canvas3D_File_Eet* eet_file; + Eet_Data_Descriptor *_file_descriptor; + - _evas_canvas3d_eet_file_init(); + _file_descriptor = _evas_canvas3d_eet_file_get(); ef = eet_mmap(file); eet_file = eet_data_read(ef, _file_descriptor, @@ -119,14 +118,14 @@ evas_model_load_file_eet(Evas_Canvas3D_Mesh *mesh, Eina_File *file) if ((eet_file->mesh == NULL) || (eet_file->header == NULL)) { ERR("Reading of file is failed."); - _evas_canvas3d_eet_file_free(); + _evas_canvas3d_eet_file_free(eet_file); return; } _set_geometry_to_mesh_from_eet_file(mesh, eet_file->mesh); _set_material_to_mesh_from_eet_file(mesh, eet_file->mesh); - _evas_canvas3d_eet_file_free(); + _evas_canvas3d_eet_file_free(eet_file); } diff --git a/src/modules/evas/model_savers/eet/evas_model_save_eet.c b/src/modules/evas/model_savers/eet/evas_model_save_eet.c index ff43a3f..0ac7e66 100644 --- a/src/modules/evas/model_savers/eet/evas_model_save_eet.c +++ b/src/modules/evas/model_savers/eet/evas_model_save_eet.c @@ -9,10 +9,6 @@ #define COMPONENT_OF_DEFAULT_GREY_COLOR 0.3 #define TRANSPARENCY_OF_DEFAULT_GREY_COLOR 0.5 -extern Evas_Canvas3D_File_Eet* eet_file; -extern const char EVAS_CANVAS3D_FILE_CACHE_FILE_ENTRY[]; -extern Eet_Data_Descriptor *_file_descriptor; - void _set_geometry_to_eet_file_from_mesh(Evas_Canvas3D_Mesh_Data *mesh, Evas_Canvas3D_Mesh_Eet *eet_mesh, @@ -154,8 +150,10 @@ evas_model_save_file_eet(const Evas_Canvas3D_Mesh *mesh, Eet_File *ef = eet_open(file, EET_FILE_MODE_WRITE); Evas_Canvas3D_Mesh_Eet* eet_mesh = malloc(sizeof(Evas_Canvas3D_Mesh_Eet)); Evas_Canvas3D_Header_Eet* eet_header = malloc(sizeof(Evas_Canvas3D_Header_Eet)); + Eet_Data_Descriptor *_file_descriptor; + Evas_Canvas3D_File_Eet *eet_file; - _evas_canvas3d_eet_file_init(); + _file_descriptor = _evas_canvas3d_eet_file_get(); eet_file = _evas_canvas3d_eet_file_new(); @@ -176,7 +174,7 @@ evas_model_save_file_eet(const Evas_Canvas3D_Mesh *mesh, ERR("Opening of file is failed."); free(eet_mesh); free(eet_header); - _evas_canvas3d_eet_file_free(); + _evas_canvas3d_eet_file_free(eet_file); return; } @@ -190,6 +188,6 @@ evas_model_save_file_eet(const Evas_Canvas3D_Mesh *mesh, EINA_TRUE); eet_close(ef); - _evas_canvas3d_eet_file_free(); + _evas_canvas3d_eet_file_free(eet_file); } -- 2.7.4