d8723bd
From 023d50db40912e4f7ee333543e05995a9c730bae Mon Sep 17 00:00:00 2001
d448651
From: fujiwarat <takao.fujiwara1@gmail.com>
d8723bd
Date: Fri, 15 May 2020 21:44:16 +0900
0e0ffba
Subject: [PATCH] Fix SEGV in bus_panel_proxy_focus_in()
0e0ffba
0e0ffba
rhbz#1350291 SEGV in BUS_IS_CONNECTION(skip_connection) in
0e0ffba
bus_dbus_impl_dispatch_message_by_rule()
0e0ffba
check if dbus_connection is closed in bus_dbus_impl_connection_filter_cb().
0e0ffba
c9bb32a
rhbz#1767976 SEGV in assert(connection != NULL) in
c9bb32a
bus_dbus_impl_connection_filter_cb()
c9bb32a
call bus_connection_set_filter() in bus_dbus_impl_destroy().
0e0ffba
c9bb32a
rhbz#1601577 rhbz#1797726 SEGV in ibus_engine_desc_get_layout() in
0e0ffba
bus_engine_proxy_new_internal()
0e0ffba
WIP: Added a GError to get the error message to check why the SEGV happened.
0e0ffba
0e0ffba
rhbz#1663528 SEGV in g_mutex_clear() in bus_dbus_impl_destroy()
0e0ffba
If the mutex is not unlocked, g_mutex_clear() causes assert.
d448651
8884c34
rhbz#1767691 SEGV in client/x11/main.c:_sighandler().
8884c34
Do not call atexit functions in _sighandler().
8884c34
8884c34
rhbz#1795499 SEGV in ibus_bus_get_bus_address() because of no _bus->priv.
8884c34
_changed_cb() should not be called after ibus_bus_destroy() is called.
8884c34
c9bb32a
rhbz#1771238 SEGV in assert(m_loop == null) in switcher.vala.
c9bb32a
Grabbing keyboard could be failed and switcher received the keyboard
c9bb32a
events and m_loop was not released.
c9bb32a
c9bb32a
rhbz#1797120 SEGV in assert(bus.is_connected()) in panel_binding_construct()
c9bb32a
Check m_ibus in extension.vala:bus_name_acquired_cb()
c9bb32a
d448651
BUG=rhbz#1350291
d165013
BUG=rhbz#1601577
0e0ffba
BUG=rhbz#1663528
8884c34
BUG=rhbz#1767691
8884c34
BUG=rhbz#1795499
c9bb32a
BUG=rhbz#1771238
c9bb32a
BUG=rhbz#1767976
c9bb32a
BUG=rhbz#1797120
d448651
---
c9bb32a
 bus/dbusimpl.c         | 47 ++++++++++++++++++++++++---
d8723bd
 bus/engineproxy.c      | 51 ++++++++++++++++++++++-------
c9bb32a
 client/x11/main.c      |  8 ++++-
c9bb32a
 src/ibusbus.c          |  5 +++
c9bb32a
 ui/gtk3/extension.vala |  4 +++
c9bb32a
 ui/gtk3/switcher.vala  | 73 +++++++++++++++++++++++++-----------------
d8723bd
 6 files changed, 141 insertions(+), 47 deletions(-)
d448651
d448651
diff --git a/bus/dbusimpl.c b/bus/dbusimpl.c
c9bb32a
index 59787a80..af2fbde2 100644
d448651
--- a/bus/dbusimpl.c
d448651
+++ b/bus/dbusimpl.c
c9bb32a
@@ -610,6 +610,7 @@ static void
769eef0
 bus_dbus_impl_destroy (BusDBusImpl *dbus)
769eef0
 {
769eef0
     GList *p;
769eef0
+    int i;
0e0ffba
 
769eef0
     for (p = dbus->objects; p != NULL; p = p->next) {
769eef0
         IBusService *object = (IBusService *) p->data;
c9bb32a
@@ -633,6 +634,10 @@ bus_dbus_impl_destroy (BusDBusImpl *dbus)
c9bb32a
 
c9bb32a
     for (p = dbus->connections; p != NULL; p = p->next) {
c9bb32a
         BusConnection *connection = BUS_CONNECTION (p->data);
c9bb32a
+        /* rhbz#1767976 Fix connection == NULL in
c9bb32a
+         * bus_dbus_impl_connection_filter_cb()
c9bb32a
+         */
c9bb32a
+        bus_connection_set_filter (connection, NULL, NULL, NULL);
c9bb32a
         g_signal_handlers_disconnect_by_func (connection,
c9bb32a
                 bus_dbus_impl_connection_destroy_cb, dbus);
c9bb32a
         ibus_object_destroy (IBUS_OBJECT (connection));
c9bb32a
@@ -647,12 +652,39 @@ bus_dbus_impl_destroy (BusDBusImpl *dbus)
769eef0
     dbus->unique_names = NULL;
769eef0
     dbus->names = NULL;
0e0ffba
 
769eef0
+    for (i = 0; g_idle_remove_by_data (dbus); i++) {
769eef0
+        if (i > 1000) {
769eef0
+            g_warning ("Too many idle threads were generated by " \
769eef0
+                       "bus_dbus_impl_forward_message_idle_cb and " \
769eef0
+                       "bus_dbus_impl_dispatch_message_by_rule_idle_cb");
769eef0
+            break;
769eef0
+        }
769eef0
+    }
769eef0
     g_list_free_full (dbus->start_service_calls,
0e0ffba
                       (GDestroyNotify) bus_method_call_free);
0e0ffba
     dbus->start_service_calls = NULL;
0e0ffba
 
0e0ffba
-    g_mutex_clear (&dbus->dispatch_lock);
0e0ffba
-    g_mutex_clear (&dbus->forward_lock);
769eef0
+   /* rhbz#1663528 Call g_mutex_trylock() before g_mutex_clear()
769eef0
+    * because if the mutex is not unlocked, g_mutex_clear() causes assert.
769eef0
+    */
769eef0
+#define BUS_DBUS_MUTEX_SAFE_CLEAR(mtex) {                               \
769eef0
+    int count = 0;                                                      \
769eef0
+    while (!g_mutex_trylock ((mtex))) {                                 \
769eef0
+        g_usleep (1);                                                   \
769eef0
+        if (count > 60) {                                               \
769eef0
+            g_warning (#mtex " is dead lock");                          \
769eef0
+            break;                                                      \
769eef0
+        }                                                               \
769eef0
+        ++count;                                                        \
769eef0
+    }                                                                   \
769eef0
+    g_mutex_unlock ((mtex));                                            \
769eef0
+    g_mutex_clear ((mtex));                                             \
769eef0
+}
0e0ffba
+
769eef0
+    BUS_DBUS_MUTEX_SAFE_CLEAR (&dbus->dispatch_lock);
769eef0
+    BUS_DBUS_MUTEX_SAFE_CLEAR (&dbus->forward_lock);
769eef0
+
769eef0
+#undef BUS_DBUS_MUTEX_SAFE_CLEAR
0e0ffba
 
0e0ffba
     /* FIXME destruct _lock and _queue members. */
0e0ffba
     IBUS_OBJECT_CLASS(bus_dbus_impl_parent_class)->destroy ((IBusObject *) dbus);
c9bb32a
@@ -1483,13 +1515,20 @@ bus_dbus_impl_connection_filter_cb (GDBusConnection *dbus_connection,
d448651
                                     gboolean         incoming,
d448651
                                     gpointer         user_data)
d448651
 {
d448651
+    BusDBusImpl *dbus;
d448651
+    BusConnection *connection;
d448651
+
d448651
     g_assert (G_IS_DBUS_CONNECTION (dbus_connection));
d448651
     g_assert (G_IS_DBUS_MESSAGE (message));
d448651
     g_assert (BUS_IS_DBUS_IMPL (user_data));
d448651
 
d448651
-    BusDBusImpl *dbus = (BusDBusImpl *) user_data;
d448651
-    BusConnection *connection = bus_connection_lookup (dbus_connection);
d448651
+    if (g_dbus_connection_is_closed (dbus_connection))
d448651
+        return NULL;
d448651
+
d448651
+    dbus = (BusDBusImpl *) user_data;
d448651
+    connection = bus_connection_lookup (dbus_connection);
d448651
     g_assert (connection != NULL);
d448651
+    g_assert (BUS_IS_CONNECTION (connection));
d448651
 
d448651
     if (incoming) {
d448651
         /* is incoming message */
d165013
diff --git a/bus/engineproxy.c b/bus/engineproxy.c
d8723bd
index 2d98995c..bbbe5532 100644
d165013
--- a/bus/engineproxy.c
d165013
+++ b/bus/engineproxy.c
d8723bd
@@ -660,20 +660,33 @@ bus_engine_proxy_g_signal (GDBusProxy  *proxy,
c9bb32a
     g_return_if_reached ();
c9bb32a
 }
c9bb32a
 
c9bb32a
+#pragma GCC optimize ("O0")
c9bb32a
 static BusEngineProxy *
c9bb32a
 bus_engine_proxy_new_internal (const gchar     *path,
d165013
                                IBusEngineDesc  *desc,
d8723bd
-                               GDBusConnection *connection)
d8723bd
+                               GDBusConnection *connection,
d8723bd
+                               GError         **error)
d165013
 {
d8723bd
+    GDBusProxyFlags flags;
d8723bd
+    BusEngineProxy *engine;
d8723bd
+
d165013
     g_assert (path);
d165013
     g_assert (IBUS_IS_ENGINE_DESC (desc));
d165013
     g_assert (G_IS_DBUS_CONNECTION (connection));
d8723bd
+    g_assert (error && *error == NULL);
d8723bd
 
d8723bd
-    GDBusProxyFlags flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
d8723bd
-    BusEngineProxy *engine =
d8723bd
+    /* rhbz#1601577 engine == NULL if connection is closed. */
d8723bd
+    if (g_dbus_connection_is_closed (connection)) {
d8723bd
+        *error = g_error_new (G_DBUS_ERROR,
d8723bd
+                              G_DBUS_ERROR_FAILED,
d8723bd
+                              "Connection is closed.");
d8723bd
+        return NULL;
d8723bd
+    }
d8723bd
+    flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
d8723bd
+    engine =
d165013
         (BusEngineProxy *) g_initable_new (BUS_TYPE_ENGINE_PROXY,
d165013
                                            NULL,
d165013
-                                           NULL,
d8723bd
+                                           error,
d165013
                                            "desc",              desc,
d165013
                                            "g-connection",      connection,
d165013
                                            "g-interface-name",  IBUS_INTERFACE_ENGINE,
d8723bd
@@ -681,12 +694,19 @@ bus_engine_proxy_new_internal (const gchar     *path,
d165013
                                            "g-default-timeout", g_gdbus_timeout,
d165013
                                            "g-flags",           flags,
d165013
                                            NULL);
d165013
+    /* FIXME: rhbz#1601577 */
d8723bd
+    if (!engine) {
0e0ffba
+        /* show abrt local variable */
d8723bd
+        gchar *message = g_strdup ((*error)->message);
0e0ffba
+        g_error ("%s", message);
0e0ffba
+    }
d165013
     const gchar *layout = ibus_engine_desc_get_layout (desc);
d165013
     if (layout != NULL && layout[0] != '\0') {
d165013
         engine->keymap = ibus_keymap_get (layout);
c9bb32a
     }
c9bb32a
     return engine;
d448651
 }
c9bb32a
+#pragma GCC reset_options
c9bb32a
 
c9bb32a
 typedef struct {
c9bb32a
     GTask           *task;
d8723bd
@@ -748,23 +768,30 @@ create_engine_ready_cb (BusFactoryProxy    *factory,
d8723bd
                         GAsyncResult       *res,
d8723bd
                         EngineProxyNewData *data)
d8723bd
 {
d8723bd
+    GError *error = NULL;
d8723bd
+    gchar *path;
d8723bd
+    BusEngineProxy *engine;
d8723bd
+
d8723bd
     g_return_if_fail (data->task != NULL);
d8723bd
 
d8723bd
-    GError *error = NULL;
d8723bd
-    gchar *path = bus_factory_proxy_create_engine_finish (factory,
d8723bd
-                                                          res,
d8723bd
-                                                          &error);
d8723bd
+    path = bus_factory_proxy_create_engine_finish (factory, res, &error);
d8723bd
     if (path == NULL) {
d8723bd
         g_task_return_error (data->task, error);
d8723bd
         engine_proxy_new_data_free (data);
d8723bd
         return;
d8723bd
     }
d8723bd
 
d8723bd
-    BusEngineProxy *engine =
d8723bd
-            bus_engine_proxy_new_internal (path,
d8723bd
-                                           data->desc,
d8723bd
-                                           g_dbus_proxy_get_connection ((GDBusProxy *)data->factory));
d8723bd
+    engine = bus_engine_proxy_new_internal (
d8723bd
+            path,
d8723bd
+            data->desc,
d8723bd
+            g_dbus_proxy_get_connection ((GDBusProxy *)data->factory),
d8723bd
+            &error);
d8723bd
     g_free (path);
d8723bd
+    if (!engine) {
d8723bd
+        g_task_return_error (data->task, error);
d8723bd
+        engine_proxy_new_data_free (data);
d8723bd
+        return;
d8723bd
+    }
d8723bd
 
d8723bd
     /* FIXME: set destroy callback ? */
d8723bd
     g_task_return_pointer (data->task, engine, NULL);
8884c34
diff --git a/client/x11/main.c b/client/x11/main.c
8884c34
index c9ee174d..768b91f0 100644
8884c34
--- a/client/x11/main.c
8884c34
+++ b/client/x11/main.c
8884c34
@@ -40,6 +40,7 @@
8884c34
 #include <iconv.h>
8884c34
 #include <signal.h>
8884c34
 #include <stdlib.h>
8884c34
+#include <unistd.h>
8884c34
 
8884c34
 #include <getopt.h>
8884c34
 
8884c34
@@ -1104,7 +1105,12 @@ _atexit_cb ()
8884c34
 static void
8884c34
 _sighandler (int sig)
8884c34
 {
8884c34
-    exit(EXIT_FAILURE);
8884c34
+    /* rhbz#1767691 _sighandler() is called with SIGTERM
8884c34
+     * and exit() causes SEGV during calling atexit functions.
8884c34
+     * _atexit_cb() might be broken. _exit() does not call
8884c34
+     * atexit functions.
8884c34
+     */
8884c34
+    _exit(EXIT_FAILURE);
8884c34
 }
8884c34
 
8884c34
 static void
8884c34
diff --git a/src/ibusbus.c b/src/ibusbus.c
8884c34
index b7ffbb47..668c8a26 100644
8884c34
--- a/src/ibusbus.c
8884c34
+++ b/src/ibusbus.c
8884c34
@@ -689,6 +689,11 @@ ibus_bus_destroy (IBusObject *object)
8884c34
     _bus = NULL;
8884c34
 
8884c34
     if (bus->priv->monitor) {
8884c34
+        /* rhbz#1795499 _changed_cb() causes SEGV because of no bus->priv
8884c34
+         * after ibus_bus_destroy() is called.
8884c34
+         */
8884c34
+        g_signal_handlers_disconnect_by_func (bus->priv->monitor,
8884c34
+                                              (GCallback) _changed_cb, bus);
8884c34
         g_object_unref (bus->priv->monitor);
8884c34
         bus->priv->monitor = NULL;
8884c34
     }
c9bb32a
diff --git a/ui/gtk3/extension.vala b/ui/gtk3/extension.vala
c9bb32a
index ea3cd464..57ed1357 100644
c9bb32a
--- a/ui/gtk3/extension.vala
c9bb32a
+++ b/ui/gtk3/extension.vala
c9bb32a
@@ -73,6 +73,10 @@ class ExtensionGtk : Gtk.Application {
c9bb32a
                                       string signal_name,
c9bb32a
                                       Variant parameters) {
c9bb32a
         debug("signal_name = %s", signal_name);
c9bb32a
+        /* rhbz#1797120 Fix assert(bus.is_connected()) in
c9bb32a
+         * panel_binding_construct()
c9bb32a
+         */
c9bb32a
+        return_if_fail(m_bus.is_connected());
c9bb32a
         m_panel = new PanelBinding(m_bus, this);
c9bb32a
         m_panel.load_settings();
c9bb32a
     }
c9bb32a
diff --git a/ui/gtk3/switcher.vala b/ui/gtk3/switcher.vala
c9bb32a
index a4529c88..29a70dd5 100644
c9bb32a
--- a/ui/gtk3/switcher.vala
c9bb32a
+++ b/ui/gtk3/switcher.vala
c9bb32a
@@ -140,8 +140,8 @@ class Switcher : Gtk.Window {
c9bb32a
                    IBus.EngineDesc[] engines,
c9bb32a
                    int               index,
c9bb32a
                    string            input_context_path) {
c9bb32a
-        assert (m_loop == null);
c9bb32a
-        assert (index < engines.length);
c9bb32a
+        assert(m_loop == null);
c9bb32a
+        assert(index < engines.length);
c9bb32a
 
c9bb32a
         m_is_running = true;
c9bb32a
         m_keyval = keyval;
c9bb32a
@@ -198,16 +198,18 @@ class Switcher : Gtk.Window {
c9bb32a
                            null,
c9bb32a
                            event,
c9bb32a
                            null);
c9bb32a
-        if (status != Gdk.GrabStatus.SUCCESS)
c9bb32a
+        if (status != Gdk.GrabStatus.SUCCESS) {
c9bb32a
             warning("Grab keyboard failed! status = %d", status);
c9bb32a
-        status = seat.grab(get_window(),
c9bb32a
-                           Gdk.SeatCapabilities.POINTER,
c9bb32a
-                           true,
c9bb32a
-                           null,
c9bb32a
-                           event,
c9bb32a
-                           null);
c9bb32a
-        if (status != Gdk.GrabStatus.SUCCESS)
c9bb32a
-            warning("Grab pointer failed! status = %d", status);
c9bb32a
+        } else {
c9bb32a
+            status = seat.grab(get_window(),
c9bb32a
+                               Gdk.SeatCapabilities.POINTER,
c9bb32a
+                               true,
c9bb32a
+                               null,
c9bb32a
+                               event,
c9bb32a
+                               null);
c9bb32a
+            if (status != Gdk.GrabStatus.SUCCESS)
c9bb32a
+                warning("Grab pointer failed! status = %d", status);
c9bb32a
+        }
c9bb32a
 #else
c9bb32a
         Gdk.Device device = event.get_device();
c9bb32a
         if (device == null) {
c9bb32a
@@ -243,30 +245,41 @@ class Switcher : Gtk.Window {
c9bb32a
                                Gdk.EventMask.KEY_RELEASE_MASK,
c9bb32a
                                null,
c9bb32a
                                Gdk.CURRENT_TIME);
c9bb32a
-        if (status != Gdk.GrabStatus.SUCCESS)
c9bb32a
+        if (status != Gdk.GrabStatus.SUCCESS) {
c9bb32a
             warning("Grab keyboard failed! status = %d", status);
c9bb32a
-        // Grab all pointer events
c9bb32a
-        status = pointer.grab(get_window(),
c9bb32a
-                              Gdk.GrabOwnership.NONE,
c9bb32a
-                              true,
c9bb32a
-                              Gdk.EventMask.BUTTON_PRESS_MASK |
c9bb32a
-                              Gdk.EventMask.BUTTON_RELEASE_MASK,
c9bb32a
-                              null,
c9bb32a
-                              Gdk.CURRENT_TIME);
c9bb32a
-        if (status != Gdk.GrabStatus.SUCCESS)
c9bb32a
-            warning("Grab pointer failed! status = %d", status);
c9bb32a
+        } else {
c9bb32a
+            // Grab all pointer events
c9bb32a
+            status = pointer.grab(get_window(),
c9bb32a
+                                  Gdk.GrabOwnership.NONE,
c9bb32a
+                                  true,
c9bb32a
+                                  Gdk.EventMask.BUTTON_PRESS_MASK |
c9bb32a
+                                  Gdk.EventMask.BUTTON_RELEASE_MASK,
c9bb32a
+                                  null,
c9bb32a
+                                  Gdk.CURRENT_TIME);
c9bb32a
+            if (status != Gdk.GrabStatus.SUCCESS)
c9bb32a
+                warning("Grab pointer failed! status = %d", status);
c9bb32a
+        }
c9bb32a
 #endif
c9bb32a
 
c9bb32a
-        // Probably we can delete m_popup_delay_time in 1.6
c9bb32a
-        pointer.get_position_double(null,
c9bb32a
-                                    out m_mouse_init_x,
c9bb32a
-                                    out m_mouse_init_y);
c9bb32a
-        m_mouse_moved = false;
c9bb32a
+        /* Fix RHBZ #1771238 assert(m_loop == null)
c9bb32a
+         * Grabbing keyboard can be failed when the second Super-e is typed
c9bb32a
+         * before Switcher dialog is focused. And m_loop could not be released
c9bb32a
+         * if the failed Super-e would call m_loop.run() below and could not
c9bb32a
+         * call key_release_event(). And m_loop == null would be false in the
c9bb32a
+         * third Super-e.
c9bb32a
+         */
c9bb32a
+        if (status == Gdk.GrabStatus.SUCCESS) {
c9bb32a
+            // Probably we can delete m_popup_delay_time in 1.6
c9bb32a
+            pointer.get_position_double(null,
c9bb32a
+                                        out m_mouse_init_x,
c9bb32a
+                                        out m_mouse_init_y);
c9bb32a
+            m_mouse_moved = false;
c9bb32a
 
c9bb32a
 
c9bb32a
-        m_loop = new GLib.MainLoop();
c9bb32a
-        m_loop.run();
c9bb32a
-        m_loop = null;
c9bb32a
+            m_loop = new GLib.MainLoop();
c9bb32a
+            m_loop.run();
c9bb32a
+            m_loop = null;
c9bb32a
+        }
c9bb32a
 
c9bb32a
 #if VALA_0_34
c9bb32a
         seat.ungrab();
d448651
-- 
8884c34
2.24.1
d448651