malloc failed case handling 63/139963/4
authorjungkon.kim <jungkon.kim@samsung.com>
Fri, 21 Jul 2017 06:50:24 +0000 (15:50 +0900)
committerjungkon.kim <jungkon.kim@samsung.com>
Wed, 26 Jul 2017 05:51:21 +0000 (14:51 +0900)
Change-Id: I3aee85e4a6c1c9e0db640a0a6df40aef8ffa2fd6

ui/src/privacy_guard_list_view.c
ui/src/privacy_guard_package_list_view.c

index d1dbe73ed94641b1589a47f5a070fd1e67be207b..8d3ede5b842ff74f9f7bb657fa10c522d00cb2f6 100644 (file)
@@ -39,7 +39,7 @@ static void gl_del_cb(void *data, Evas_Object *obj)
 {
        /* Unrealized callback can be called after this. */
        /* Accessing item_data_s can be dangerous on unrealized callback. */
-       if(!data)
+       if (!data)
                free(data);
 }
 
index c8bcd32d917481537bf856eaad61c1d08bed08fe..94d1615127cfdf30c77af715d8ce56b415ff4205 100644 (file)
@@ -31,6 +31,8 @@
 #include "privacy_view.h"
 
 #define DEFAULT_ICON_PATH _TZ_SYS_RO_APP"/org.tizen.privacy-setting/res/icon/default.png"
+#define SUCCESS 1
+#define FAIL   0
 
 typedef struct pg_data_list_struct {
        pg_data_s *pg_data;
@@ -44,19 +46,45 @@ static int len;
 static pg_data_list *start;
 static pg_data_list *cur;
 
-void init_pg_data_list(void)
+int init_pg_data_list(void)
 {
        start = (pg_data_list *)malloc(sizeof(pg_data_list) * 1);
+       if (start == NULL) {
+               LOGE("Failed to allocate memory");
+               return FAIL;
+       }
+
        start->next = NULL;
        cur = start;
+
+       return SUCCESS;
 }
 
-void add_pg_data(const char *package_id, const int count, time_t time, const int monitor_policy)
+int add_pg_data(const char *package_id, const int count, time_t time, const int monitor_policy)
 {
        pg_data_list *node = (pg_data_list *)malloc(sizeof(pg_data_list) * 1);
+       if (node == NULL) {
+               LOGE("Failed to allocate memory");
+               return FAIL;
+       }
+
        node->pg_data = (pg_data_s *)malloc(sizeof(pg_data_s));
+       if (node->pg_data == NULL) {
+               LOGE("Failed to allocate memory");
+               free(node);
+               return FAIL;
+       }
+
        node->next = NULL;
+
        node->pg_data->pkg_id = strdup(package_id);
+       if (node->pg_data->pkg_id == NULL) {
+               LOGE("Failed to allocate memory");
+               free(node->pg_data);
+               free(node);
+               return FAIL;
+       }
+
        node->pg_data->count = count;
        node->pg_data->time = time;
        node->pg_data->monitor_policy = monitor_policy;
@@ -64,6 +92,8 @@ void add_pg_data(const char *package_id, const int count, time_t time, const int
        cur->next = node;
        cur = node;
        len++;
+
+       return SUCCESS;
 }
 
 static void free_pg_data(pg_data_list *node, pg_data_list *next)
@@ -72,9 +102,13 @@ static void free_pg_data(pg_data_list *node, pg_data_list *next)
                free_pg_data(next, next->next);
 
        if (node == start) {
-               free(start);
+               if (start != NULL) {
+                       free(start);
+                       start = NULL;
+               }
                return;
        } else {
+
                if (node != NULL && node->pg_data != NULL && node->pg_data->pkg_id != NULL)
                        free(node->pg_data->pkg_id);
 
@@ -96,6 +130,7 @@ static void free_item(pg_item_data_s *data, int *is_description)
        if (data != NULL) {
                if (data->label != NULL) {
                        free(data->label);
+
                        if (*is_description == 0) {
                                *is_description = 1;
                                return;
@@ -113,26 +148,43 @@ static void free_item(pg_item_data_s *data, int *is_description)
                if (data->description != NULL)
                        free(data->description);
        }
+
 }
 
-static void _create_privacy_guard_item_list(void)
+static int _create_privacy_guard_item_list(void)
 {
-       item_list = (pg_item_data_s **)malloc(sizeof(pg_item_data_s *) * len);
        int j = 0;
-       for (j = 0; j < len; j++)
+       item_list = (pg_item_data_s **)malloc(sizeof(pg_item_data_s *) * len);
+       if (item_list == NULL) {
+               LOGE("Failed to allocate memory");
+               return FAIL;
+       }
+
+       for (j = 0; j < len; j++) {
                item_list[j] = (pg_item_data_s *)malloc(sizeof(pg_item_data_s));
+               if (item_list[j] == NULL) {
+                       LOGE("Failed to allocate memory");
+                       return FAIL;
+               }
+       }
+
+       return SUCCESS;
 }
 static void _free_privacy_guard_item_list(void)
 {
+
        int is_description = 0;
        int j = 0;
+
        for (j = 0; j < len; j++) {
                free_item(item_list[j], &is_description);
                free(item_list[j]);
        }
+
        free(item_list);
        item_list = NULL;
 }
+
 static void _gl_del_cb(void *data, Evas_Object *obj)
 {
        is_last++;
@@ -177,14 +229,29 @@ static void _privacy_package_check_changed_cb(void *data, Evas_Object *obj, void
 static char* _gl_text_get_cb(void *data, Evas_Object *obj, const char *part)
 {
        pg_item_data_s *id = data;
+       char *ret = NULL;
 
-       if (!strcmp(part, "elm.text"))
-               return strdup(id->label);
+       if (!strcmp(part, "elm.text")) {
+               ret = strdup(id->label);
+               if (ret == NULL) {
+                       LOGE("Failed to allocate memory");
+                       return NULL;
+               }
+
+               return ret;
+       }
 
        if (!strcmp(part, "elm.text.sub") && id->index != 0) {
                if (id->description == NULL)
                        return NULL;
-               return strdup(id->description);
+
+               ret = strdup(id->description);
+               if (ret == NULL) {
+                       LOGE("Failed to allocate memory");
+                       return NULL;
+               }
+
+               return ret;
        }
 
        return NULL;
@@ -227,7 +294,11 @@ static Evas_Object* _gl_content_get_cb(void *data, Evas_Object *obj, const char
 
 static bool _privacy_package_info_cb(const char *package_id, const int count, time_t time, const int monitor_policy, void *user_data)
 {
-       add_pg_data(package_id, count, time, monitor_policy);
+       if (add_pg_data(package_id, count, time, monitor_policy) == FAIL) {
+               LOGE("add_pg_data() is failed.");
+               return false;
+       }
+
        return true;
 }
 
@@ -235,9 +306,13 @@ void create_privacy_guard_package_list_view(struct app_data_s *ad)
 {
        Evas_Object *genlist = NULL;
        is_last = 0;
-       init_pg_data_list();
        len = 1;
 
+       if (init_pg_data_list() == FAIL) {
+               LOGE("init_pg_data_list() is failed.");
+               return;
+       }
+
        /* Get privacy guard data (pkg, count, policy) list by a privacy */
        // user ID
        uid_t user_id = getuid();
@@ -277,7 +352,11 @@ void create_privacy_guard_package_list_view(struct app_data_s *ad)
 
                Elm_Object_Item *it = NULL;
                pg_data_list *l = NULL;
-               _create_privacy_guard_item_list();
+
+               if (_create_privacy_guard_item_list() == FAIL) {
+                       LOGE("_create_privacy_guard_item_list() is failed.");
+                       return;
+               }
                /* Append guide text to the top of genlist */
                pg_item_data_s *description_item = item_list[0];
                description_item->index = 0;
@@ -287,6 +366,10 @@ void create_privacy_guard_package_list_view(struct app_data_s *ad)
                snprintf(str_label, sizeof(str_label), "<wrap=word><ellipsis=-1.0><font_size=35><color=#A9A9A9FF>%s</color></font_size></ellipsis></wrap>", dgettext("privacy-setting", "IDS_ST_BODY_THE_SELECTED_APPS_WILL_BE_MONITORED_FOR_ANY_ATTEMPTS_TO_VIOLATE_YOUR_PRIVACY"));
                description_item->label = strdup(str_label);
 
+               if (description_item->label == NULL) {
+                       LOGE("Failed to allocate memory");
+                       return;
+               }
                it = elm_genlist_item_append(genlist, itc, description_item, NULL, ELM_GENLIST_ITEM_NONE, _privacy_package_selected_cb, description_item);
                elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
 
@@ -307,7 +390,11 @@ void create_privacy_guard_package_list_view(struct app_data_s *ad)
 
                        // package ID
                        item->pkg_id = strdup(data->pkg_id);
-
+                       if (item->pkg_id == NULL) {
+                               LOGE("Failed to allocate memory");
+                               return;
+                       }
+                       
                        // package label
                        res = pkgmgrinfo_pkginfo_get_pkginfo(item->pkg_id, &handle);
                        if (res != PMINFO_R_OK) {
@@ -326,7 +413,11 @@ void create_privacy_guard_package_list_view(struct app_data_s *ad)
                        // title.. (package label)
                        snprintf(str_temp, sizeof(str_temp), "%s", label);
                        item->label = strdup(str_temp);
-
+                       if (item->label == NULL) {
+                               LOGE("Failed to allocate memory");
+                               return;
+                       }
+                       
                        // description - e.g.) 25/08/2010 01:30 PM 21 time(s)
                        if (data->time > 0) {
                                localtime_r(&data->time, &timeinfo);
@@ -337,6 +428,10 @@ void create_privacy_guard_package_list_view(struct app_data_s *ad)
                                snprintf(str_temp, sizeof(str_temp), str_count, data->count);
                                snprintf(str_label, sizeof(str_label), "<font_size=27>%s <font color=#3DB9CCFF>%s</font></font_size>", str_time, str_temp);
                                item->description = strdup(str_label);
+                               if (item->description == NULL) {
+                                       LOGE("Failed to allocate memory");
+                                       return;
+                               }
                        } else {
                                item->description = NULL;
                        }
@@ -346,19 +441,34 @@ void create_privacy_guard_package_list_view(struct app_data_s *ad)
 
                        // privacy ID
                        item->privacy_id = strdup(ad->privacy);
-
+                       if (item->privacy_id == NULL) {
+                               LOGE("Failed to allocate memory");
+                               return;
+                       }
                        // icon
                        res = pkgmgrinfo_pkginfo_get_icon(handle, &icon);
                        if (res != PMINFO_R_OK) {
                                LOGE("Failed to operate pkgmgrinfo_pkginfo_get_icon [%d]", res);
                                LOGE("So replace the icon to the default icon");
                                item->icon = strdup(DEFAULT_ICON_PATH);
+                               if (item->icon == NULL) {
+                                       LOGE("Failed to allocate memory");
+                                       return;
+                               }
                        } else {
                                if (EINA_TRUE == ecore_file_exists(icon)) {
                                        item->icon = strdup(icon);
+                                       if (item->icon == NULL) {
+                                               LOGE("Failed to allocate memory");
+                                               return;
+                                       }
                                } else {
                                        LOGD("The icon is not exist for %s. So replace it to the default icon.", label);
                                        item->icon = strdup(DEFAULT_ICON_PATH);
+                                       if (item->icon == NULL) {
+                                               LOGE("Failed to allocate memory");
+                                               return;
+                                       }
                                }
                        }