Fix the memory leak
[platform/core/connectivity/bluetooth-agent.git] / map-agent / bluetooth_map_agent.c
index 2d18a2c..346dcfc 100644 (file)
@@ -177,6 +177,7 @@ static GVariant *__bt_map_set_delete_status(char *handle, gboolean delete_status
 static void __bt_map_noti_registration(char *remote_addr, gboolean status);
 static void __bt_map_destroy_agent(void);
 
+/* LCOV_EXCL_START */
 /* Create GError from error code and error message */
 static GError *__bt_map_error(int error_code, char *error_message)
 {
@@ -219,7 +220,6 @@ static map_msg_filter_t __bt_map_get_filters(GVariant *filters)
                        DBG("FilterPriority :%u", filter.priority);
                }
        }
-
        return filter;
 }
 
@@ -378,7 +378,7 @@ static GDBusConnection *__bt_map_get_gdbus_connection(void)
        if (map_dbus_conn)
                return map_dbus_conn;
 
-       map_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &err);
+       map_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
        if (!map_dbus_conn) {
                if (err) {
                        ERR("Unable to connect to dbus: %s", err->message);
@@ -390,13 +390,14 @@ static GDBusConnection *__bt_map_get_gdbus_connection(void)
        FN_END;
        return map_dbus_conn;
 }
+/* LCOV_EXCL_STOP */
 
 gboolean is_mns_connected(void)
 {
        if (!g_mns_proxy)
                return FALSE;
        else
-               return TRUE;
+               return TRUE; /* LCOV_EXCL_LINE */
 }
 
 guint64 _bt_validate_uid(int uid, int msg_type)
@@ -413,7 +414,7 @@ guint64 _bt_validate_uid(int uid, int msg_type)
                        return info->map_id;
                }
 
-               list = g_slist_next(list);
+               list = g_slist_next(list); /* LCOV_EXCL_LINE */
        }
 
        FN_END;
@@ -429,7 +430,7 @@ guint64 _bt_add_id(int uid, int msg_type)
 
        DBG("Add id: %d, MsgType:%d", uid, msg_type);
        test = _bt_validate_uid(uid, msg_type);
-       DBG("test: %llx\n", test);
+       DBG("test: %llu\n", (unsigned long long int)test);
        if (test)
                return test;
 
@@ -440,7 +441,7 @@ guint64 _bt_add_id(int uid, int msg_type)
        info->map_id = map_id;
        info->uid = uid;
        info->msg_type = msg_type;
-       DBG("map_id = %llx, uid = %d, MsgType=%d", info->map_id, info->uid, msg_type);
+       DBG("map_id = %llu, uid = %d, MsgType=%d", (unsigned long long int)info->map_id, info->uid, msg_type);
 
        id_list = g_slist_append(id_list, info);
 
@@ -448,6 +449,7 @@ guint64 _bt_add_id(int uid, int msg_type)
        return map_id;
 }
 
+/* LCOV_EXCL_START */
 static struct id_info *__bt_get_id(guint64 map_id)
 {
        FN_START;
@@ -485,6 +487,7 @@ static struct id_info *__bt_get_uid(gchar *handle)
        FN_END;
        return handle_info;
 }
+/* LCOV_EXCL_STOP */
 
 int _bt_update_id(guint64 map_id, int new_uid, int msg_type)
 {
@@ -493,6 +496,7 @@ int _bt_update_id(guint64 map_id, int new_uid, int msg_type)
        GSList *list = id_list;
 
        while (list) {
+               /* LCOV_EXCL_START */
                info = list->data;
 
                if (info->map_id == map_id) {
@@ -502,12 +506,14 @@ int _bt_update_id(guint64 map_id, int new_uid, int msg_type)
                }
 
                list = g_slist_next(list);
+               /* LCOV_EXCL_STOP */
        }
 
        FN_END;
        return -1;
 }
 
+/* LCOV_EXCL_START */
 static void __bt_remove_list(GSList *id_list)
 {
        FN_START;
@@ -518,6 +524,7 @@ static void __bt_remove_list(GSList *id_list)
        g_slist_free_full(id_list, g_free);
        FN_END;
 }
+/* LCOV_EXCL_STOP */
 
 gboolean _bt_verify_sender(message_info_t *msg_info, char *sender)
 {
@@ -574,6 +581,7 @@ void _get_msg_timestamp(time_t *ltime, char *timestamp)
        FN_START;
        struct tm local_time;
        int year;
+       int buf_len = 0;
        int month;
 
        if (!localtime_r(ltime, &local_time))
@@ -581,13 +589,17 @@ void _get_msg_timestamp(time_t *ltime, char *timestamp)
 
        year = local_time.tm_year + 1900; /* years since 1900 */
        month = local_time.tm_mon + 1; /* months since January */
-       snprintf(timestamp, 16, "%04d%02d%02dT%02d%02d%02d", year, month,
-                                       local_time.tm_mday, local_time.tm_hour,
-                                       local_time.tm_min, local_time.tm_sec);
+       buf_len = snprintf(timestamp, 16, "%04hu%02hu%02huT%02hu%02hu%02hu",(unsigned short int)year, (unsigned short int)month,
+                                       (unsigned short int)local_time.tm_mday, (unsigned short int)local_time.tm_hour,
+                                       (unsigned short int)local_time.tm_min, (unsigned short int)local_time.tm_sec);
+
+       if (buf_len < 0)
+               ERR("Encoding error occured in snprintf");
 
        FN_END;
 }
 
+/* LCOV_EXCL_START */
 time_t _get_time_t_from_timestamp(char *timestamp)
 {
        struct tm local_time;
@@ -595,9 +607,27 @@ time_t _get_time_t_from_timestamp(char *timestamp)
        int month;
        time_t int_time;
 
-       sscanf(timestamp, "%04d%02d%02dT%02d%02d%02d", &year, &month,
-                       &local_time.tm_mday, &local_time.tm_hour,
-                       &local_time.tm_min, &local_time.tm_sec);
+       int ymd; /* year, month, day */
+       int hms; /* hour, min, sec */
+       char *ptr, *ptr2;
+
+       ymd = strtol(timestamp, &ptr, 10);
+       hms = strtol(ptr + 1, &ptr2, 10);
+
+       /* Initialize local_time */
+       memset(&local_time, 0, sizeof(struct tm));
+
+       /* parse year, month, day */
+       local_time.tm_mday = ymd % 100;
+       ymd = (ymd - local_time.tm_mday) / 100;
+       month = ymd % 100;
+       year = (ymd - month) / 100;
+
+       /* parse hour, minute, sec */
+       local_time.tm_sec = hms % 100;
+       hms = (hms - local_time.tm_sec) / 100;
+       local_time.tm_min = hms % 100;
+       local_time.tm_hour = (hms - local_time.tm_min) / 100;
 
        local_time.tm_year = year - 1900;
        local_time.tm_mon = month - 1;
@@ -605,6 +635,7 @@ time_t _get_time_t_from_timestamp(char *timestamp)
        int_time = mktime(&local_time);
        return int_time;
 }
+/* LCOV_EXCL_STOP */
 
 gboolean _bt_verify_time(message_info_t *msg_info, map_msg_filter_t *filter)
 {
@@ -642,6 +673,7 @@ gboolean _bt_verify_time(message_info_t *msg_info, map_msg_filter_t *filter)
        dest |= npi & 0x0F;             \
 }
 
+/* LCOV_EXCL_START */
 static int __bt_ascii_to_upper(int ch)
 {
        return (('a' <= (ch) && (ch) <= 'z') ? ((ch) - ('a'-'A')) : (ch));
@@ -798,7 +830,7 @@ static int __bt_sms_encode_time(gchar *addr_field, time_t *tm)
        return index;
 }
 
-gchar *__bt_get_sms_pdu_from_msg_data(gchar *number,
+gchar *_bt_get_sms_pdu_from_msg_data(gchar *number,
                                                char *msg, time_t tm,
                                                int *msg_pdu_len)
 {
@@ -864,6 +896,7 @@ static void __bt_get_sms_sca(TapiHandle *handle, int result, void *data,
        g_sca_info->DialNumLen = scaInfo->DialNumLen;
        FN_END;
 }
+/* LCOV_EXCL_STOP */
 
 void _bt_message_info_free(gpointer data)
 {
@@ -885,6 +918,7 @@ void _bt_message_info_free(gpointer data)
        FN_END;
 }
 
+/* LCOV_EXCL_START */
 static gboolean __bluetooth_map_start_service()
 {
        FN_START;
@@ -899,6 +933,7 @@ static gboolean __bluetooth_map_start_service()
        FN_END;
        return FALSE;
 }
+/* LCOV_EXCL_STOP */
 
 static gboolean __bt_validate_utf8(char **text)
 {
@@ -906,8 +941,10 @@ static gboolean __bt_validate_utf8(char **text)
        if (g_utf8_validate(*text, -1, NULL))
                return TRUE;
 
+       /* LCOV_EXCL_START */
        FN_END;
        return FALSE;
+       /* LCOV_EXCL_STOP */
 }
 
 gboolean _bt_validate_msg_data(message_info_t *msg_info)
@@ -922,6 +959,7 @@ gboolean _bt_validate_msg_data(message_info_t *msg_info)
        if (msg_info->sender_name)
                return __bt_validate_utf8(&msg_info->sender_name);
 
+       /* LCOV_EXCL_START */
        if (msg_info->sender_addressing)
                return __bt_validate_utf8(&msg_info->sender_addressing);
 
@@ -936,8 +974,10 @@ gboolean _bt_validate_msg_data(message_info_t *msg_info)
 
        FN_END;
        return TRUE;
+       /* LCOV_EXCL_STOP */
 }
 
+/* LCOV_EXCL_START */
 static void __bt_mns_client_connect(char *address)
 {
        FN_START;
@@ -1042,6 +1082,7 @@ static void __bt_mns_client_disconnect()
        g_variant_unref(value);
        FN_END;
 }
+/* LCOV_EXCL_STOP */
 
 void _bt_mns_client_event_notify(gchar *event, guint64 handle,
                                        gchar *folder, gchar *old_folder,
@@ -1058,6 +1099,7 @@ void _bt_mns_client_event_notify(gchar *event, guint64 handle,
                return;
        }
 
+       /* LCOV_EXCL_START */
        connection = __bt_map_get_gdbus_connection();
        if (connection == NULL) {
                DBG("Could not get GDBus Connection");
@@ -1093,8 +1135,10 @@ void _bt_mns_client_event_notify(gchar *event, guint64 handle,
        g_variant_unref(value);
        g_object_unref(mns_proxy);
        FN_END;
+       /* LCOV_EXCL_STOP */
 }
 
+/* LCOV_EXCL_START */
 static gchar *__bt_get_map_folder(int folder)
 {
        switch (folder) {
@@ -1496,7 +1540,7 @@ static gboolean __bt_map_dbus_init(void)
                return FALSE;
        }
 
-       owner_id = g_bus_own_name(G_BUS_TYPE_SESSION,
+       owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
                        BT_MAP_SERVICE_NAME,
                        G_BUS_NAME_OWNER_FLAGS_NONE,
                        NULL, NULL, NULL,
@@ -1576,3 +1620,4 @@ int main(void)
        FN_END;
        return EXIT_FAILURE;
 }
+/* LCOV_EXCL_STOP */