GDBus: Properly handle empty address strings
authorDavid Zeuthen <davidz@redhat.com>
Tue, 6 Jul 2010 17:56:35 +0000 (13:56 -0400)
committerDavid Zeuthen <davidz@redhat.com>
Tue, 6 Jul 2010 21:03:36 +0000 (17:03 -0400)
Changes this error

 DBUS_SESSION_BUS_ADDRESS= \
 gdbus introspect --session \
                  --dest org.freedesktop.DBus \
                  --object-path /org/freedesktop/DBus
 **
 GLib-GIO:ERROR:gdbusaddress.c:913:g_dbus_address_get_stream_sync: assertion failed: (last_error != NULL)
 Aborted (core dumped)

to

 DBUS_SESSION_BUS_ADDRESS= \
 gdbus introspect --session \
                  --dest org.freedesktop.DBus \
                  --object-path /org/freedesktop/DBus
 Error connecting: The given address is empty

which is much more preferable.

Signed-off-by: David Zeuthen <davidz@redhat.com>
gio/gdbusaddress.c
gio/tests/gdbus-addresses.c

index 906e038..21c5203 100644 (file)
@@ -83,6 +83,9 @@ g_dbus_is_address (const gchar *string)
   g_return_val_if_fail (string != NULL, FALSE);
 
   a = g_strsplit (string, ";", 0);
+  if (a[0] == NULL)
+    goto out;
+
   for (n = 0; a[n] != NULL; n++)
     {
       if (!_g_dbus_address_parse_entry (a[n],
@@ -879,11 +882,19 @@ g_dbus_address_get_stream_sync (const gchar   *address,
   last_error = NULL;
 
   addr_array = g_strsplit (address, ";", 0);
-  last_error = NULL;
+  if (addr_array[0] == NULL)
+    {
+      last_error = g_error_new_literal (G_IO_ERROR,
+                                        G_IO_ERROR_INVALID_ARGUMENT,
+                                        _("The given address is empty"));
+      goto out;
+    }
+
   for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
     {
       const gchar *addr = addr_array[n];
       GError *this_error;
+
       this_error = NULL;
       ret = g_dbus_address_try_connect_one (addr,
                                             out_guid,
index 568c775..dad986f 100644 (file)
 
 /* ---------------------------------------------------------------------------------------------------- */
 
+static void
+test_empty_address (void)
+{
+  GError *error;
+  error = NULL;
+  g_dbus_address_get_stream_sync ("",
+                                  NULL,
+                                  NULL,
+                                  &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_error_free (error);
+}
+
 #ifdef G_OS_UNIX
 static void
 test_unix_address (void)
@@ -68,6 +81,7 @@ main (int   argc,
   g_type_init ();
   g_test_init (&argc, &argv, NULL);
 
+  g_test_add_func ("/gdbus/empty-address", test_empty_address);
 #ifdef G_OS_UNIX
   g_test_add_func ("/gdbus/unix-address", test_unix_address);
 #endif