OSDN Git Service

android/gatt: Refator bt_gatt_register function
authorSzymon Janc <szymon.janc@tieto.com>
Tue, 1 Apr 2014 12:04:57 +0000 (14:04 +0200)
committerSzymon Janc <szymon.janc@tieto.com>
Tue, 1 Apr 2014 12:06:28 +0000 (14:06 +0200)
This fix possible memory leaks and not unregistering IPC handlers if
function failed.

android/gatt.c

index 75a80a5..941582a 100644 (file)
@@ -2095,31 +2095,32 @@ bool bt_gatt_register(struct ipc *ipc, const bdaddr_t *addr)
 {
        DBG("");
 
-       bacpy(&adapter_addr, addr);
+       conn_list = queue_new();
+       conn_wait_queue = queue_new();
+       gatt_clients = queue_new();
 
-       hal_ipc = ipc;
+       if (!conn_list || !conn_wait_queue || !gatt_clients) {
+               error("gatt: Failed to allocate memory for queues");
 
-       conn_list = queue_new();
-       if (!conn_list) {
-               error("gatt: Can not create conn queue");
-               return false;
-       }
+               queue_destroy(gatt_clients, NULL);
+               gatt_clients = NULL;
+
+               queue_destroy(conn_list, NULL);
+               conn_list = NULL;
+
+               queue_destroy(conn_wait_queue, NULL);
+               conn_wait_queue = NULL;
 
-       conn_wait_queue = queue_new();
-       if (!conn_wait_queue) {
-               error("gatt: Can not create conn queue");
                return false;
        }
 
+       bacpy(&adapter_addr, addr);
+
+       hal_ipc = ipc;
+
        ipc_register(hal_ipc, HAL_SERVICE_ID_GATT, cmd_handlers,
                                                G_N_ELEMENTS(cmd_handlers));
 
-       gatt_clients = queue_new();
-       if (!gatt_clients) {
-               error("gatt: Cannot allocate gatt_clients");
-               return false;
-       }
-
        return true;
 }