ua-daemon: Fix resource leak, possible NULL derefernce and coverity issues 32/224232/1
authorAbhay Agarwal <ay.agarwal@samsung.com>
Thu, 30 Jan 2020 05:39:36 +0000 (11:09 +0530)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Fri, 7 Feb 2020 06:46:26 +0000 (15:46 +0900)
Change-Id: Ib94b45ee765549198c5b56f0967a78e6ac121c06
Signed-off-by: Abhay Agarwal <ay.agarwal@samsung.com>
ua-daemon/src/ua-manager-core.c

index da0b1f3..997b0d3 100644 (file)
@@ -711,7 +711,7 @@ static void __uam_copy_db_payload_info(uam_ble_payload_s *dst_payload,
        FUNC_ENTRY;
 
        int user_data_len = 0;
-       ret_if (NULL == src_payload);
+       ret_if(NULL == src_payload);
 
        dst_payload->primary_key = src_payload->primary_key;
        dst_payload->device_icon = src_payload->device_icon;
@@ -744,7 +744,7 @@ static void __uam_copy_uam_payload_info(
        FUNC_ENTRY;
 
        int user_data_len = 0;
-       ret_if (NULL == src_payload);
+       ret_if(NULL == src_payload);
 
        dst_payload->primary_key = src_payload->primary_key;
        dst_payload->device_icon = src_payload->device_icon;
@@ -1647,7 +1647,7 @@ static int _uam_core_update_svc_dev_info(const char *device_id, uam_tech_type_e
 
        if (discriminant >= 0)
                svc->discriminant = discriminant;
-       if (last_seen >= 0)
+       if (last_seen > 0)
                svc->last_seen = last_seen;
        if (payload_info)
                __uam_copy_uam_payload_info(svc->payload, payload_info);
@@ -2679,6 +2679,7 @@ int _uam_core_handle_device_added(int status,
        GSList *svc_dev_list = NULL;
        uam_db_user_info_t *user = NULL;
        uam_svc_dev_info_t *svc_dev = NULL;
+       uam_db_service_info_t *service = NULL;
 
        UAM_INFO("[%d]", user_id);
 
@@ -2745,19 +2746,6 @@ int _uam_core_handle_device_added(int status,
        }
        user = l->data;
 
-       /* Get default service and add it to device's service list by default */
-       l = g_slist_find_custom(services, UAM_SERVICE_DEFAULT, __compare_svc_name);
-       if (!l) {
-               uam_db_service_info_t *service = g_new0(uam_db_service_info_t, 1);
-               service->name = g_strdup(UAM_SERVICE_DEFAULT);
-               service->cycle = UAM_DETECTION_CYCLE_DEFAULT;
-               services = g_slist_append(services, service);
-               svc_list = g_slist_append(svc_list, service);
-       } else {
-               uam_db_service_info_t *service = l->data;
-               svc_list = g_slist_append(svc_list, service);
-       }
-
        /** updates for svc dev*/
        ret = _uam_core_update_svc_dev_info(dev_info->device_id, dev_info->type,
                                UAM_SERVICE_DEFAULT, dev_info->discriminant, 0, NULL);
@@ -2767,6 +2755,18 @@ int _uam_core_handle_device_added(int status,
                return ret;
        }
 
+       /* Get default service and add it to device's service list by default */
+       l = g_slist_find_custom(services, UAM_SERVICE_DEFAULT, __compare_svc_name);
+       if (!l) {
+               service = g_new0(uam_db_service_info_t, 1);
+               service->name = g_strdup(UAM_SERVICE_DEFAULT);
+               service->cycle = UAM_DETECTION_CYCLE_DEFAULT;
+               services = g_slist_append(services, service);
+       } else {
+               service = l->data;
+       }
+
+       svc_list = g_slist_append(svc_list, service);
        svc_dev = _uam_core_find_svc_dev_info(dev_info->device_id,
                        dev_info->type, UAM_SERVICE_DEFAULT);
        svc_dev_list = g_slist_append(svc_dev_list, svc_dev);
@@ -2992,11 +2992,15 @@ void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor,
 
                /** Compare device payload*/
                /** If payload is NULL, send event to all services */
-               UAM_DBG("Compare payload - primary_key detected: [%d], svc_dev: [%d] ",
-                       payload->primary_key, svc_dev->payload->primary_key);
+               if (payload) {
+                       UAM_DBG("Compare payload - primary_key detected: [%d], svc_dev: [%d] ",
+                               payload->primary_key, svc_dev->payload->primary_key);
 
-               if (payload && (0 != __compare_payload(svc_dev->payload, payload)))
-                       continue;
+                       if (0 != __compare_payload(svc_dev->payload, payload))
+                               continue;
+               } else {
+                       UAM_DBG("Detected payload is NULL");
+               }
 
                UAM_DBG("Send event");
                for (l1 = svc->monitors; NULL != l1; l1 = g_slist_next(l1)) {
@@ -3109,9 +3113,8 @@ int _uam_core_handle_presence_detected(unsigned int sensor,
 
        /** Update payload info in list with user data */
        l = g_slist_find_custom(payloads, payload, __compare_payload);
-       if (l) {
+       if (l)
                __uam_copy_uam_payload_info(l->data, payload);
-       }
 
        retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_INVALID_PARAMETER);
 
@@ -4109,7 +4112,7 @@ int _uam_core_add_payload(uam_ble_payload_s *payload,
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
        uam_db_tech_info_t *tech_info;
-       uam_db_payload_info_t *db_payload = g_new0(uam_db_payload_info_t, 1);
+       uam_db_payload_info_t *db_payload;
        char *mac;
 
        retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
@@ -4124,6 +4127,7 @@ int _uam_core_add_payload(uam_ble_payload_s *payload,
        retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAMETER);
        mac = __get_mac_addr(tech_info);
 
+       db_payload = g_new0(uam_db_payload_info_t, 1);
        __uam_copy_uam_payload_info(db_payload, payload);
        db_payload->device_id = g_strdup(device_id);
        db_payload->tech_type = tech_type;