6c8280
From 865b204f1c06b782cf7b4a479b358e76f4b3dfee Mon Sep 17 00:00:00 2001
6c8280
From: fujiwarat <takao.fujiwara1@gmail.com>
6c8280
Date: Tue, 17 Jul 2018 13:39:30 +0900
6c8280
Subject: [PATCH] bus: Fix SEGV in bus_panel_proxy_focus_in()
6c8280
6c8280
BUG=rhbz#1349148
6c8280
BUG=rhbz#1385349
6c8280
BUG=rhbz#1350291
6c8280
BUG=rhbz#1406699
6c8280
BUG=rhbz#1432252
6c8280
BUG=rhbz#1601577
6c8280
---
6c8280
 bus/dbusimpl.c    | 38 ++++++++++++++++++++++++++++++++------
6c8280
 bus/engineproxy.c |  5 ++++-
6c8280
 bus/ibusimpl.c    | 21 ++++++++++++++++++---
6c8280
 3 files changed, 54 insertions(+), 10 deletions(-)
6c8280
6c8280
diff --git a/bus/dbusimpl.c b/bus/dbusimpl.c
6c8280
index b54ef817..e4dd8683 100644
6c8280
--- a/bus/dbusimpl.c
6c8280
+++ b/bus/dbusimpl.c
6c8280
@@ -2,7 +2,8 @@
6c8280
 /* vim:set et sts=4: */
6c8280
 /* ibus - The Input Bus
6c8280
  * Copyright (C) 2008-2013 Peng Huang <shawn.p.huang@gmail.com>
6c8280
- * Copyright (C) 2008-2013 Red Hat, Inc.
6c8280
+ * Copyright (C) 2015-2017 Takao Fujiwara <takao.fujiwara1@gmail.com>
6c8280
+ * Copyright (C) 2008-2017 Red Hat, Inc.
6c8280
  *
6c8280
  * This library is free software; you can redistribute it and/or
6c8280
  * modify it under the terms of the GNU Lesser General Public
6c8280
@@ -344,6 +345,8 @@ bus_name_service_set_primary_owner (BusNameService     *service,
6c8280
                                     BusConnectionOwner *owner,
6c8280
                                     BusDBusImpl        *dbus)
6c8280
 {
6c8280
+    gboolean has_old_owner = FALSE;
6c8280
+
6c8280
     g_assert (service != NULL);
6c8280
     g_assert (owner != NULL);
6c8280
     g_assert (dbus != NULL);
6c8280
@@ -351,6 +354,13 @@ bus_name_service_set_primary_owner (BusNameService     *service,
6c8280
     BusConnectionOwner *old = service->owners != NULL ?
6c8280
             (BusConnectionOwner *)service->owners->data : NULL;
6c8280
 
6c8280
+    /* rhbz#1432252 If bus_connection_get_unique_name() == NULL,
6c8280
+     * "Hello" method is not received yet.
6c8280
+     */
6c8280
+    if (old != NULL && bus_connection_get_unique_name (old->conn) != NULL) {
6c8280
+        has_old_owner = TRUE;
6c8280
+    }
6c8280
+
6c8280
     if (old != NULL) {
6c8280
         g_signal_emit (dbus,
6c8280
                        dbus_signals[NAME_LOST],
6c8280
@@ -370,7 +380,8 @@ bus_name_service_set_primary_owner (BusNameService     *service,
6c8280
                    0,
6c8280
                    owner->conn,
6c8280
                    service->name,
6c8280
-                   old != NULL ? bus_connection_get_unique_name (old->conn) : "",
6c8280
+                   has_old_owner ? bus_connection_get_unique_name (old->conn) :
6c8280
+                           "",
6c8280
                    bus_connection_get_unique_name (owner->conn));
6c8280
 
6c8280
     if (old != NULL && old->do_not_queue != 0) {
6c8280
@@ -427,6 +438,7 @@ bus_name_service_remove_owner (BusNameService     *service,
6c8280
                                BusDBusImpl        *dbus)
6c8280
 {
6c8280
     GSList *owners;
6c8280
+    gboolean has_new_owner = FALSE;
6c8280
 
6c8280
     g_assert (service != NULL);
6c8280
     g_assert (owner != NULL);
6c8280
@@ -439,6 +451,13 @@ bus_name_service_remove_owner (BusNameService     *service,
6c8280
         BusConnectionOwner *_new = NULL;
6c8280
         if (owners->next != NULL) {
6c8280
             _new = (BusConnectionOwner *)owners->next->data;
6c8280
+            /* rhbz#1406699 If bus_connection_get_unique_name() == NULL,
6c8280
+             * "Hello" method is not received yet.
6c8280
+             */
6c8280
+            if (_new != NULL &&
6c8280
+                bus_connection_get_unique_name (_new->conn) != NULL) {
6c8280
+                has_new_owner = TRUE;
6c8280
+            }
6c8280
         }
6c8280
 
6c8280
         if (dbus != NULL) {
6c8280
@@ -447,7 +466,7 @@ bus_name_service_remove_owner (BusNameService     *service,
6c8280
                            0,
6c8280
                            owner->conn,
6c8280
                            service->name);
6c8280
-            if (_new != NULL) {
6c8280
+            if (has_new_owner) {
6c8280
                 g_signal_emit (dbus,
6c8280
                                dbus_signals[NAME_ACQUIRED],
6c8280
                                0,
6c8280
@@ -460,7 +479,7 @@ bus_name_service_remove_owner (BusNameService     *service,
6c8280
                     _new != NULL ? _new->conn : NULL,
6c8280
                     service->name,
6c8280
                     bus_connection_get_unique_name (owner->conn),
6c8280
-                    _new != NULL ? bus_connection_get_unique_name (_new->conn) : "");
6c8280
+                    has_new_owner ? bus_connection_get_unique_name (_new->conn) : "");
6c8280
 
6c8280
         }
6c8280
     }
6c8280
@@ -1464,13 +1483,20 @@ bus_dbus_impl_connection_filter_cb (GDBusConnection *dbus_connection,
6c8280
                                     gboolean         incoming,
6c8280
                                     gpointer         user_data)
6c8280
 {
6c8280
+    BusDBusImpl *dbus;
6c8280
+    BusConnection *connection;
6c8280
+
6c8280
     g_assert (G_IS_DBUS_CONNECTION (dbus_connection));
6c8280
     g_assert (G_IS_DBUS_MESSAGE (message));
6c8280
     g_assert (BUS_IS_DBUS_IMPL (user_data));
6c8280
 
6c8280
-    BusDBusImpl *dbus = (BusDBusImpl *) user_data;
6c8280
-    BusConnection *connection = bus_connection_lookup (dbus_connection);
6c8280
+    if (g_dbus_connection_is_closed (dbus_connection))
6c8280
+        return NULL;
6c8280
+
6c8280
+    dbus = (BusDBusImpl *) user_data;
6c8280
+    connection = bus_connection_lookup (dbus_connection);
6c8280
     g_assert (connection != NULL);
6c8280
+    g_assert (BUS_IS_CONNECTION (connection));
6c8280
 
6c8280
     if (incoming) {
6c8280
         /* is incoming message */
6c8280
diff --git a/bus/engineproxy.c b/bus/engineproxy.c
6c8280
index 2d98995c..d661673a 100644
6c8280
--- a/bus/engineproxy.c
6c8280
+++ b/bus/engineproxy.c
6c8280
@@ -665,6 +665,7 @@ bus_engine_proxy_new_internal (const gchar     *path,
6c8280
                                IBusEngineDesc  *desc,
6c8280
                                GDBusConnection *connection)
6c8280
 {
6c8280
+    GError *error = NULL;
6c8280
     g_assert (path);
6c8280
     g_assert (IBUS_IS_ENGINE_DESC (desc));
6c8280
     g_assert (G_IS_DBUS_CONNECTION (connection));
6c8280
@@ -673,7 +674,7 @@ bus_engine_proxy_new_internal (const gchar     *path,
6c8280
     BusEngineProxy *engine =
6c8280
         (BusEngineProxy *) g_initable_new (BUS_TYPE_ENGINE_PROXY,
6c8280
                                            NULL,
6c8280
-                                           NULL,
6c8280
+                                           &error,
6c8280
                                            "desc",              desc,
6c8280
                                            "g-connection",      connection,
6c8280
                                            "g-interface-name",  IBUS_INTERFACE_ENGINE,
6c8280
@@ -681,6 +682,8 @@ bus_engine_proxy_new_internal (const gchar     *path,
6c8280
                                            "g-default-timeout", g_gdbus_timeout,
6c8280
                                            "g-flags",           flags,
6c8280
                                            NULL);
6c8280
+    /* FIXME: rhbz#1601577 */
6c8280
+    g_assert_no_error (error);
6c8280
     const gchar *layout = ibus_engine_desc_get_layout (desc);
6c8280
     if (layout != NULL && layout[0] != '\0') {
6c8280
         engine->keymap = ibus_keymap_get (layout);
6c8280
diff --git a/bus/ibusimpl.c b/bus/ibusimpl.c
6c8280
index ec1caea8..9ae3751b 100644
6c8280
--- a/bus/ibusimpl.c
6c8280
+++ b/bus/ibusimpl.c
6c8280
@@ -484,13 +484,16 @@ _dbus_name_owner_changed_cb (BusDBusImpl   *dbus,
6c8280
     else if (!g_strcmp0 (name, IBUS_SERVICE_PANEL_EXTENSION_EMOJI))
6c8280
         panel_type = PANEL_TYPE_EXTENSION_EMOJI;
6c8280
 
6c8280
-    if (panel_type != PANEL_TYPE_NONE) {
6c8280
+    do {
6c8280
+        if (panel_type == PANEL_TYPE_NONE)
6c8280
+            break;
6c8280
         if (g_strcmp0 (new_name, "") != 0) {
6c8280
             /* a Panel process is started. */
6c8280
             BusConnection *connection;
6c8280
             BusInputContext *context = NULL;
6c8280
             BusPanelProxy   **panel = (panel_type == PANEL_TYPE_PANEL) ?
6c8280
                                       &ibus->panel : &ibus->emoji_extension;
6c8280
+            GDBusConnection *dbus_connection = NULL;
6c8280
 
6c8280
             if (*panel != NULL) {
6c8280
                 ibus_proxy_destroy ((IBusProxy *)(*panel));
6c8280
@@ -499,9 +502,21 @@ _dbus_name_owner_changed_cb (BusDBusImpl   *dbus,
6c8280
                 g_assert (*panel == NULL);
6c8280
             }
6c8280
 
6c8280
-            connection = bus_dbus_impl_get_connection_by_name (BUS_DEFAULT_DBUS, new_name);
6c8280
+            connection = bus_dbus_impl_get_connection_by_name (BUS_DEFAULT_DBUS,
6c8280
+                                                               new_name);
6c8280
             g_return_if_fail (connection != NULL);
6c8280
 
6c8280
+            dbus_connection = bus_connection_get_dbus_connection (connection);
6c8280
+            /* rhbz#1349148 rhbz#1385349
6c8280
+             * Avoid SEGV of BUS_IS_PANEL_PROXY (ibus->panel)
6c8280
+             * This function is called during destroying the connection
6c8280
+             * in this case? */
6c8280
+            if (dbus_connection == NULL ||
6c8280
+                g_dbus_connection_is_closed (dbus_connection)) {
6c8280
+                new_name = "";
6c8280
+                break;
6c8280
+            }
6c8280
+
6c8280
             *panel = bus_panel_proxy_new (connection, panel_type);
6c8280
             if (panel_type == PANEL_TYPE_EXTENSION_EMOJI)
6c8280
                 ibus->enable_emoji_extension = FALSE;
6c8280
@@ -555,7 +570,7 @@ _dbus_name_owner_changed_cb (BusDBusImpl   *dbus,
6c8280
                 }
6c8280
             }
6c8280
         }
6c8280
-    }
6c8280
+    } while (0);
6c8280
 
6c8280
     bus_ibus_impl_component_name_owner_changed (ibus, name, old_name, new_name);
6c8280
 }
6c8280
-- 
6c8280
2.17.1
6c8280