Fix the coverity issue (Dereference after null check)
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-service.c
index 91149a8..2bcbb9f 100644 (file)
@@ -155,15 +155,15 @@ int bluetooth_gatt_convert_perm2string(
 
 #define NUMBER_OF_FLAGS        10
 
-GDBusConnection *g_conn;
-guint owner_id;
-guint manager_id;
+static GDBusConnection *g_conn;
+static guint owner_id;
+static guint manager_id;
 static gboolean new_service = FALSE;
 static gboolean new_char = FALSE;
 static int serv_id = 1;
 static bool is_server_started = false;
 
-GCancellable *register_cancel;
+static GCancellable *register_cancel;
 
 /* Introspection data for the service we are exporting */
 static const gchar service_introspection_xml[] =
@@ -405,12 +405,20 @@ static gboolean bluetooth_gatt_write_channel_watch_cb(GIOChannel *gio,
                g_io_channel_shutdown(gio, TRUE, NULL);
                g_io_channel_unref(gio);
 
-               gatt_characteristic_server_notify_list = g_slist_remove(gatt_characteristic_server_notify_list, chr_info);
-               bluetooth_characteristic_info_free(chr_info);
+               if (g_slist_find(gatt_characteristic_server_notify_list, chr_info)) {
+                       BT_INFO("found char_info in the list");
+                       gatt_characteristic_server_notify_list = g_slist_remove(gatt_characteristic_server_notify_list, chr_info);
+                       bluetooth_characteristic_info_free(chr_info);
+               }
 
                return FALSE;
        }
 
+       if (g_slist_find(gatt_characteristic_server_notify_list, chr_info) == NULL) {
+               BT_INFO("chr_info is not in the list");
+               return FALSE;
+       }
+
        return TRUE;
 }
 
@@ -1558,12 +1566,18 @@ BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
                        if (err != NULL) {
                                BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
                                                err->code, err->message);
+                               if (err->code == G_DBUS_ERROR_SERVICE_UNKNOWN ||
+                                       g_strrstr(err->message, BT_ERROR_DOES_NOT_EXIST)) {
+                                       g_clear_error(&err);
+                                       goto done;
+                               }
                                g_clear_error(&err);
                        }
                        return BLUETOOTH_ERROR_INTERNAL;
                }
                g_variant_unref(ret);
 
+done:
                is_server_started = false;
 
                BT_INFO("UnregisterApplication is completed");
@@ -2815,12 +2829,8 @@ BT_EXPORT_API int bluetooth_gatt_server_add_new_characteristic(const char *char_
 
        int result;
        char uuid[BT_GATT_ATT_UUID_LEN_MAX + 1];
-       int flag_count = 0;
-       char *char_flags[NUMBER_OF_FLAGS];
 
        g_strlcpy(uuid, char_uuid, sizeof(uuid));
-       flag_count = bluetooth_gatt_convert_prop2string(param->properties, char_flags);
-       BT_INFO("Flag count [%d]", flag_count);
 
        BT_INIT_PARAMS();
        BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
@@ -3338,5 +3348,9 @@ void cleanup_gatt_acquire_fd(int handle)
                        BT_INFO("closing fd");
                        close(chr_info->write_fd);
                }
+
+               BT_INFO("Removing char_info from the list");
+               gatt_characteristic_server_notify_list = g_slist_remove(gatt_characteristic_server_notify_list, chr_info);
+               bluetooth_characteristic_info_free(chr_info);
        }
 }