Blame SOURCES/ibus-1385349-segv-bus-proxy.patch

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