Add NULL check to fix crash when receiving a dbus reply after cleanup
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / bridge.c
index 2df9889..b016da6 100644 (file)
@@ -88,6 +88,9 @@ tally_event_reply ()
 {
   static int replies_received = 0;
 
+  if (!spi_global_app_data)
+    return;
+
   replies_received++;
   if (replies_received == 3)
   {
@@ -262,7 +265,8 @@ register_reply (DBusPendingCall *pending, void *user_data)
     }
   dbus_message_unref (reply);
 
-  get_registered_event_listeners (spi_global_app_data);
+  if (!spi_global_app_data->events_initialized)
+    get_registered_event_listeners (spi_global_app_data);
 }
 
 static gboolean
@@ -275,6 +279,9 @@ register_application (SpiBridge * app)
 
   dbus_error_init (&error);
 
+  g_free (app->desktop_name);
+  g_free (app->desktop_path);
+
   /* These will be overridden when we get a reply, but in practice these
      defaults should always be correct */
   app->desktop_name = g_strdup (ATSPI_DBUS_NAME_REGISTRY);
@@ -434,6 +441,8 @@ socket_ref_state_set (AtkObject *accessible)
 static void
 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
 {
+  g_return_if_fail (spi_global_register != NULL);
+
   AtkObject *accessible = ATK_OBJECT(socket);
   gchar *plug_name, *plug_path;
   AtkObjectClass *klass;
@@ -649,6 +658,7 @@ signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
   const char *interface = dbus_message_get_interface (message);
   const char *member = dbus_message_get_member (message);
   DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+  static gboolean registry_lost = FALSE;
 
   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
@@ -678,14 +688,23 @@ signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
       !g_strcmp0(member, "NameOwnerChanged"))
     {
       char *name, *old, *new;
-      result = DBUS_HANDLER_RESULT_HANDLED;
       if (dbus_message_get_args (message, NULL,
                                  DBUS_TYPE_STRING, &name,
                                  DBUS_TYPE_STRING, &old,
                                  DBUS_TYPE_STRING, &new,
                                  DBUS_TYPE_INVALID))
         {
-          if (*old != '\0' && *new == '\0')
+          if (!strcmp (name, "org.a11y.atspi.Registry"))
+            {
+              if (registry_lost && !old[0])
+                {
+                  register_application (spi_global_app_data);
+                  registry_lost = FALSE;
+                }
+              else if (!new[0])
+                registry_lost = TRUE;
+            }
+          else if (*old != '\0' && *new == '\0')
               spi_atk_remove_client (old);
         }
     }
@@ -920,6 +939,7 @@ atk_bridge_adaptor_init (gint * argc, gchar ** argv[])
   /* Set up filter and match rules to catch signals */
   dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.Registry', sender='org.a11y.atspi.Registry'", NULL);
   dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.DeviceEventListener', sender='org.a11y.atspi.Registry'", NULL);
+  dbus_bus_add_match (spi_global_app_data->bus, "type='signal', arg0='org.a11y.atspi.Registry', interface='org.freedesktop.DBus', member='NameOwnerChanged'", NULL);
   dbus_connection_add_filter (spi_global_app_data->bus, signal_filter, NULL,
                               NULL);
 
@@ -938,7 +958,8 @@ atk_bridge_adaptor_cleanup (void)
   GList *l;
   GSList *ls;
 
-  g_return_if_fail (inited);
+  if (!inited)
+    return;
 
   if (!spi_global_app_data)
       return;