[Spice-devel] [spice-gtk] Make sure default SpiceSession::shared-dir is not NULL

Christophe Fergeau cfergeau at redhat.com
Fri Jan 23 06:01:06 PST 2015


Calling spice_session_set_shared_dir() with a NULL argument will trigger
a runtime warning. g_get_user_special_dir(G_USER_DIRECTORY_PUBLIC_SHARE)
will return NULL if (for example) ~/.config/user-dirs.dirs is missing,
and this is the default value for the "shared-dir" property which is set
upon SpiceSession construction. This means we could end up calling
spice_session_set_shared_dir() with a NULL parameter when instantiating
a SpiceSession instance.
---
 gtk/spice-session.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/gtk/spice-session.c b/gtk/spice-session.c
index 169294d..86891ee 100644
--- a/gtk/spice-session.c
+++ b/gtk/spice-session.c
@@ -287,6 +287,18 @@ session_disconnect(SpiceSession *self, gboolean keep_main)
     spice_session_abort_migration(self);
 }
 
+static char *
+spice_session_get_default_share_dir(void)
+{
+    const char *xdg_shared_dir;
+    xdg_shared_dir = g_get_user_special_dir(G_USER_DIRECTORY_PUBLIC_SHARE);
+    if (xdg_shared_dir != NULL) {
+        return g_strdup(xdg_shared_dir);
+    }
+
+    return g_build_filename(g_get_home_dir(), "Public", NULL);
+}
+
 static void
 spice_session_dispose(GObject *gobject)
 {
@@ -754,6 +766,7 @@ static void spice_session_set_property(GObject      *gobject,
 static void spice_session_class_init(SpiceSessionClass *klass)
 {
     GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+    char *default_share_dir;
 
     _wocky_http_proxy_get_type();
     _wocky_https_proxy_get_type();
@@ -1310,15 +1323,17 @@ static void spice_session_class_init(SpiceSessionClass *klass)
      *
      * Since: 0.24
      **/
+    default_share_dir = spice_session_get_default_share_dir();
     g_object_class_install_property
         (gobject_class, PROP_SHARED_DIR,
          g_param_spec_string("shared-dir",
                              "Shared directory",
                              "Shared directory",
-                             g_get_user_special_dir(G_USER_DIRECTORY_PUBLIC_SHARE),
+                             default_share_dir,
                              G_PARAM_READWRITE |
                              G_PARAM_CONSTRUCT |
                              G_PARAM_STATIC_STRINGS));
+    g_free(default_share_dir);
 
     g_type_class_add_private(klass, sizeof(SpiceSessionPrivate));
 }
-- 
2.1.0



More information about the Spice-devel mailing list