Fix API to get device list for user 93/259093/1
authorAbhay Agarwal <ay.agarwal@samsung.com>
Tue, 1 Jun 2021 04:35:57 +0000 (10:05 +0530)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Tue, 1 Jun 2021 05:26:01 +0000 (10:56 +0530)
This patch fix the crash occuring while destroying
device list provided to application on use of API
ua_user_foreach_devices(). The crash was occuring
due to double free of device list.

Change-Id: I17163439ef2b14b5e4ef5dd018275e10bd0b0d54
Signed-off-by: Abhay Agarwal <ay.agarwal@samsung.com>
src/user-awareness-device.c

index d56404564b03f1db1ab20ccccba532dae8fe483f..9239327632d70be59238b637fd3d63db59d27023 100755 (executable)
@@ -192,6 +192,7 @@ void _ua_free_ua_device_info_t(gpointer data)
        g_free(device->device_id);
 
        g_free(device);
+       device = NULL;
        FUNC_EXIT;
 }
 
@@ -346,7 +347,8 @@ int _ua_foreach_devices_by_user(
        devices_list = g_ptr_array_new();
        retv_if(NULL == devices_list, UA_ERROR_OUT_OF_MEMORY);
 
-       ret = _ua_get_error_code(_uam_request_get_user_devices(user->account, &devices_list));
+       ret = _ua_get_error_code(_uam_request_get_user_devices(
+                                       user->account, &devices_list));
        if (UA_ERROR_NONE != ret) {
                UA_ERR("Failed with error: %s(0x%X)",
                                _ua_get_error_string(ret), ret);
@@ -359,45 +361,9 @@ int _ua_foreach_devices_by_user(
                ptr = g_ptr_array_index(devices_list, i);
                if (ptr) {
                        ua_dev_info_s* device_info;
-                       device_info = g_malloc0(sizeof(ua_dev_info_s));
-                       if (!device_info) {
-                               UA_ERR("g_malloc0 failed");
-                               ret = UA_ERROR_OUT_OF_MEMORY;
-                               goto done;
-                       }
-
-                       device_info->mac = g_strdup(ptr->mac);
-                       if (device_info->mac == NULL) {
-                               UA_ERR("g_malloc0 failed");
-                               ret = UA_ERROR_OUT_OF_MEMORY;
-                               _ua_free_ua_device_info_t((gpointer)device_info);
-                               goto done;
-                       }
-
-                       device_info->ipv4 = g_strdup(ptr->ipv4_addr);
-                       if (device_info->ipv4 == NULL) {
-                               UA_ERR("g_malloc0 failed");
-                               ret = UA_ERROR_OUT_OF_MEMORY;
-                               _ua_free_ua_device_info_t((gpointer)device_info);
-                               goto done;
-                       }
-
-                       device_info->device_id = g_strdup(ptr->device_id);
-                       if (device_info->device_id == NULL) {
-                               UA_ERR("g_malloc0 failed");
-                               ret = UA_ERROR_OUT_OF_MEMORY;
-                               _ua_free_ua_device_info_t((gpointer)device_info);
-                               goto done;
-                       }
-
-                       device_info->isadded = true;
-                       device_info->handle = (ua_device_h)device_info;
-                       device_info->type = _to_ua_mac_type(ptr->type);
-                       device_info->os = ptr->operating_system;
-                       device_info->last_seen = ptr->last_seen;
-                       device_info->discriminant = ptr->discriminant;
-                       device_info->user = (ua_user_h)user;
-                       user_devices_list = g_slist_append(user_devices_list, device_info);
+                       device_info = __ua_add_device_info_to_list(ptr);
+                       user_devices_list = g_slist_append(
+                               user_devices_list, device_info);
                } else {
                        UA_ERR("OPERATION_FAILED(0x%08x)",
                                        UA_ERROR_OPERATION_FAILED);
@@ -414,13 +380,16 @@ int _ua_foreach_devices_by_user(
                if (NULL == u)
                        continue;
 
+               if (!(u->isadded))
+                       continue;
+
                if (!foreach_cb(u->handle, user_data))
                        break;
                /* LCOV_EXCL_STOP */
        }
 
 done:
-       g_slist_free_full(user_devices_list, _ua_free_ua_device_info_t);
+       g_slist_free(user_devices_list);
        g_ptr_array_foreach(devices_list, (GFunc)g_free, NULL);
        g_ptr_array_free(devices_list, TRUE);