Add the exception handle logic for socket fd is -1 63/317563/1 accepted/tizen_unified_x_asan accepted/tizen/unified/20250106.154425 accepted/tizen/unified/x/20250106.211619 accepted/tizen/unified/x/asan/20250113.002000
authorDohyun Pyun <dh79.pyun@samsung.com>
Mon, 30 Dec 2024 01:19:21 +0000 (10:19 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Fri, 3 Jan 2025 06:45:30 +0000 (15:45 +0900)
Change-Id: I5e682c59687a7d6661ace45998d02107399abd5a
Signed-off-by: Dohyun Pyun <dh79.pyun@samsung.com>
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
bt-api/bt-l2cap-le-server.c
bt-api/bt-rfcomm-server.c

index a4e767cc130bf6b9f381638614e3f69a705e41bf..482993b8220c13d0713bec2bc0bbc0d4a78b61dd 100644 (file)
@@ -298,7 +298,7 @@ static void __remove_l2cap_le_server(l2cap_le_server_info_t *info)
        BT_DBG("-");
 }
 
-static void __connected_cb(l2cap_le_remote_client_info_t *client_info,
+static void __connected_cb(int result, l2cap_le_remote_client_info_t *client_info,
                                        bt_event_info_t *event_info)
 {
        bluetooth_l2cap_le_connection_t conn_info;
@@ -316,7 +316,7 @@ static void __connected_cb(l2cap_le_remote_client_info_t *client_info,
 
        BT_INFO_C("Connected [L2CAP_LE Server] psm %d", server_info->psm);
        _bt_common_event_cb(BLUETOOTH_EVENT_L2CAP_LE_CONNECTED,
-                       BLUETOOTH_ERROR_NONE, &conn_info,
+                       result, &conn_info,
                        event_info->cb, event_info->user_data);
 }
 
@@ -489,6 +489,7 @@ static gboolean __new_connection_request_cb(GIOChannel *chan,
        int client_fd;
        char buf[BLUETOOTH_SOCK_CONNECT_INFO_LEN];
        unsigned char addr[BT_ADDRESS_LENGTH_MAX];
+       int result = BLUETOOTH_ERROR_NONE;
 
        bt_event_info_t *event_info;
        GIOChannel *io;
@@ -547,18 +548,26 @@ static gboolean __new_connection_request_cb(GIOChannel *chan,
        BT_INFO("New client [%s] connection with socket_fd: %d, server_id: %d",
                        rem_client->addr, rem_client->sock_fd, rem_client->server_id);
 
-       io = g_io_channel_unix_new(rem_client->sock_fd);
-       g_io_channel_set_encoding(io, NULL, NULL);
-       g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
-       rem_client->watch_id = g_io_add_watch(io,
-                       G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
-                       __data_received_cb, rem_client);
-       g_io_channel_unref(io);
+       if (client_fd >= 0) {
+               io = g_io_channel_unix_new(rem_client->sock_fd);
+               g_io_channel_set_encoding(io, NULL, NULL);
+               g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
+               rem_client->watch_id = g_io_add_watch(io,
+                               G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+                               __data_received_cb, rem_client);
+               g_io_channel_unref(io);
+
+               server_info->conn_list = g_slist_append(server_info->conn_list, rem_client);
+       } else {
+               result = BLUETOOTH_ERROR_INTERNAL;
+       }
 
-       server_info->conn_list = g_slist_append(server_info->conn_list, rem_client);
        event_info = _bt_event_get_cb_data(BT_L2CAP_LE_SERVER_EVENT);
        if (event_info)
-               __connected_cb(rem_client, event_info);
+               __connected_cb(result, rem_client, event_info);
+
+       if (result != BLUETOOTH_ERROR_NONE)
+               g_free(rem_client);
 
        return TRUE;
 
index 7a4e32a8a9fffa9784ba7ffc69e9f6f9d2b525d7..26a020db5e6fd23ec66843ed1941e6dde2c8307b 100644 (file)
@@ -179,7 +179,7 @@ gboolean _check_uuid_path(char *path, char *uuid)
        return FALSE;
 }
 
-static void __connected_cb(rfcomm_info_t *info, rfcomm_conn_t *conn,
+static void __connected_cb(int result, rfcomm_info_t *info, rfcomm_conn_t *conn,
                           bt_event_info_t *event_info)
 {
        bluetooth_rfcomm_connection_t conn_info;
@@ -199,7 +199,7 @@ static void __connected_cb(rfcomm_info_t *info, rfcomm_conn_t *conn,
                BT_ERR("Fail to send the connection info");
 
        _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
-                       BLUETOOTH_ERROR_NONE, &conn_info,
+                       result, &conn_info,
                        event_info->cb, event_info->user_data);
 }
 
@@ -376,6 +376,7 @@ int new_server_connection(const char *path, int fd, bluetooth_device_address_t *
        rfcomm_conn_t *conn;
        GIOChannel *data_io;
        bt_event_info_t *event_info;
+       int result = BLUETOOTH_ERROR_NONE;
 
        BT_INFO("%s %d", path, fd);
 
@@ -400,22 +401,27 @@ int new_server_connection(const char *path, int fd, bluetooth_device_address_t *
        conn = g_new0(rfcomm_conn_t, 1);
        conn->fd = fd;
        memcpy(&conn->addr, addr, sizeof(bluetooth_device_address_t));
-       info->rfcomm_conns = g_slist_append(info->rfcomm_conns, conn);
-
-       data_io = g_io_channel_unix_new(conn->fd);
 
-       g_io_channel_set_encoding(data_io, NULL, NULL);
-       g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
+       if (fd >= 0) {
+               data_io = g_io_channel_unix_new(conn->fd);
+               g_io_channel_set_encoding(data_io, NULL, NULL);
+               g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
+               conn->watch_id = g_io_add_watch(data_io,
+                               G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+                               __data_received_cb, info);
+               g_io_channel_unref(data_io);
 
-       conn->watch_id = g_io_add_watch(data_io,
-                          G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
-                          __data_received_cb, info);
-
-       g_io_channel_unref(data_io);
+               info->rfcomm_conns = g_slist_append(info->rfcomm_conns, conn);
+       } else {
+               result = BLUETOOTH_ERROR_INTERNAL;
+       }
 
        event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
        if (event_info)
-               __connected_cb(info, conn, event_info);
+               __connected_cb(result, info, conn, event_info);
+
+       if (result != BLUETOOTH_ERROR_NONE)
+               g_free(conn);
 
        return 0;
 }
@@ -814,7 +820,7 @@ static void __remove_rfcomm_server(rfcomm_server_info_t *info)
        BT_DBG("-");
 }
 
-static void __connected_cb(rfcomm_remote_client_info_t *client_info, bt_event_info_t *event_info)
+static void __connected_cb(int result, rfcomm_remote_client_info_t *client_info, bt_event_info_t *event_info)
 {
        bluetooth_rfcomm_connection_t conn_info;
        rfcomm_server_info_t *server_info;
@@ -831,7 +837,7 @@ static void __connected_cb(rfcomm_remote_client_info_t *client_info, bt_event_in
 
        BT_INFO_C("Connected [RFCOMM Server]");
        _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
-                       BLUETOOTH_ERROR_NONE, &conn_info,
+                       result, &conn_info,
                        event_info->cb, event_info->user_data);
 }
 
@@ -1002,6 +1008,7 @@ static gboolean __new_connection_request_cb(GIOChannel *chan, GIOCondition cond,
        int client_fd;
        char buf[BLUETOOTH_SOCK_CONNECT_INFO_LEN];
        unsigned char addr[BT_ADDRESS_LENGTH_MAX];
+       int result = BLUETOOTH_ERROR_NONE;
 
        bt_event_info_t *event_info;
        GIOChannel *io;
@@ -1060,18 +1067,26 @@ static gboolean __new_connection_request_cb(GIOChannel *chan, GIOCondition cond,
        BT_INFO("New client [%s] connection with socket_fd: %d, server_id: %d",
                        rem_client->addr, rem_client->sock_fd, rem_client->server_id);
 
-       io = g_io_channel_unix_new(rem_client->sock_fd);
-       g_io_channel_set_encoding(io, NULL, NULL);
-       g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
-       rem_client->watch_id = g_io_add_watch(io,
-                       G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
-                       __data_received_cb, rem_client);
-       g_io_channel_unref(io);
+       if (client_fd >= 0) {
+               io = g_io_channel_unix_new(rem_client->sock_fd);
+               g_io_channel_set_encoding(io, NULL, NULL);
+               g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
+               rem_client->watch_id = g_io_add_watch(io,
+                               G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+                               __data_received_cb, rem_client);
+               g_io_channel_unref(io);
+
+               server_info->conn_list = g_slist_append(server_info->conn_list, rem_client);
+       } else {
+               result = BLUETOOTH_ERROR_INTERNAL;
+       }
 
-       server_info->conn_list = g_slist_append(server_info->conn_list, rem_client);
        event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
        if (event_info)
-               __connected_cb(rem_client, event_info);
+               __connected_cb(result, rem_client, event_info);
+
+       if (result != BLUETOOTH_ERROR_NONE)
+               g_free(rem_client);
 
        return TRUE;