Fix dbus close connection and Add dangling pointer checker 30/107730/3
authorsooyeon.kim <sooyeon.kim@samsung.com>
Thu, 29 Dec 2016 10:59:03 +0000 (19:59 +0900)
committersooyeon.kim <sooyeon.kim@samsung.com>
Thu, 29 Dec 2016 12:45:40 +0000 (21:45 +0900)
Change-Id: Id608569d8d73abf617b9550b2d6867a07902b9f0
Signed-off-by: sooyeon.kim <sooyeon.kim@samsung.com>
server/ttsd_dbus.c
server/ttsd_server.c

index a882ab1..e993ad5 100644 (file)
@@ -281,6 +281,24 @@ static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handle
        return ECORE_CALLBACK_RENEW;
 }
 
+void __ttsd_dbus_service_free()
+{
+       if (NULL != g_service_name) {
+               free(g_service_name);
+               g_service_name = NULL;
+       }
+
+       if (NULL != g_service_object) {
+               free(g_service_object);
+               g_service_object = NULL;
+       }
+
+       if (NULL != g_service_interface) {
+               free(g_service_interface);
+               g_service_interface = NULL;
+       }
+}
+
 int ttsd_dbus_open_connection()
 {
        DBusError err;
@@ -305,14 +323,22 @@ int ttsd_dbus_open_connection()
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
                dbus_error_free(&err);
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
                return -1;
        }
 
        if (NULL == g_conn_listener) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection");
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
                return -1;
        }
 
+       __ttsd_dbus_service_free();
+
        if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
                g_service_name = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, sizeof(char));
                g_service_object = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
@@ -339,16 +365,40 @@ int ttsd_dbus_open_connection()
                snprintf(g_service_interface, strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SERVER_SERVICE_INTERFACE);
        }
 
+       if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
+               __ttsd_dbus_service_free();
+               dbus_connection_close(g_conn_listener);
+               dbus_connection_unref(g_conn_listener);
+               g_conn_listener = NULL;
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
+               return -1;
+       }
+
        /* request our name on the bus and check for errors */
        ret = dbus_bus_request_name(g_conn_listener, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
        if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to be primary owner");
+               dbus_connection_close(g_conn_listener);
+               dbus_connection_unref(g_conn_listener);
+               g_conn_listener = NULL;
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
                return -1;
        }
 
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to request dbus name : %s", err.message);
                dbus_error_free(&err);
+               dbus_connection_close(g_conn_listener);
+               dbus_connection_unref(g_conn_listener);
+               g_conn_listener = NULL;
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
                return -1;
        }
 
@@ -362,6 +412,12 @@ int ttsd_dbus_open_connection()
 
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
+               dbus_connection_close(g_conn_listener);
+               dbus_connection_unref(g_conn_listener);
+               g_conn_listener = NULL;
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
                return -1;
        }
 
@@ -371,6 +427,12 @@ int ttsd_dbus_open_connection()
        g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
        if (NULL == g_dbus_fd_handler) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get fd handler");
+               dbus_connection_close(g_conn_listener);
+               dbus_connection_unref(g_conn_listener);
+               g_conn_listener = NULL;
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
                return -1;
        }
 
@@ -387,24 +449,25 @@ int ttsd_dbus_close_connection()
                g_dbus_fd_handler = NULL;
        }
 
-       dbus_bus_release_name(g_conn_listener, g_service_name, &err);
-       if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
-               dbus_error_free(&err);
-       }
-
-       dbus_connection_close(g_conn_sender);
-       dbus_connection_close(g_conn_listener);
+       if (NULL != g_conn_listener) {
+               dbus_bus_release_name(g_conn_listener, g_service_name, &err);
+               if (dbus_error_is_set(&err)) {
+                       SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
+                       dbus_error_free(&err);
+               }
 
-       dbus_connection_unref(g_conn_sender);
-       dbus_connection_unref(g_conn_listener);
+               dbus_connection_close(g_conn_listener);
+               dbus_connection_unref(g_conn_listener);
+               g_conn_listener = NULL;
+       }
 
-       g_conn_listener = NULL;
-       g_conn_sender = NULL;
+       if (NULL != g_conn_sender) {
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
+       }
 
-       if (NULL != g_service_name)     free(g_service_name);
-       if (NULL != g_service_object)   free(g_service_object);
-       if (NULL != g_service_interface)free(g_service_interface);
+       __ttsd_dbus_service_free();
 
        return 0;
 }
index 9064378..ef93604 100644 (file)
@@ -520,7 +520,7 @@ int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
 
 static Eina_Bool __quit_ecore_loop(void *data)
 {
-       ttsd_dbus_close_connection();
+//     ttsd_dbus_close_connection();
 
        ttsd_network_finalize();