678486
From 8c5ccd2c990080e581f6cf5c71d8f5603a87bf15 Mon Sep 17 00:00:00 2001
d44865
From: fujiwarat <takao.fujiwara1@gmail.com>
678486
Date: Wed, 20 Jun 2018 17:40:15 +0900
d44865
Subject: [PATCH] bus: Fix SEGV in bus_panel_proxy_focus_in()
d44865
d44865
BUG=rhbz#1349148
d44865
BUG=rhbz#1385349
d44865
BUG=rhbz#1350291
3818b8
BUG=rhbz#1406699
3818b8
BUG=rhbz#1432252
d44865
---
3818b8
 bus/dbusimpl.c | 38 ++++++++++++++++++++++++++++++++------
98919f
 bus/ibusimpl.c | 21 ++++++++++++++++++---
98919f
 2 files changed, 50 insertions(+), 9 deletions(-)
d44865
d44865
diff --git a/bus/dbusimpl.c b/bus/dbusimpl.c
98919f
index b54ef817..e4dd8683 100644
d44865
--- a/bus/dbusimpl.c
d44865
+++ b/bus/dbusimpl.c
d44865
@@ -2,7 +2,8 @@
d44865
 /* vim:set et sts=4: */
d44865
 /* ibus - The Input Bus
d44865
  * Copyright (C) 2008-2013 Peng Huang <shawn.p.huang@gmail.com>
d44865
- * Copyright (C) 2008-2013 Red Hat, Inc.
d44865
+ * Copyright (C) 2015-2017 Takao Fujiwara <takao.fujiwara1@gmail.com>
d44865
+ * Copyright (C) 2008-2017 Red Hat, Inc.
d44865
  *
d44865
  * This library is free software; you can redistribute it and/or
d44865
  * modify it under the terms of the GNU Lesser General Public
3818b8
@@ -344,6 +345,8 @@ bus_name_service_set_primary_owner (BusNameService     *service,
3818b8
                                     BusConnectionOwner *owner,
3818b8
                                     BusDBusImpl        *dbus)
3818b8
 {
3818b8
+    gboolean has_old_owner = FALSE;
3818b8
+
3818b8
     g_assert (service != NULL);
3818b8
     g_assert (owner != NULL);
3818b8
     g_assert (dbus != NULL);
3818b8
@@ -351,6 +354,13 @@ bus_name_service_set_primary_owner (BusNameService     *service,
3818b8
     BusConnectionOwner *old = service->owners != NULL ?
3818b8
             (BusConnectionOwner *)service->owners->data : NULL;
3818b8
 
3818b8
+    /* rhbz#1432252 If bus_connection_get_unique_name() == NULL,
3818b8
+     * "Hello" method is not received yet.
3818b8
+     */
3818b8
+    if (old != NULL && bus_connection_get_unique_name (old->conn) != NULL) {
3818b8
+        has_old_owner = TRUE;
3818b8
+    }
3818b8
+
3818b8
     if (old != NULL) {
3818b8
         g_signal_emit (dbus,
3818b8
                        dbus_signals[NAME_LOST],
3818b8
@@ -370,7 +380,8 @@ bus_name_service_set_primary_owner (BusNameService     *service,
3818b8
                    0,
3818b8
                    owner->conn,
3818b8
                    service->name,
3818b8
-                   old != NULL ? bus_connection_get_unique_name (old->conn) : "",
3818b8
+                   has_old_owner ? bus_connection_get_unique_name (old->conn) :
3818b8
+                           "",
3818b8
                    bus_connection_get_unique_name (owner->conn));
3818b8
 
3818b8
     if (old != NULL && old->do_not_queue != 0) {
3818b8
@@ -427,6 +438,7 @@ bus_name_service_remove_owner (BusNameService     *service,
3818b8
                                BusDBusImpl        *dbus)
3818b8
 {
3818b8
     GSList *owners;
3818b8
+    gboolean has_new_owner = FALSE;
3818b8
 
3818b8
     g_assert (service != NULL);
3818b8
     g_assert (owner != NULL);
3818b8
@@ -439,6 +451,13 @@ bus_name_service_remove_owner (BusNameService     *service,
3818b8
         BusConnectionOwner *_new = NULL;
3818b8
         if (owners->next != NULL) {
3818b8
             _new = (BusConnectionOwner *)owners->next->data;
3818b8
+            /* rhbz#1406699 If bus_connection_get_unique_name() == NULL,
3818b8
+             * "Hello" method is not received yet.
3818b8
+             */
3818b8
+            if (_new != NULL &&
3818b8
+                bus_connection_get_unique_name (_new->conn) != NULL) {
3818b8
+                has_new_owner = TRUE;
3818b8
+            }
3818b8
         }
3818b8
 
3818b8
         if (dbus != NULL) {
3818b8
@@ -447,7 +466,7 @@ bus_name_service_remove_owner (BusNameService     *service,
3818b8
                            0,
3818b8
                            owner->conn,
3818b8
                            service->name);
3818b8
-            if (_new != NULL) {
3818b8
+            if (has_new_owner) {
3818b8
                 g_signal_emit (dbus,
3818b8
                                dbus_signals[NAME_ACQUIRED],
3818b8
                                0,
3818b8
@@ -460,7 +479,7 @@ bus_name_service_remove_owner (BusNameService     *service,
3818b8
                     _new != NULL ? _new->conn : NULL,
3818b8
                     service->name,
3818b8
                     bus_connection_get_unique_name (owner->conn),
3818b8
-                    _new != NULL ? bus_connection_get_unique_name (_new->conn) : "");
3818b8
+                    has_new_owner ? bus_connection_get_unique_name (_new->conn) : "");
3818b8
 
3818b8
         }
3818b8
     }
3818b8
@@ -1464,13 +1483,20 @@ bus_dbus_impl_connection_filter_cb (GDBusConnection *dbus_connection,
d44865
                                     gboolean         incoming,
d44865
                                     gpointer         user_data)
d44865
 {
d44865
+    BusDBusImpl *dbus;
d44865
+    BusConnection *connection;
d44865
+
d44865
     g_assert (G_IS_DBUS_CONNECTION (dbus_connection));
d44865
     g_assert (G_IS_DBUS_MESSAGE (message));
d44865
     g_assert (BUS_IS_DBUS_IMPL (user_data));
d44865
 
d44865
-    BusDBusImpl *dbus = (BusDBusImpl *) user_data;
d44865
-    BusConnection *connection = bus_connection_lookup (dbus_connection);
d44865
+    if (g_dbus_connection_is_closed (dbus_connection))
d44865
+        return NULL;
d44865
+
d44865
+    dbus = (BusDBusImpl *) user_data;
d44865
+    connection = bus_connection_lookup (dbus_connection);
d44865
     g_assert (connection != NULL);
d44865
+    g_assert (BUS_IS_CONNECTION (connection));
d44865
 
d44865
     if (incoming) {
d44865
         /* is incoming message */
d44865
diff --git a/bus/ibusimpl.c b/bus/ibusimpl.c
678486
index ec1caea8..9ae3751b 100644
d44865
--- a/bus/ibusimpl.c
d44865
+++ b/bus/ibusimpl.c
678486
@@ -484,13 +484,16 @@ _dbus_name_owner_changed_cb (BusDBusImpl   *dbus,
678486
     else if (!g_strcmp0 (name, IBUS_SERVICE_PANEL_EXTENSION_EMOJI))
678486
         panel_type = PANEL_TYPE_EXTENSION_EMOJI;
d44865
 
98919f
-    if (panel_type != PANEL_TYPE_NONE) {
d44865
+    do {
98919f
+        if (panel_type == PANEL_TYPE_NONE)
d44865
+            break;
d44865
         if (g_strcmp0 (new_name, "") != 0) {
d44865
             /* a Panel process is started. */
d44865
             BusConnection *connection;
d44865
             BusInputContext *context = NULL;
98919f
             BusPanelProxy   **panel = (panel_type == PANEL_TYPE_PANEL) ?
678486
                                       &ibus->panel : &ibus->emoji_extension;
d44865
+            GDBusConnection *dbus_connection = NULL;
d44865
 
98919f
             if (*panel != NULL) {
98919f
                 ibus_proxy_destroy ((IBusProxy *)(*panel));
678486
@@ -499,9 +502,21 @@ _dbus_name_owner_changed_cb (BusDBusImpl   *dbus,
98919f
                 g_assert (*panel == NULL);
98919f
             }
98919f
 
98919f
-            connection = bus_dbus_impl_get_connection_by_name (BUS_DEFAULT_DBUS, new_name);
98919f
+            connection = bus_dbus_impl_get_connection_by_name (BUS_DEFAULT_DBUS,
98919f
+                                                               new_name);
d44865
             g_return_if_fail (connection != NULL);
d44865
 
d44865
+            dbus_connection = bus_connection_get_dbus_connection (connection);
d44865
+            /* rhbz#1349148 rhbz#1385349
d44865
+             * Avoid SEGV of BUS_IS_PANEL_PROXY (ibus->panel)
d44865
+             * This function is called during destroying the connection
d44865
+             * in this case? */
d44865
+            if (dbus_connection == NULL ||
d44865
+                g_dbus_connection_is_closed (dbus_connection)) {
d44865
+                new_name = "";
d44865
+                break;
d44865
+            }
d44865
+
98919f
             *panel = bus_panel_proxy_new (connection, panel_type);
678486
             if (panel_type == PANEL_TYPE_EXTENSION_EMOJI)
678486
                 ibus->enable_emoji_extension = FALSE;
678486
@@ -555,7 +570,7 @@ _dbus_name_owner_changed_cb (BusDBusImpl   *dbus,
d44865
                 }
d44865
             }
d44865
         }
d44865
-    }
d44865
+    } while (0);
d44865
 
d44865
     bus_ibus_impl_component_name_owner_changed (ibus, name, old_name, new_name);
d44865
 }
d44865
-- 
98919f
2.14.3
d44865