c: coverity: resource leaked when using realloc.
authorBruno da Silva Belo <brunodasilvabelo@gmail.com>
Mon, 7 Oct 2019 13:02:10 +0000 (09:02 -0400)
committerJongmin Lee <jm105.lee@samsung.com>
Mon, 7 Oct 2019 21:06:31 +0000 (06:06 +0900)
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
src/lib/elementary/efl_ui_widget_factory.c

index 7dbfc0a..5c714d9 100644 (file)
@@ -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);
 
index 13160af..5ab9477 100644 (file)
@@ -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);