From f18a5b4389d1ed779998c9f07901fb49db28b279 Mon Sep 17 00:00:00 2001 From: Bruno da Silva Belo Date: Mon, 7 Oct 2019 09:02:10 -0400 Subject: [PATCH] c: coverity: resource leaked when using realloc. Summary: from https://en.cppreference.com/w/c/memory/realloc ``` On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated with free() or realloc(). The original pointer ptr is invalidated and any access to it is undefined behavior (even if reallocation was in-place). On failure, returns a null pointer. The original pointer ptr remains valid and may need to be deallocated with free() or realloc(). ``` So a temporary to test if `realloc` failed then use the original pointer to use `free`. `CID1404749` `CID1404741` Reviewers: lauromoura, felipealmeida, zmike Reviewed By: zmike Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D10284 --- src/lib/elementary/efl_ui_caching_factory.c | 19 ++++++++++++------- src/lib/elementary/efl_ui_widget_factory.c | 17 +++++++++++------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/lib/elementary/efl_ui_caching_factory.c b/src/lib/elementary/efl_ui_caching_factory.c index 7dbfc0aae9..5c714d99b8 100644 --- a/src/lib/elementary/efl_ui_caching_factory.c +++ b/src/lib/elementary/efl_ui_caching_factory.c @@ -201,15 +201,15 @@ _efl_ui_caching_factory_efl_ui_factory_create(Eo *obj, Efl_Ui_Caching_Factory_Data *pd, Eina_Iterator *models) { - Efl_Ui_Caching_Factory_Request *r; - Efl_Ui_Caching_Factory_Group_Request *gr; + Efl_Ui_Caching_Factory_Request *r = NULL; + Efl_Ui_Caching_Factory_Group_Request *gr = NULL; Efl_Gfx_Entity *w = NULL; - Efl_Model *model; - Eina_Future *f; + Efl_Model *model = NULL; + Eina_Future *f = NULL; if (pd->cache && pd->style && !pd->klass) { - Eina_Future **all; + Eina_Future **all = NULL; int count = 0; r = calloc(1, sizeof (Efl_Ui_Caching_Factory_Request)); @@ -228,8 +228,13 @@ _efl_ui_caching_factory_efl_ui_factory_create(Eo *obj, .success = _efl_ui_caching_factory_create_then, .data = r); - all = realloc(all, (count + 1) * sizeof (Eina_Future *)); - if (!all) goto alloc_array_error; + Eina_Future **tmp = realloc(all, (count + 1) * sizeof (Eina_Future *)); + if (!tmp) + { + free(all); + goto alloc_array_error; + } + all = tmp; } eina_iterator_free(models); diff --git a/src/lib/elementary/efl_ui_widget_factory.c b/src/lib/elementary/efl_ui_widget_factory.c index 13160af8e1..5ab9477a85 100644 --- a/src/lib/elementary/efl_ui_widget_factory.c +++ b/src/lib/elementary/efl_ui_widget_factory.c @@ -267,9 +267,9 @@ static Eina_Future * _efl_ui_widget_factory_efl_ui_factory_create(Eo *obj, Efl_Ui_Widget_Factory_Data *pd, Eina_Iterator *models) { - Efl_Ui_Widget_Factory_Request *r; - Eina_Future **f; - Efl_Model *model; + Efl_Ui_Widget_Factory_Request *r = NULL; + Eina_Future **f = NULL; + Efl_Model *model = NULL; int count = 0; if (!pd->klass) @@ -277,7 +277,7 @@ _efl_ui_widget_factory_efl_ui_factory_create(Eo *obj, Efl_Ui_Widget_Factory_Data if (!pd->style) { - Efl_Ui_Widget *w; + Efl_Ui_Widget *w = NULL; Eina_Value r; eina_value_array_setup(&r, EINA_VALUE_TYPE_OBJECT, 4); @@ -309,8 +309,13 @@ _efl_ui_widget_factory_efl_ui_factory_create(Eo *obj, Efl_Ui_Widget_Factory_Data .success = _efl_ui_widget_factory_create_then, .free = _efl_ui_widget_factory_single_cleanup); - f = realloc(f, (count + 1) * sizeof (Eina_Future *)); - if (!f) goto alloc_array_error; + Eina_Future** tmp = realloc(f, (count + 1) * sizeof (Eina_Future *)); + if (!tmp) + { + free(f); + goto alloc_array_error; + } + f = tmp; } eina_iterator_free(models);