diff options
author | Robert Ancell <robert.ancell@canonical.com> | 2017-09-27 17:35:55 -0400 |
---|---|---|
committer | Robert Ancell <robert.ancell@canonical.com> | 2017-09-27 17:35:55 -0400 |
commit | 90c4278a35e4666e7c830fedc8826cd332a1753b (patch) | |
tree | f6625149b7e8da1573cfab7ed46c0a99523216eb | |
parent | 2b7eaa44d401a0a187917e53385d079e688f5172 (diff) | |
download | lightdm-git-90c4278a35e4666e7c830fedc8826cd332a1753b.tar.gz |
Use g_auto to automatically free memory
64 files changed, 1294 insertions, 2016 deletions
diff --git a/common/configuration.c b/common/configuration.c index 27416cd0..961871a9 100644 --- a/common/configuration.c +++ b/common/configuration.c @@ -47,16 +47,14 @@ config_get_instance (void) gboolean config_load_from_file (Configuration *config, const gchar *path, GList **messages, GError **error) { - GKeyFile *key_file; - gchar *source_path, **groups; + g_autoptr(GKeyFile) key_file = NULL; + gchar *source_path; + g_auto(GStrv) groups = NULL; int i; key_file = g_key_file_new (); if (!g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, error)) - { - g_key_file_free (key_file); return FALSE; - } source_path = g_strdup (path); config->priv->sources = g_list_append (config->priv->sources, source_path); @@ -64,7 +62,8 @@ config_load_from_file (Configuration *config, const gchar *path, GList **message groups = g_key_file_get_groups (key_file, NULL); for (i = 0; groups[i]; i++) { - gchar **keys, *group; + g_auto(GStrv) keys = NULL; + const gchar *group; GHashTable *known_keys = NULL; int j; @@ -95,7 +94,8 @@ config_load_from_file (Configuration *config, const gchar *path, GList **message for (j = 0; keys[j]; j++) { - gchar *value, *k; + g_autofree gchar *value = NULL; + g_autofree gchar *k = NULL; if (known_keys != NULL) { @@ -114,17 +114,11 @@ config_load_from_file (Configuration *config, const gchar *path, GList **message value = g_key_file_get_value (key_file, groups[i], keys[j], NULL); g_key_file_set_value (config->priv->key_file, group, keys[j], value); - g_free (value); k = g_strdup_printf ("%s]%s", group, keys[j]); - g_hash_table_insert (config->priv->key_sources, k, source_path); + g_hash_table_insert (config->priv->key_sources, g_steal_pointer (&k), source_path); } - - g_strfreev (keys); } - g_strfreev (groups); - - g_key_file_free (key_file); return TRUE; } @@ -132,7 +126,7 @@ config_load_from_file (Configuration *config, const gchar *path, GList **message static gchar * path_make_absolute (gchar *path) { - gchar *cwd, *abs_path; + g_autofree gchar *cwd = NULL; if (!path) return NULL; @@ -141,10 +135,7 @@ path_make_absolute (gchar *path) return path; cwd = g_get_current_dir (); - abs_path = g_build_filename (cwd, path, NULL); - g_free (path); - - return abs_path; + return g_build_filename (cwd, path, NULL); } static int @@ -158,13 +149,12 @@ load_config_directory (const gchar *path, GList **messages) { GDir *dir; GList *files = NULL, *link; - GError *error = NULL; + g_autoptr(GError) error = NULL; /* Find configuration files */ dir = g_dir_open (path, 0, &error); if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) g_printerr ("Failed to open configuration directory %s: %s\n", path, error->message); - g_clear_error (&error); if (dir) { const gchar *name; @@ -178,21 +168,20 @@ load_config_directory (const gchar *path, GList **messages) for (link = files; link; link = link->next) { gchar *filename = link->data; - gchar *conf_path; + g_autofree gchar *conf_path = NULL; + g_autoptr(GError) conf_error = NULL; conf_path = g_build_filename (path, filename, NULL); if (g_str_has_suffix (filename, ".conf")) { if (messages) *messages = g_list_append (*messages, g_strdup_printf ("Loading configuration from %s", conf_path)); - config_load_from_file (config_get_instance (), conf_path, messages, &error); - if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) - g_printerr ("Failed to load configuration from %s: %s\n", filename, error->message); - g_clear_error (&error); + config_load_from_file (config_get_instance (), conf_path, messages, &conf_error); + if (conf_error && !g_error_matches (conf_error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) + g_printerr ("Failed to load configuration from %s: %s\n", filename, conf_error->message); } else g_debug ("Ignoring configuration file %s, it does not have .conf suffix", conf_path); - g_free (conf_path); } g_list_free_full (files, g_free); } @@ -205,20 +194,19 @@ load_config_directories (const gchar * const *dirs, GList **messages) /* Load in reverse order, because XDG_* fields are preference-ordered and the directories in front should override directories in back. */ for (i = g_strv_length ((gchar **)dirs) - 1; i >= 0; i--) { - gchar *full_dir = g_build_filename (dirs[i], "lightdm", "lightdm.conf.d", NULL); + g_autofree gchar *full_dir = g_build_filename (dirs[i], "lightdm", "lightdm.conf.d", NULL); if (messages) *messages = g_list_append (*messages, g_strdup_printf ("Loading configuration dirs from %s", full_dir)); load_config_directory (full_dir, messages); - g_free (full_dir); } } gboolean config_load_from_standard_locations (Configuration *config, const gchar *config_path, GList **messages) { - gchar *config_d_dir = NULL, *path; - gboolean success = TRUE; - GError *error = NULL; + g_autofree gchar *config_d_dir = NULL; + g_autofree gchar *path = NULL; + g_autoptr(GError) error = NULL; g_return_val_if_fail (config->priv->dir == NULL, FALSE); @@ -227,8 +215,11 @@ config_load_from_standard_locations (Configuration *config, const gchar *config_ if (config_path) { + g_autofree gchar *basename = NULL; + path = g_strdup (config_path); - config->priv->dir = path_make_absolute (g_path_get_basename (config_path)); + basename = g_path_get_basename (config_path); + config->priv->dir = path_make_absolute (basename); } else { @@ -252,15 +243,11 @@ config_load_from_standard_locations (Configuration *config, const gchar *config_ { if (error) g_printerr ("Failed to load configuration from %s: %s\n", path, error->message); - success = FALSE; + return FALSE; } } - g_clear_error (&error); - - g_free (config_d_dir); - g_free (path); - return success; + return TRUE; } const gchar * @@ -296,12 +283,11 @@ config_get_sources (Configuration *config) const gchar * config_get_source (Configuration *config, const gchar *section, const gchar *key) { - gchar *k; + g_autofree gchar *k = NULL; const gchar *source; k = g_strdup_printf ("%s]%s", section, key); source = g_hash_table_lookup (config->priv->key_sources, k); - g_free (k); return source; } @@ -356,17 +342,13 @@ config_get_boolean (Configuration *config, const gchar *section, const gchar *ke */ /*return g_key_file_get_boolean (config->priv->key_file, section, key, NULL);*/ - gchar *value; - gboolean v; + g_autofree gchar *value = NULL; value = g_key_file_get_value (config->priv->key_file, section, key, NULL); if (!value) return FALSE; g_strchomp (value); - v = strcmp (value, "true") == 0; - g_free (value); - - return v; + return strcmp (value, "true") == 0; } static void @@ -462,8 +444,8 @@ config_finalize (GObject *object) { Configuration *self = CONFIGURATION (object); - g_free (self->priv->dir); - g_key_file_free (self->priv->key_file); + g_clear_pointer (&self->priv->dir, g_free); + g_clear_pointer (&self->priv->key_file, g_key_file_free); g_list_free_full (self->priv->sources, g_free); g_hash_table_destroy (self->priv->key_sources); g_hash_table_destroy (self->priv->lightdm_keys); diff --git a/common/dmrc.c b/common/dmrc.c index 5039887d..c7dce7c6 100644 --- a/common/dmrc.c +++ b/common/dmrc.c @@ -21,8 +21,8 @@ GKeyFile * dmrc_load (CommonUser *user) { - GKeyFile *dmrc_file; - gchar *path; + g_autoptr(GKeyFile) dmrc_file = NULL; + g_autofree gchar *path = NULL; gboolean have_dmrc, drop_privileges; dmrc_file = g_key_file_new (); @@ -38,31 +38,33 @@ dmrc_load (CommonUser *user) have_dmrc = g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_KEEP_COMMENTS, NULL); if (drop_privileges) privileges_reclaim (); - g_free (path); /* If no ~/.dmrc, then load from the cache */ if (!have_dmrc) { - gchar *filename, *cache_dir; + g_autofree gchar *cache_path = NULL; + g_autofree gchar *filename = NULL; + g_autofree gchar *cache_dir = NULL; filename = g_strdup_printf ("%s.dmrc", common_user_get_name (user)); cache_dir = config_get_string (config_get_instance (), "LightDM", "cache-directory"); - path = g_build_filename (cache_dir, "dmrc", filename, NULL); - g_free (filename); - g_free (cache_dir); + cache_path = g_build_filename (cache_dir, "dmrc", filename, NULL); - g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_KEEP_COMMENTS, NULL); - g_free (path); + g_key_file_load_from_file (dmrc_file, cache_path, G_KEY_FILE_KEEP_COMMENTS, NULL); } - return dmrc_file; + return g_steal_pointer (&dmrc_file); } void dmrc_save (GKeyFile *dmrc_file, CommonUser *user) { - gchar *path, *filename, *cache_dir, *dmrc_cache_dir; - gchar *data; + g_autofree gchar *data = NULL; + g_autofree gchar *path = NULL; + g_autofree gchar *cache_path = NULL; + g_autofree gchar *filename = NULL; + g_autofree gchar *cache_dir = NULL; + g_autofree gchar *dmrc_cache_dir = NULL; gsize length; gboolean drop_privileges; @@ -80,21 +82,13 @@ dmrc_save (GKeyFile *dmrc_file, CommonUser *user) if (drop_privileges) privileges_reclaim (); - g_free (path); - /* Update the .dmrc cache */ cache_dir = config_get_string (config_get_instance (), "LightDM", "cache-directory"); dmrc_cache_dir = g_build_filename (cache_dir, "dmrc", NULL); - g_free (cache_dir); if (g_mkdir_with_parents (dmrc_cache_dir, 0700) < 0) g_warning ("Failed to make DMRC cache directory %s: %s", dmrc_cache_dir, strerror (errno)); filename = g_strdup_printf ("%s.dmrc", common_user_get_name (user)); - path = g_build_filename (dmrc_cache_dir, filename, NULL); - g_file_set_contents (path, data, length, NULL); - - g_free (data); - g_free (path); - g_free (filename); - g_free (dmrc_cache_dir); + cache_path = g_build_filename (dmrc_cache_dir, filename, NULL); + g_file_set_contents (cache_path, data, length, NULL); } diff --git a/common/user-list.c b/common/user-list.c index 58cb0284..87c45166 100644 --- a/common/user-list.c +++ b/common/user-list.c @@ -286,7 +286,7 @@ make_passwd_user (CommonUserList *user_list, struct passwd *entry) { CommonUser *user = g_object_new (COMMON_TYPE_USER, NULL); CommonUserPrivate *priv = GET_USER_PRIVATE (user); - char **tokens; + g_auto(GStrv) tokens = NULL; gchar *real_name, *image; g_signal_connect (user, "get-logged-in", G_CALLBACK (get_logged_in_cb), user_list); @@ -296,7 +296,6 @@ make_passwd_user (CommonUserList *user_list, struct passwd *entry) real_name = g_strdup (tokens[0]); else real_name = g_strdup (""); - g_strfreev (tokens); image = g_build_filename (entry->pw_dir, ".face", NULL); if (!g_file_test (image, G_FILE_TEST_EXISTS)) @@ -325,12 +324,14 @@ static void load_passwd_file (CommonUserList *user_list, gboolean emit_add_signal) { CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list); - GKeyFile *config; - gchar *value; + g_autoptr(GKeyFile) config = NULL; + g_autofree gchar *hidden_users_list = NULL; + g_autofree gchar *hidden_shells_list = NULL; gint minimum_uid; - gchar **hidden_users, **hidden_shells; + g_auto(GStrv) hidden_users = NULL; + g_auto(GStrv) hidden_shells = NULL; GList *users = NULL, *old_users, *new_users = NULL, *changed_users = NULL, *link; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_debug ("Loading user config from %s", USER_CONFIG_FILE); @@ -338,26 +339,21 @@ load_passwd_file (CommonUserList *user_list, gboolean emit_add_signal) g_key_file_load_from_file (config, USER_CONFIG_FILE, G_KEY_FILE_NONE, &error); if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) g_warning ("Failed to load configuration from %s: %s", USER_CONFIG_FILE, error->message); - g_clear_error (&error); if (g_key_file_has_key (config, "UserList", "minimum-uid", NULL)) minimum_uid = g_key_file_get_integer (config, "UserList", "minimum-uid", NULL); else minimum_uid = 500; - value = g_key_file_get_string (config, "UserList", "hidden-users", NULL); - if (!value) - value = g_strdup ("nobody nobody4 noaccess"); - hidden_users = g_strsplit (value, " ", -1); - g_free (value); + hidden_users_list = g_key_file_get_string (config, "UserList", "hidden-users", NULL); + if (!hidden_users_list) + hidden_users_list = g_strdup ("nobody nobody4 noaccess"); + hidden_users = g_strsplit (hidden_users_list, " ", -1); - value = g_key_file_get_string (config, "UserList", "hidden-shells", NULL); - if (!value) - value = g_strdup ("/bin/false /usr/sbin/nologin"); - hidden_shells = g_strsplit (value, " ", -1); - g_free (value); - - g_key_file_free (config); + hidden_shells_list = g_key_file_get_string (config, "UserList", "hidden-shells", NULL); + if (!hidden_shells_list) + hidden_shells_list = g_strdup ("/bin/false /usr/sbin/nologin"); + hidden_shells = g_strsplit (hidden_shells_list, " ", -1); setpwent (); @@ -412,8 +408,6 @@ load_passwd_file (CommonUserList *user_list, gboolean emit_add_signal) } users = g_list_insert_sorted (users, user, compare_user); } - g_strfreev (hidden_users); - g_strfreev (hidden_shells); if (errno != 0) g_warning ("Failed to read password database: %s", strerror (errno)); @@ -499,11 +493,12 @@ static gboolean load_accounts_user (CommonUser *user) { CommonUserPrivate *priv = GET_USER_PRIVATE (user); - GVariant *result, *value; + g_autoptr(GVariant) result = NULL; + GVariant *value; GVariantIter *iter; gchar *name; gboolean system_account = FALSE; - GError *error = NULL; + g_autoptr(GError) error = NULL; /* Get the properties for this user */ if (!priv->changed_signal) @@ -530,7 +525,6 @@ load_accounts_user (CommonUser *user) &error); if (error) g_warning ("Error updating user %s: %s", priv->path, error->message); - g_clear_error (&error); if (!result) return FALSE; @@ -571,10 +565,7 @@ load_accounts_user (CommonUser *user) g_free (priv->image); priv->image = g_variant_dup_string (value, NULL); if (strcmp (priv->image, "") == 0) - { - g_free (priv->image); - priv->image = NULL; - } + g_clear_pointer (&priv->image, g_free); } else if (strcmp (name, "XSession") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) { @@ -586,10 +577,7 @@ load_accounts_user (CommonUser *user) g_free (priv->background); priv->background = g_variant_dup_string (value, NULL); if (strcmp (priv->background, "") == 0) - { - g_free (priv->background); - priv->background = NULL; - } + g_clear_pointer (&priv->background, g_free); } else if (strcmp (name, "XKeyboardLayouts") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING_ARRAY)) { @@ -608,8 +596,6 @@ load_accounts_user (CommonUser *user) } g_variant_iter_free (iter); - g_variant_unref (result); - return !system_account; } @@ -705,8 +691,9 @@ load_session (CommonUserList *user_list, const gchar *path) { CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list); CommonSession *session = NULL; - GVariant *result, *username; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GVariant) username = NULL; + g_autoptr(GError) error = NULL; result = g_dbus_connection_call_sync (priv->bus, "org.freedesktop.DisplayManager", @@ -721,7 +708,6 @@ load_session (CommonUserList *user_list, const gchar *path) &error); if (error) g_warning ("Error getting UserName from org.freedesktop.DisplayManager.Session: %s", error->message); - g_clear_error (&error); if (!result) return NULL; @@ -738,8 +724,6 @@ load_session (CommonUserList *user_list, const gchar *path) session->path = g_strdup (path); priv->sessions = g_list_append (priv->sessions, session); } - g_variant_unref (username); - g_variant_unref (result); return session; } @@ -816,8 +800,8 @@ static void load_sessions (CommonUserList *user_list) { CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list); - GVariant *result; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; priv->session_added_signal = g_dbus_connection_signal_subscribe (priv->bus, "org.freedesktop.DisplayManager", @@ -852,12 +836,11 @@ load_sessions (CommonUserList *user_list) &error); if (error) g_warning ("Error getting session list from org.freedesktop.DisplayManager: %s", error->message); - g_clear_error (&error); if (result) { if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(v)"))) { - GVariant *value; + g_autoptr(GVariant) value = NULL; GVariantIter *iter; const gchar *path; @@ -868,13 +851,9 @@ load_sessions (CommonUserList *user_list) while (g_variant_iter_loop (iter, "&o", &path)) load_session (user_list, path); g_variant_iter_free (iter); - - g_variant_unref (value); } else g_warning ("Unexpected type from org.freedesktop.DisplayManager.Sessions: %s", g_variant_get_type_string (result)); - - g_variant_unref (result); } } @@ -882,8 +861,8 @@ static void load_users (CommonUserList *user_list) { CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list); - GVariant *result; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; if (priv->have_users) return; @@ -923,7 +902,6 @@ load_users (CommonUserList *user_list) &error); if (error) g_warning ("Error getting user list from org.freedesktop.Accounts: %s", error->message); - g_clear_error (&error); if (result) { GVariantIter *iter; @@ -934,11 +912,11 @@ load_users (CommonUserList *user_list) while (g_variant_iter_loop (iter, "&o", &path)) add_accounts_user (user_list, path, FALSE); g_variant_iter_free (iter); - g_variant_unref (result); } else { - GFile *passwd_file; + g_autoptr(GFile) passwd_file = NULL; + g_autoptr(GError) e = NULL; g_dbus_connection_signal_unsubscribe (priv->bus, priv->user_added_signal); priv->user_added_signal = 0; @@ -950,13 +928,11 @@ load_users (CommonUserList *user_list) /* Watch for changes to user list */ passwd_file = g_file_new_for_path (PASSWD_FILE); - priv->passwd_monitor = g_file_monitor (passwd_file, G_FILE_MONITOR_NONE, NULL, &error); - g_object_unref (passwd_file); - if (error) - g_warning ("Error monitoring %s: %s", PASSWD_FILE, error->message); + priv->passwd_monitor = g_file_monitor (passwd_file, G_FILE_MONITOR_NONE, NULL, &e); + if (e) + g_warning ("Error monitoring %s: %s", PASSWD_FILE, e->message); else g_signal_connect (priv->passwd_monitor, "changed", G_CALLBACK (passwd_changed_cb), user_list); - g_clear_error (&error); } } @@ -1158,8 +1134,8 @@ static gboolean call_method (CommonUser *user, const gchar *method, GVariant *args, const gchar *expected, GVariant **result) { - GVariant *answer; - GError *error = NULL; + g_autoptr(GVariant) answer = NULL; + g_autoptr(GError) error = NULL; CommonUserPrivate *priv = GET_USER_PRIVATE (user); answer = g_dbus_connection_call_sync (priv->bus, @@ -1175,15 +1151,12 @@ call_method (CommonUser *user, const gchar *method, GVariant *args, &error); if (error) g_warning ("Could not call %s: %s", method, error->message); - g_clear_error (&error); if (!answer) return FALSE; if (result) - *result = answer; - else - g_variant_unref (answer); + *result = g_steal_pointer (&answer); return TRUE; } @@ -1192,13 +1165,9 @@ static void save_string_to_dmrc (CommonUser *user, const gchar *group, const gchar *key, const gchar *value) { - GKeyFile *dmrc; - - dmrc = dmrc_load (user); + g_autoptr(GKeyFile) dmrc = dmrc_load (user); g_key_file_set_string (dmrc, group, key, value); dmrc_save (dmrc, user); - - g_key_file_free (dmrc); } /* Loads language/layout/session info for user */ @@ -1206,7 +1175,7 @@ static void load_dmrc (CommonUser *user) { CommonUserPrivate *priv = GET_USER_PRIVATE (user); - GKeyFile *dmrc; + g_autoptr(GKeyFile) dmrc = NULL; /* We're using Accounts service instead */ if (priv->path) @@ -1233,8 +1202,6 @@ load_dmrc (CommonUser *user) g_free (priv->session); priv->session = g_key_file_get_string (dmrc, "Desktop", "Session", NULL); - - g_key_file_free (dmrc); } /** @@ -1612,19 +1579,19 @@ common_user_finalize (GObject *object) CommonUser *self = COMMON_USER (object); CommonUserPrivate *priv = GET_USER_PRIVATE (self); - g_free (priv->path); + g_clear_pointer (&priv->path, g_free); if (priv->changed_signal) g_dbus_connection_signal_unsubscribe (priv->bus, priv->changed_signal); g_clear_object (&priv->bus); - g_free (priv->name); - g_free (priv->real_name); - g_free (priv->home_directory); - g_free (priv->shell); - g_free (priv->image); - g_free (priv->background); - g_free (priv->language); - g_strfreev (priv->layouts); - g_free (priv->session); + g_clear_pointer (&priv->name, g_free); + g_clear_pointer (&priv->real_name, g_free); + g_clear_pointer (&priv->home_directory, g_free); + g_clear_pointer (&priv->shell, g_free); + g_clear_pointer (&priv->image, g_free); + g_clear_pointer (&priv->background, g_free); + g_clear_pointer (&priv->language, g_free); + g_clear_pointer (&priv->layouts, g_strfreev); + g_clear_pointer (&priv->session, g_free); } static void @@ -1784,8 +1751,8 @@ common_session_finalize (GObject *object) { CommonSession *self = COMMON_SESSION (object); - g_free (self->path); - g_free (self->username); + g_clear_pointer (&self->path, g_free); + g_clear_pointer (&self->username, g_free); } static void diff --git a/liblightdm-gobject/greeter.c b/liblightdm-gobject/greeter.c index 2961220a..abe4a9ca 100644 --- a/liblightdm-gobject/greeter.c +++ b/liblightdm-gobject/greeter.c @@ -533,7 +533,7 @@ send_message (LightDMGreeter *greeter, guint8 *message, gsize message_length, GE gchar *data; gsize data_length; guint32 stated_length; - GError *flush_error = NULL; + g_autoptr(GError) flush_error = NULL; if (!connect_to_daemon (greeter, error)) return FALSE; @@ -558,14 +558,13 @@ send_message (LightDMGreeter *greeter, guint8 *message, gsize message_length, GE { GIOStatus status; gsize n_written; - GError *write_error = NULL; + g_autoptr(GError) write_error = NULL; status = g_io_channel_write_chars (priv->to_server_channel, data, data_length, &n_written, &write_error); if (write_error) g_set_error (error, LIGHTDM_GREETER_ERROR, LIGHTDM_GREETER_ERROR_COMMUNICATION_ERROR, "Failed to write to daemon: %s", write_error->message); - g_clear_error (&write_error); if (status == G_IO_STATUS_AGAIN) continue; if (status != G_IO_STATUS_NORMAL) @@ -580,7 +579,6 @@ send_message (LightDMGreeter *greeter, guint8 *message, gsize message_length, GE g_set_error (error, LIGHTDM_GREETER_ERROR, LIGHTDM_GREETER_ERROR_COMMUNICATION_ERROR, "Failed to write to daemon: %s", flush_error->message); - g_clear_error (&flush_error); return FALSE; } @@ -591,7 +589,7 @@ static void handle_connected (LightDMGreeter *greeter, gboolean v2, guint8 *message, gsize message_length, gsize *offset) { LightDMGreeterPrivate *priv = GET_PRIVATE (greeter); - GString *debug_string; + g_autoptr(GString) debug_string = NULL; int timeout; Request *request; @@ -638,7 +636,6 @@ handle_connected (LightDMGreeter *greeter, gboolean v2, guint8 *message, gsize m priv->connected = TRUE; g_debug ("%s", debug_string->str); - g_string_free (debug_string, TRUE); /* Set timeout for default login */ timeout = lightdm_greeter_get_autologin_timeout_hint (greeter); @@ -773,7 +770,7 @@ static void handle_reset (LightDMGreeter *greeter, guint8 *message, gsize message_length, gsize *offset) { LightDMGreeterPrivate *priv = GET_PRIVATE (greeter); - GString *hint_string; + g_autoptr(GString) hint_string = NULL; g_hash_table_remove_all (priv->hints); @@ -789,7 +786,6 @@ handle_reset (LightDMGreeter *greeter, guint8 *message, gsize message_length, gs } g_debug ("Reset%s", hint_string->str); - g_string_free (hint_string, TRUE); g_signal_emit (G_OBJECT (greeter), signals[RESET], 0); } @@ -900,7 +896,7 @@ recv_message (LightDMGreeter *greeter, gboolean block, guint8 **message, gsize * do { GIOStatus status; - GError *read_error = NULL; + g_autoptr(GError) read_error = NULL; status = g_io_channel_read_chars (priv->from_server_channel, (gchar *) priv->read_buffer + priv->n_read, @@ -917,7 +913,6 @@ recv_message (LightDMGreeter *greeter, gboolean block, guint8 **message, gsize * g_set_error (error, LIGHTDM_GREETER_ERROR, LIGHTDM_GREETER_ERROR_COMMUNICATION_ERROR, "Failed to read from daemon: %s", read_error->message); - g_clear_error (&read_error); return FALSE; } @@ -964,24 +959,20 @@ static gboolean from_server_cb (GIOChannel *source, GIOCondition condition, gpointer data) { LightDMGreeter *greeter = data; - guint8 *message; + g_autofree guint8 *message = NULL; gsize message_length; - GError *error = NULL; + g_autoptr(GError) error = NULL; /* Read one message and process it */ if (!recv_message (greeter, FALSE, &message, &message_length, &error)) { // FIXME: Should push this up to the client somehow g_warning ("Failed to read from daemon: %s\n", error->message); - g_clear_error (&error); return G_SOURCE_REMOVE; } if (message) - { handle_message (greeter, message, message_length); - g_free (message); - } return G_SOURCE_CONTINUE; } diff --git a/liblightdm-gobject/language.c b/liblightdm-gobject/language.c index 20c3e1cd..a0d7973e 100644 --- a/liblightdm-gobject/language.c +++ b/liblightdm-gobject/language.c @@ -61,25 +61,23 @@ static void update_languages (void) { gchar *command = "locale -a"; - gchar *stdout_text = NULL, *stderr_text = NULL; + g_autofree gchar *stdout_text = NULL; + g_autofree gchar *stderr_text = NULL; gint exit_status; gboolean result; - GError *error = NULL; + g_autoptr(GError) error = NULL; if (have_languages) return; result = g_spawn_command_line_sync (command, &stdout_text, &stderr_text, &exit_status, &error); if (error) - { g_warning ("Failed to run '%s': %s", command, error->message); - g_clear_error (&error); - } else if (exit_status != 0) g_warning ("Failed to get languages, '%s' returned %d", command, exit_status); else if (result) { - gchar **tokens; + g_auto(GStrv) tokens = NULL; int i; tokens = g_strsplit_set (stdout_text, "\n\r", -1); @@ -99,13 +97,8 @@ update_languages (void) language = g_object_new (LIGHTDM_TYPE_LANGUAGE, "code", code, NULL); languages = g_list_append (languages, language); } - - g_strfreev (tokens); } - g_free (stdout_text); - g_free (stderr_text); - have_languages = TRUE; } @@ -134,19 +127,13 @@ get_locale_name (const gchar *code) if (!avail_locales) { - gchar *locales; - GError *error = NULL; + g_autofree gchar *locales = NULL; + g_autoptr(GError) error = NULL; if (g_spawn_command_line_sync ("locale -a", &locales, NULL, NULL, &error)) - { avail_locales = g_strsplit (g_strchomp (locales), "\n", -1); - g_free (locales); - } else - { g_warning ("Failed to run 'locale -a': %s", error->message); - g_clear_error (&error); - } } if (avail_locales) @@ -259,9 +246,8 @@ lightdm_language_get_name (LightDMLanguage *language) } if (!priv->name) { - gchar **tokens = g_strsplit_set (priv->code, "_.@", 2); + g_auto(GStrv) tokens = g_strsplit_set (priv->code, "_.@", 2); priv->name = g_strdup (tokens[0]); - g_strfreev (tokens); } } @@ -302,9 +288,8 @@ lightdm_language_get_territory (LightDMLanguage *language) } if (!priv->territory) { - gchar **tokens = g_strsplit_set (priv->code, "_.@", 3); + g_auto(GStrv) tokens = g_strsplit_set (priv->code, "_.@", 3); priv->territory = g_strdup (tokens[1]); - g_strfreev (tokens); } } diff --git a/liblightdm-gobject/layout.c b/liblightdm-gobject/layout.c index d2d81c27..22aa8706 100644 --- a/liblightdm-gobject/layout.c +++ b/liblightdm-gobject/layout.c @@ -72,7 +72,7 @@ make_layout_string (const gchar *layout, const gchar *variant) static void parse_layout_string (const gchar *name, gchar **layout, gchar **variant) { - gchar **split; + g_auto(GStrv) split = NULL; *layout = NULL; *variant = NULL; @@ -87,7 +87,6 @@ parse_layout_string (const gchar *name, gchar **layout, gchar **variant) if (split[1]) *variant = g_strdup (split[1]); } - g_strfreev (split); } static void diff --git a/liblightdm-gobject/power.c b/liblightdm-gobject/power.c index bc1ddefd..5ad4d4f8 100644 --- a/liblightdm-gobject/power.c +++ b/liblightdm-gobject/power.c @@ -125,7 +125,7 @@ gboolean lightdm_get_can_suspend (void) { gboolean can_suspend = FALSE; - GVariant *r; + g_autoptr(GVariant) r = NULL; r = login1_call_function ("CanSuspend", NULL, NULL); if (r) @@ -149,8 +149,6 @@ lightdm_get_can_suspend (void) if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) g_variant_get (r, "(b)", &can_suspend); } - if (r) - g_variant_unref (r); return can_suspend; } @@ -166,30 +164,26 @@ lightdm_get_can_suspend (void) gboolean lightdm_suspend (GError **error) { - GVariant *result; - gboolean suspended; + g_autoptr(GVariant) login1_result = NULL; + g_autoptr(GVariant) ck_result = NULL; + g_autoptr(GVariant) upower_result = NULL; + g_autoptr(GError) login1_error = NULL; + g_autoptr(GError) ck_error = NULL; - result = login1_call_function ("Suspend", g_variant_new("(b)", FALSE), error); - if (!result) - { - if (error) - g_debug ("Can't suspend using logind; falling back to ConsoleKit: %s", (*error)->message); - g_clear_error (error); - result = ck_call_function ("Suspend", g_variant_new ("(b)", FALSE), error); - } - if (!result) - { - if (error) - g_debug ("Can't suspend using logind or ConsoleKit; falling back to UPower: %s", (*error)->message); - g_clear_error (error); - result = upower_call_function ("Suspend", error); - } + login1_result = login1_call_function ("Suspend", g_variant_new("(b)", FALSE), &login1_error); + if (login1_result) + return TRUE; + + g_debug ("Can't suspend using logind; falling back to ConsoleKit: %s", login1_error->message); + + ck_result = ck_call_function ("Suspend", g_variant_new ("(b)", FALSE), &ck_error); + if (ck_result) + return TRUE; - suspended = result != NULL; - if (result) - g_variant_unref (result); + g_debug ("Can't suspend using logind or ConsoleKit; falling back to UPower: %s", ck_error->message); - return suspended; + upower_result = upower_call_function ("Suspend", error); + return upower_result != NULL; } /** @@ -203,7 +197,7 @@ gboolean lightdm_get_can_hibernate (void) { gboolean can_hibernate = FALSE; - GVariant *r; + g_autoptr(GVariant) r = NULL; r = login1_call_function ("CanHibernate", NULL, NULL); if (r) @@ -227,8 +221,6 @@ lightdm_get_can_hibernate (void) if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) g_variant_get (r, "(b)", &can_hibernate); } - if (r) - g_variant_unref (r); return can_hibernate; } @@ -244,30 +236,26 @@ lightdm_get_can_hibernate (void) gboolean lightdm_hibernate (GError **error) { - GVariant *result; - gboolean hibernated; + g_autoptr(GVariant) login1_result = NULL; + g_autoptr(GVariant) ck_result = NULL; + g_autoptr(GVariant) upower_result = NULL; + g_autoptr(GError) login1_error = NULL; + g_autoptr(GError) ck_error = NULL; - result = login1_call_function ("Hibernate", g_variant_new("(b)", FALSE), error); - if (!result) - { - if (error) - g_debug ("Can't hibernate using logind; falling back to ConsoleKit: %s", (*error)->message); - g_clear_error (error); - result = ck_call_function ("Hibernate", g_variant_new ("(b)", FALSE), error); - } - if (!result) - { - if (error) - g_debug ("Can't hibernate using logind or ConsoleKit; falling back to UPower: %s", (*error)->message); - g_clear_error (error); - result = upower_call_function ("Hibernate", error); - } + login1_result = login1_call_function ("Hibernate", g_variant_new("(b)", FALSE), &login1_error); + if (login1_result) + return TRUE; + + g_debug ("Can't hibernate using logind; falling back to ConsoleKit: %s", login1_error->message); - hibernated = result != NULL; - if (result) - g_variant_unref (result); + ck_result = ck_call_function ("Hibernate", g_variant_new ("(b)", FALSE), &ck_error); + if (ck_result) + return TRUE; - return hibernated; + g_debug ("Can't hibernate using logind or ConsoleKit; falling back to UPower: %s", ck_error->message); + + upower_result = upower_call_function ("Hibernate", error); + return upower_result != NULL; } /** @@ -281,7 +269,7 @@ gboolean lightdm_get_can_restart (void) { gboolean can_restart = FALSE; - GVariant *r; + g_autoptr(GVariant) r = NULL; r = login1_call_function ("CanReboot", NULL, NULL); if (r) @@ -299,8 +287,6 @@ lightdm_get_can_restart (void) if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) g_variant_get (r, "(b)", &can_restart); } - if (r) - g_variant_unref (r); return can_restart; } @@ -316,20 +302,16 @@ lightdm_get_can_restart (void) gboolean lightdm_restart (GError **error) { - GVariant *r; - gboolean restarted; + g_autoptr(GVariant) login1_result = NULL; + g_autoptr(GVariant) ck_result = NULL; + g_autoptr(GError) login1_error = NULL; - r = login1_call_function ("Reboot", g_variant_new("(b)", FALSE), error); - if (!r) - { - g_clear_error (error); - r = ck_call_function ("Restart", NULL, error); - } - restarted = r != NULL; - if (r) - g_variant_unref (r); + login1_result = login1_call_function ("Reboot", g_variant_new("(b)", FALSE), &login1_error); + if (login1_result) + return TRUE; - return restarted; + ck_result = ck_call_function ("Restart", NULL, error); + return ck_result != NULL; } /** @@ -343,7 +325,7 @@ gboolean lightdm_get_can_shutdown (void) { gboolean can_shutdown = FALSE; - GVariant *r; + g_autoptr(GVariant) r = NULL; r = login1_call_function ("CanPowerOff", NULL, NULL); if (r) @@ -361,8 +343,6 @@ lightdm_get_can_shutdown (void) if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) g_variant_get (r, "(b)", &can_shutdown); } - if (r) - g_variant_unref (r); return can_shutdown; } @@ -378,18 +358,14 @@ lightdm_get_can_shutdown (void) gboolean lightdm_shutdown (GError **error) { - GVariant *r; - gboolean shutdown; + g_autoptr(GVariant) login1_result = NULL; + g_autoptr(GVariant) ck_result = NULL; + g_autoptr(GError) login1_error = NULL; - r = login1_call_function ("PowerOff", g_variant_new("(b)", FALSE), error); - if (!r) - { - g_clear_error (error); - r = ck_call_function ("Stop", NULL, error); - } - shutdown = r != NULL; - if (r) - g_variant_unref (r); + login1_result = login1_call_function ("PowerOff", g_variant_new("(b)", FALSE), &login1_error); + if (login1_result) + return TRUE; - return shutdown; + ck_result = ck_call_function ("Stop", NULL, error); + return ck_result != NULL; } diff --git a/liblightdm-gobject/session.c b/liblightdm-gobject/session.c index 555a71a1..92412080 100644 --- a/liblightdm-gobject/session.c +++ b/liblightdm-gobject/session.c @@ -137,12 +137,11 @@ static GList * load_sessions_dir (GList *sessions, const gchar *sessions_dir, const gchar *default_type) { GDir *directory; - GError *error = NULL; + g_autoptr(GError) error = NULL; directory = g_dir_open (sessions_dir, 0, &error); if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) g_warning ("Failed to open sessions directory: %s", error->message); - g_clear_error (&error); if (!directory) return sessions; @@ -152,6 +151,7 @@ load_sessions_dir (GList *sessions, const gchar *sessions_dir, const gchar *defa gchar *path; GKeyFile *key_file; gboolean result; + g_autoptr(GError) e = NULL; filename = g_dir_read_name (directory); if (filename == NULL) @@ -163,10 +163,9 @@ load_sessions_dir (GList *sessions, const gchar *sessions_dir, const gchar *defa path = g_build_filename (sessions_dir, filename, NULL); key_file = g_key_file_new (); - result = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error); - if (error) - g_warning ("Failed to load session file %s: %s:", path, error->message); - g_clear_error (&error); + result = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &e); + if (e) + g_warning ("Failed to load session file %s: %s:", path, e->message); if (result) { @@ -198,7 +197,7 @@ static GList * load_sessions (const gchar *sessions_dir) { GList *sessions = NULL; - gchar **dirs; + g_auto(GStrv) dirs = NULL; int i; dirs = g_strsplit (sessions_dir, ":", -1); @@ -212,8 +211,6 @@ load_sessions (const gchar *sessions_dir) sessions = load_sessions_dir (sessions, dirs[i], default_type); } - g_strfreev (dirs); - return sessions; } diff --git a/liblightdm-gobject/system.c b/liblightdm-gobject/system.c index b1e23d20..9ff8ffd1 100644 --- a/liblightdm-gobject/system.c +++ b/liblightdm-gobject/system.c @@ -57,8 +57,8 @@ use_os_value (const gchar *name, const gchar *value) static void load_os_release (void) { - gchar *data; - gchar **lines; + g_autofree gchar *data = NULL; + g_auto(GStrv) lines = NULL; guint i; if (os_release_loaded) @@ -70,8 +70,7 @@ load_os_release (void) lines = g_strsplit (data, "\n", -1); for (i = 0; lines[i] != NULL; i++) { - gchar **tokens; - tokens = g_strsplit (lines[i], "=", 2); + g_auto(GStrv) tokens = g_strsplit (lines[i], "=", 2); if (tokens[0] != NULL && tokens[1] != NULL) { gchar *name, *value; @@ -87,10 +86,7 @@ load_os_release (void) use_os_value (name, value); } } - g_strfreev (tokens); } - g_strfreev (lines); - g_free (data); os_release_loaded = TRUE; } diff --git a/src/accounts.h b/src/accounts.h index ba77c0ff..ff40b25f 100644 --- a/src/accounts.h +++ b/src/accounts.h @@ -34,6 +34,8 @@ typedef struct GObjectClass parent_class; } UserClass; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (User, g_object_unref); + User *accounts_get_user_by_name (const gchar *username); User *accounts_get_current_user (void); diff --git a/src/console-kit.c b/src/console-kit.c index afa33c17..093bf118 100644 --- a/src/console-kit.c +++ b/src/console-kit.c @@ -17,17 +17,16 @@ gchar * ck_open_session (GVariantBuilder *parameters) { - GDBusConnection *bus; - GVariant *result; - gchar *cookie; - GError *error = NULL; + g_autoptr(GDBusConnection) bus = NULL; + g_autoptr(GVariant) result = NULL; + g_autofree gchar *cookie = NULL; + g_autoptr(GError) error = NULL; g_return_val_if_fail (parameters != NULL, NULL); bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error) g_warning ("Failed to get system bus: %s", error->message); - g_clear_error (&error); if (!bus) return NULL; result = g_dbus_connection_call_sync (bus, @@ -41,27 +40,24 @@ ck_open_session (GVariantBuilder *parameters) -1, NULL, &error); - g_object_unref (bus); if (error) g_warning ("Failed to open CK session: %s", error->message); - g_clear_error (&error); if (!result) return NULL; g_variant_get (result, "(s)", &cookie); - g_variant_unref (result); g_debug ("Opened ConsoleKit session %s", cookie); - return cookie; + return g_steal_pointer (&cookie); } static gchar * get_ck_session (GDBusConnection *bus, const gchar *cookie) { - GVariant *result; - gchar *session_path; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autofree gchar *session_path = NULL; + g_autoptr(GError) error = NULL; bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); result = g_dbus_connection_call_sync (bus, @@ -75,25 +71,23 @@ get_ck_session (GDBusConnection *bus, const gchar *cookie) -1, NULL, &error); - g_object_unref (bus); if (error) g_warning ("Error getting ConsoleKit session: %s", error->message); - g_clear_error (&error); if (!result) return NULL; g_variant_get (result, "(o)", &session_path); - g_variant_unref (result); - return session_path; + return g_steal_pointer (&session_path); } void ck_lock_session (const gchar *cookie) { - GDBusConnection *bus; - gchar *session_path; - GError *error = NULL; + g_autoptr(GDBusConnection) bus = NULL; + g_autofree gchar *session_path = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; g_return_if_fail (cookie != NULL); @@ -102,41 +96,35 @@ ck_lock_session (const gchar *cookie) bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error) g_warning ("Failed to get system bus: %s", error->message); - g_clear_error (&error); if (!bus) return; session_path = get_ck_session (bus, cookie); - if (session_path) - { - GVariant *result; - - result = g_dbus_connection_call_sync (bus, - "org.freedesktop.ConsoleKit", - session_path, - "org.freedesktop.ConsoleKit.Session", - "Lock", - g_variant_new ("()"), - G_VARIANT_TYPE ("()"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (error) - g_warning ("Error locking ConsoleKit session: %s", error->message); - g_clear_error (&error); - if (result) - g_variant_unref (result); - } - g_object_unref (bus); + if (!session_path) + return; + + result = g_dbus_connection_call_sync (bus, + "org.freedesktop.ConsoleKit", + session_path, + "org.freedesktop.ConsoleKit.Session", + "Lock", + g_variant_new ("()"), + G_VARIANT_TYPE ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error locking ConsoleKit session: %s", error->message); } void ck_unlock_session (const gchar *cookie) { - GDBusConnection *bus; - gchar *session_path; - GError *error = NULL; + g_autoptr(GDBusConnection) bus = NULL; + g_autofree gchar *session_path = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; g_return_if_fail (cookie != NULL); @@ -145,41 +133,35 @@ ck_unlock_session (const gchar *cookie) bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error) g_warning ("Failed to get system bus: %s", error->message); - g_clear_error (&error); if (!bus) return; session_path = get_ck_session (bus, cookie); - if (session_path) - { - GVariant *result; - - result = g_dbus_connection_call_sync (bus, - "org.freedesktop.ConsoleKit", - session_path, - "org.freedesktop.ConsoleKit.Session", - "Unlock", - g_variant_new ("()"), - G_VARIANT_TYPE ("()"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (error) - g_warning ("Error unlocking ConsoleKit session: %s", error->message); - g_clear_error (&error); - if (result) - g_variant_unref (result); - } - g_object_unref (bus); + if (!session_path) + return; + + result = g_dbus_connection_call_sync (bus, + "org.freedesktop.ConsoleKit", + session_path, + "org.freedesktop.ConsoleKit.Session", + "Unlock", + g_variant_new ("()"), + G_VARIANT_TYPE ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error unlocking ConsoleKit session: %s", error->message); } void ck_activate_session (const gchar *cookie) { - GDBusConnection *bus; - gchar *session_path; - GError *error = NULL; + g_autoptr(GDBusConnection) bus = NULL; + g_autofree gchar *session_path = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; g_return_if_fail (cookie != NULL); @@ -188,42 +170,35 @@ ck_activate_session (const gchar *cookie) bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error) g_warning ("Failed to get system bus: %s", error->message); - g_clear_error (&error); if (!bus) return; session_path = get_ck_session (bus, cookie); - if (session_path) - { - GVariant *result; - - result = g_dbus_connection_call_sync (bus, - "org.freedesktop.ConsoleKit", - session_path, - "org.freedesktop.ConsoleKit.Session", - "Activate", - g_variant_new ("()"), - G_VARIANT_TYPE ("()"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (error) - g_warning ("Error activating ConsoleKit session: %s", error->message); - g_clear_error (&error); - if (result) - g_variant_unref (result); - } - g_object_unref (bus); + if (!session_path) + return; + + result = g_dbus_connection_call_sync (bus, + "org.freedesktop.ConsoleKit", + session_path, + "org.freedesktop.ConsoleKit.Session", + "Activate", + g_variant_new ("()"), + G_VARIANT_TYPE ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error activating ConsoleKit session: %s", error->message); } void ck_close_session (const gchar *cookie) { - GDBusConnection *bus; - GVariant *result; + g_autoptr(GDBusConnection) bus = NULL; + g_autoptr(GVariant) result = NULL; gboolean is_closed; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_return_if_fail (cookie != NULL); @@ -232,7 +207,6 @@ ck_close_session (const gchar *cookie) bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error) g_warning ("Failed to get system bus: %s", error->message); - g_clear_error (&error); if (!bus) return; result = g_dbus_connection_call_sync (bus, @@ -246,17 +220,13 @@ ck_close_session (const gchar *cookie) -1, NULL, &error); - g_object_unref (bus); if (error) g_warning ("Error ending ConsoleKit session: %s", error->message); - g_clear_error (&error); if (!result) return; g_variant_get (result, "(b)", &is_closed); - g_variant_unref (result); - if (!is_closed) g_warning ("ConsoleKit.Manager.CloseSession() returned false"); } @@ -264,10 +234,11 @@ ck_close_session (const gchar *cookie) gchar * ck_get_xdg_runtime_dir (const gchar *cookie) { - GDBusConnection *bus; - gchar *session_path; - gchar *runtime_dir = NULL; - GError *error = NULL; + g_autoptr(GDBusConnection) bus = NULL; + g_autofree gchar *session_path = NULL; + g_autoptr(GVariant) result = NULL; + g_autofree gchar *runtime_dir = NULL; + g_autoptr(GError) error = NULL; g_return_val_if_fail (cookie != NULL, NULL); @@ -276,38 +247,31 @@ ck_get_xdg_runtime_dir (const gchar *cookie) bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error) g_warning ("Failed to get system bus: %s", error->message); - g_clear_error (&error); if (!bus) return NULL; session_path = get_ck_session (bus, cookie); - if (session_path) - { - GVariant *result; - - result = g_dbus_connection_call_sync (bus, - "org.freedesktop.ConsoleKit", - session_path, - "org.freedesktop.ConsoleKit.Session", - "GetXDGRuntimeDir", - g_variant_new ("()"), - G_VARIANT_TYPE ("(s)"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (error) - g_warning ("Error getting XDG_RUNTIME_DIR from ConsoleKit: %s", error->message); - g_clear_error (&error); - if (!result) - return NULL; - - g_variant_get (result, "(s)", &runtime_dir); - g_variant_unref (result); - g_debug ("ConsoleKit XDG_RUNTIME_DIR is %s", runtime_dir); - } - - g_object_unref (bus); - - return runtime_dir; + if (!session_path) + return NULL; + + result = g_dbus_connection_call_sync (bus, + "org.freedesktop.ConsoleKit", + session_path, + "org.freedesktop.ConsoleKit.Session", + "GetXDGRuntimeDir", + g_variant_new ("()"), + G_VARIANT_TYPE ("(s)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error getting XDG_RUNTIME_DIR from ConsoleKit: %s", error->message); + if (!result) + return NULL; + + g_variant_get (result, "(s)", &runtime_dir); + g_debug ("ConsoleKit XDG_RUNTIME_DIR is %s", runtime_dir); + + return g_steal_pointer (&runtime_dir); } diff --git a/src/display-manager-service.c b/src/display-manager-service.c index 796b676e..519ef056 100644 --- a/src/display-manager-service.c +++ b/src/display-manager-service.c @@ -109,7 +109,7 @@ static void emit_object_value_changed (GDBusConnection *bus, const gchar *path, const gchar *interface_name, const gchar *property_name, GVariant *property_value) { GVariantBuilder builder; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); g_variant_builder_add (&builder, "{sv}", property_name, property_value); @@ -122,13 +122,12 @@ emit_object_value_changed (GDBusConnection *bus, const gchar *path, const gchar g_variant_new ("(sa{sv}as)", interface_name, &builder, NULL), &error)) g_warning ("Failed to emit PropertiesChanged signal: %s", error->message); - g_clear_error (&error); } static void emit_object_signal (GDBusConnection *bus, const gchar *path, const gchar *signal_name, const gchar *object_path) { - GError *error = NULL; + g_autoptr(GError) error = NULL; if (!g_dbus_connection_emit_signal (bus, NULL, @@ -138,7 +137,6 @@ emit_object_signal (GDBusConnection *bus, const gchar *path, const gchar *signal g_variant_new ("(o)", object_path), &error)) g_warning ("Failed to emit %s signal on %s: %s", signal_name, path, error->message); - g_clear_error (&error); } static void @@ -430,7 +428,7 @@ running_user_session_cb (Seat *seat, Session *session, DisplayManagerService *se SeatBusEntry *seat_entry; SessionBusEntry *session_entry; gchar *path; - GError *error = NULL; + g_autoptr(GError) error = NULL; /* Set environment variables when session runs */ seat_entry = g_hash_table_lookup (service->priv->seat_bus_entries, seat); @@ -453,7 +451,6 @@ running_user_session_cb (Seat *seat, Session *session, DisplayManagerService *se &error); if (session_entry->bus_id == 0) g_warning ("Failed to register user session: %s", error->message); - g_clear_error (&error); emit_object_value_changed (service->priv->bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (service, NULL)); emit_object_signal (service->priv->bus, "/org/freedesktop/DisplayManager", "SessionAdded", session_entry->path); @@ -466,7 +463,7 @@ static void session_removed_cb (Seat *seat, Session *session, DisplayManagerService *service) { SessionBusEntry *entry; - gchar *seat_path = NULL; + g_autofree gchar *seat_path = NULL; g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat); @@ -485,7 +482,6 @@ session_removed_cb (Seat *seat, Session *session, DisplayManagerService *service { emit_object_value_changed (service->priv->bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (service, NULL)); emit_object_value_changed (service->priv->bus, seat_path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (service, seat_path)); - g_free (seat_path); } } @@ -497,15 +493,14 @@ seat_added_cb (DisplayManager *display_manager, Seat *seat, DisplayManagerServic handle_seat_call, handle_seat_get_property }; - gchar *path; + g_autofree gchar *path = NULL; SeatBusEntry *entry; - GError *error = NULL; + g_autoptr(GError) error = NULL; path = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", service->priv->seat_index); service->priv->seat_index++; entry = seat_bus_entry_new (service, seat, path); - g_free (path); g_hash_table_insert (service->priv->seat_bus_entries, g_object_ref (seat), entry); g_debug ("Registering seat with bus path %s", entry->path); @@ -518,7 +513,6 @@ seat_added_cb (DisplayManager *display_manager, Seat *seat, DisplayManagerServic &error); if (entry->bus_id == 0) g_warning ("Failed to register seat: %s", error->message); - g_clear_error (&error); emit_object_value_changed (service->priv->bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list (service)); emit_object_signal (service->priv->bus, "/org/freedesktop/DisplayManager", "SeatAdded", entry->path); @@ -616,7 +610,7 @@ bus_acquired_cb (GDBusConnection *connection, DisplayManagerService *service = user_data; GDBusNodeInfo *display_manager_info; GList *link; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_debug ("Acquired bus name %s", name); @@ -637,7 +631,6 @@ bus_acquired_cb (GDBusConnection *connection, &error); if (service->priv->reg_id == 0) g_warning ("Failed to register display manager: %s", error->message); - g_clear_error (&error); g_dbus_node_info_unref (display_manager_info); /* Add objects for existing seats and listen to new ones */ diff --git a/src/dm-tool.c b/src/dm-tool.c index fd9d57ea..9493df21 100644 --- a/src/dm-tool.c +++ b/src/dm-tool.c @@ -39,9 +39,9 @@ xephyr_setup_cb (gpointer user_data) static void xephyr_signal_cb (int signum) { - gchar *path; - GVariant *result; - GError *error = NULL; + g_autofree gchar *path = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; result = g_dbus_proxy_call_sync (dm_proxy, "AddLocalXSeat", @@ -72,7 +72,7 @@ xephyr_signal_cb (int signum) static GDBusProxy * get_seat_proxy (void) { - GError *error = NULL; + g_autoptr(GError) error = NULL; if (seat_proxy) return seat_proxy; @@ -96,7 +96,6 @@ get_seat_proxy (void) g_printerr ("Unable to contact display manager: %s\n", error->message); exit (EXIT_FAILURE); } - g_clear_error (&error); return seat_proxy; } @@ -107,7 +106,7 @@ main (int argc, char **argv) gchar *command; gint n_options; gchar **options; - GError *error = NULL; + g_autoptr(GError) error = NULL; gint arg_index; #if !defined(GLIB_VERSION_2_36) @@ -178,7 +177,6 @@ main (int argc, char **argv) g_printerr ("Unable to contact display manager: %s\n", error->message); return EXIT_FAILURE; } - g_clear_error (&error); command = argv[arg_index]; arg_index++; @@ -300,7 +298,7 @@ main (int argc, char **argv) while (g_variant_iter_loop (seat_iter, "&o", &seat_path)) { gchar *seat_name; - GDBusProxy *proxy; + g_autoptr(GDBusProxy) proxy = NULL; gchar **property_names; GVariant *sessions; GVariantIter *session_iter; @@ -327,14 +325,13 @@ main (int argc, char **argv) property_names = g_dbus_proxy_get_cached_property_names (proxy); for (i = 0; property_names[i]; i++) { - GVariant *value; + g_autoptr(GVariant) value = NULL; if (strcmp (property_names[i], "Sessions") == 0) continue; value = g_dbus_proxy_get_cached_property (proxy, property_names[i]); g_print (" %s=%s\n", property_names[i], g_variant_print (value, FALSE)); - g_variant_unref (value); } sessions = g_dbus_proxy_get_cached_property (proxy, "Sessions"); @@ -344,8 +341,8 @@ main (int argc, char **argv) g_variant_get (sessions, "ao", &session_iter); while (g_variant_iter_loop (session_iter, "&o", &session_path)) { - GDBusProxy *session_proxy; - gchar *session_name; + g_autoptr(GDBusProxy) session_proxy = NULL; + const gchar *session_name; if (g_str_has_prefix (session_path, "/org/freedesktop/DisplayManager/")) session_name = session_path + strlen ("/org/freedesktop/DisplayManager/"); @@ -367,21 +364,16 @@ main (int argc, char **argv) property_names = g_dbus_proxy_get_cached_property_names (session_proxy); for (i = 0; property_names[i]; i++) { - GVariant *value; + g_autoptr(GVariant) value = NULL; if (strcmp (property_names[i], "Seat") == 0) continue; value = g_dbus_proxy_get_cached_property (session_proxy, property_names[i]); g_print (" %s=%s\n", property_names[i], g_variant_print (value, FALSE)); - g_variant_unref (value); } - - g_object_unref (session_proxy); } g_variant_iter_free (session_iter); - - g_object_unref (proxy); } g_variant_iter_free (seat_iter); @@ -423,12 +415,11 @@ main (int argc, char **argv) xephyr_display_number = 0; while (TRUE) { - gchar *lock_name; + g_autofree gchar *lock_name = NULL; gboolean has_lock; lock_name = g_strdup_printf ("/tmp/.X%d-lock", xephyr_display_number); has_lock = g_file_test (lock_name, G_FILE_TEST_EXISTS); - g_free (lock_name); if (has_lock) xephyr_display_number++; @@ -460,7 +451,6 @@ main (int argc, char **argv) g_printerr ("Error running Xephyr: %s\n", error->message); exit (EXIT_FAILURE); } - g_clear_error (&error); /* Block until ready */ loop = g_main_loop_new (NULL, FALSE); @@ -524,7 +514,8 @@ main (int argc, char **argv) for (i = 1; i < n_options; i++) { - gchar *property, *name, *value; + g_autofree gchar *property = NULL; + gchar *name, *value; property = g_strdup (options[i]); name = property; @@ -538,7 +529,6 @@ main (int argc, char **argv) value = ""; g_variant_builder_add_value (properties, g_variant_new ("(ss)", name, value)); - g_free (property); } result = g_dbus_proxy_call_sync (dm_proxy, diff --git a/src/greeter-session.c b/src/greeter-session.c index 8ebac6f2..72594794 100644 --- a/src/greeter-session.c +++ b/src/greeter-session.c @@ -44,7 +44,8 @@ greeter_session_start (Session *session) GreeterSession *s = GREETER_SESSION (session); int to_greeter_pipe[2], from_greeter_pipe[2]; int to_greeter_input, to_greeter_output, from_greeter_input, from_greeter_output; - gchar *value; + g_autofree gchar *to_server_value = NULL; + g_autofree gchar *from_server_value = NULL; gboolean result; /* Create a pipe to talk with the greeter */ @@ -65,12 +66,10 @@ greeter_session_start (Session *session) fcntl (from_greeter_output, F_SETFD, FD_CLOEXEC); /* Let the greeter session know how to communicate with the daemon */ - value = g_strdup_printf ("%d", from_greeter_input); - session_set_env (session, "LIGHTDM_TO_SERVER_FD", value); - g_free (value); - value = g_strdup_printf ("%d", to_greeter_output); - session_set_env (session, "LIGHTDM_FROM_SERVER_FD", value); - g_free (value); + to_server_value = g_strdup_printf ("%d", from_greeter_input); + session_set_env (session, "LIGHTDM_TO_SERVER_FD", to_server_value); + from_server_value = g_strdup_printf ("%d", to_greeter_output); + session_set_env (session, "LIGHTDM_FROM_SERVER_FD", from_server_value); result = SESSION_CLASS (greeter_session_parent_class)->start (session); diff --git a/src/greeter-socket.c b/src/greeter-socket.c index 4a7beb53..878aeb12 100644 --- a/src/greeter-socket.c +++ b/src/greeter-socket.c @@ -67,13 +67,12 @@ greeter_disconnected_cb (Greeter *greeter, GreeterSocket *socket) static gboolean greeter_connect_cb (GSocket *s, GIOCondition condition, GreeterSocket *socket) { - GSocket *new_socket; - GError *error = NULL; + g_autoptr(GSocket) new_socket = NULL; + g_autoptr(GError) error = NULL; new_socket = g_socket_accept (socket->priv->socket, NULL, &error); if (error) g_warning ("Failed to accept greeter connection: %s", error->message); - g_clear_error (&error); if (!new_socket) return G_SOURCE_CONTINUE; @@ -81,14 +80,13 @@ greeter_connect_cb (GSocket *s, GIOCondition condition, GreeterSocket *socket) if (socket->priv->greeter) { g_socket_close (new_socket, NULL); - g_object_unref (new_socket); return G_SOURCE_CONTINUE; } - socket->priv->greeter_socket = new_socket; + socket->priv->greeter_socket = g_steal_pointer (&new_socket); g_signal_emit (socket, signals[CREATE_GREETER], 0, &socket->priv->greeter); g_signal_connect (socket->priv->greeter, GREETER_SIGNAL_DISCONNECTED, G_CALLBACK (greeter_disconnected_cb), socket); - greeter_set_file_descriptors (socket->priv->greeter, g_socket_get_fd (new_socket), g_socket_get_fd (new_socket)); + greeter_set_file_descriptors (socket->priv->greeter, g_socket_get_fd (socket->priv->greeter_socket), g_socket_get_fd (socket->priv->greeter_socket)); return G_SOURCE_CONTINUE; } @@ -96,7 +94,7 @@ greeter_connect_cb (GSocket *s, GIOCondition condition, GreeterSocket *socket) gboolean greeter_socket_start (GreeterSocket *socket, GError **error) { - GSocketAddress *address; + g_autoptr(GSocketAddress) address = NULL; gboolean result; g_return_val_if_fail (socket != NULL, FALSE); @@ -109,7 +107,6 @@ greeter_socket_start (GreeterSocket *socket, GError **error) unlink (socket->priv->path); address = g_unix_socket_address_new (socket->priv->path); result = g_socket_bind (socket->priv->socket, address, FALSE, error); - g_object_unref (address); if (!result) return FALSE; if (!g_socket_listen (socket->priv->socket, error)) @@ -146,8 +143,8 @@ greeter_socket_finalize (GObject *object) GreeterSocket *self = GREETER_SOCKET (object); if (self->priv->path) - unlink (self->priv->path); - g_free (self->priv->path); + unlink (self->priv->path); + g_clear_pointer (&self->priv->path, g_free); g_clear_object (&self->priv->socket); g_clear_object (&self->priv->source); g_clear_object (&self->priv->greeter_socket); diff --git a/src/greeter.c b/src/greeter.c index 0d07762f..58529f17 100644 --- a/src/greeter.c +++ b/src/greeter.c @@ -125,7 +125,8 @@ greeter_new (void) void greeter_set_file_descriptors (Greeter *greeter, int to_greeter_fd, int from_greeter_fd) { - GError *error = NULL; + g_autoptr(GError) to_error = NULL; + g_autoptr(GError) from_error = NULL; g_return_if_fail (greeter != NULL); g_return_if_fail (greeter->priv->to_greeter_input < 0); @@ -133,16 +134,14 @@ greeter_set_file_descriptors (Greeter *greeter, int to_greeter_fd, int from_gree greeter->priv->to_greeter_input = to_greeter_fd; greeter->priv->to_greeter_channel = g_io_channel_unix_new (greeter->priv->to_greeter_input); - g_io_channel_set_encoding (greeter->priv->to_greeter_channel, NULL, &error); - if (error) - g_warning ("Failed to set encoding on to greeter channel to binary: %s\n", error->message); - g_clear_error (&error); + g_io_channel_set_encoding (greeter->priv->to_greeter_channel, NULL, &to_error); + if (to_error) + g_warning ("Failed to set encoding on to greeter channel to binary: %s\n", to_error->message); greeter->priv->from_greeter_output = from_greeter_fd; greeter->priv->from_greeter_channel = g_io_channel_unix_new (greeter->priv->from_greeter_output); - g_io_channel_set_encoding (greeter->priv->from_greeter_channel, NULL, &error); - if (error) - g_warning ("Failed to set encoding on from greeter channel to binary: %s\n", error->message); - g_clear_error (&error); + g_io_channel_set_encoding (greeter->priv->from_greeter_channel, NULL, &from_error); + if (from_error) + g_warning ("Failed to set encoding on from greeter channel to binary: %s\n", from_error->message); g_io_channel_set_buffered (greeter->priv->from_greeter_channel, FALSE); greeter->priv->from_greeter_watch = g_io_add_watch (greeter->priv->from_greeter_channel, G_IO_IN | G_IO_HUP, read_cb, greeter); } @@ -213,6 +212,16 @@ secure_free (Greeter *greeter, void *ptr) return g_free (ptr); } +static void +secure_freev (Greeter *greeter, gchar **v) +{ + int i; + + for (i = 0; v[i]; i++) + secure_free (greeter, v[i]); + g_free (v); +} + static guint32 int_length (void) { @@ -227,7 +236,7 @@ write_message (Greeter *greeter, guint8 *message, gsize message_length) { gchar *data; gsize data_length; - GError *error = NULL; + g_autoptr(GError) error = NULL; data = (gchar *) message; data_length = message_length; @@ -239,7 +248,6 @@ write_message (Greeter *greeter, guint8 *message, gsize message_length) status = g_io_channel_write_chars (greeter->priv->to_greeter_channel, data, data_length, &n_written, &error); if (error) g_warning ("Error writing to greeter: %s", error->message); - g_clear_error (&error); if (status != G_IO_STATUS_NORMAL) return; data_length -= n_written; @@ -249,7 +257,6 @@ write_message (Greeter *greeter, guint8 *message, gsize message_length) g_io_channel_flush (greeter->priv->to_greeter_channel, &error); if (error) g_warning ("Failed to flush data to greeter: %s", error->message); - g_clear_error (&error); } static void @@ -550,11 +557,13 @@ handle_authenticate_as_guest (Greeter *greeter, guint32 sequence_number) static gchar * get_remote_session_service (const gchar *session_name) { - GKeyFile *session_desktop_file; + g_autoptr(GKeyFile) session_desktop_file = NULL; gboolean result; const gchar *c; - gchar *remote_sessions_dir, *filename, *path, *service = NULL; - GError *error = NULL; + g_autofree gchar *remote_sessions_dir = NULL; + g_autofree gchar *filename = NULL; + g_autofree gchar *path = NULL; + g_autoptr(GError) error = NULL; /* Validate session name doesn't contain directory separators */ for (c = session_name; *c; c++) @@ -568,24 +577,19 @@ get_remote_session_service (const gchar *session_name) filename = g_strdup_printf ("%s.desktop", session_name); remote_sessions_dir = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory"); path = g_build_filename (remote_sessions_dir, filename, NULL); - g_free (remote_sessions_dir); - g_free (filename); result = g_key_file_load_from_file (session_desktop_file, path, G_KEY_FILE_NONE, &error); if (error) g_debug ("Failed to load session file %s: %s", path, error->message); - g_free (path); - g_clear_error (&error); - if (result) - service = g_key_file_get_string (session_desktop_file, G_KEY_FILE_DESKTOP_GROUP, "X-LightDM-PAM-Service", NULL); - g_key_file_free (session_desktop_file); + if (!result) + return NULL; - return service; + return g_key_file_get_string (session_desktop_file, G_KEY_FILE_DESKTOP_GROUP, "X-LightDM-PAM-Service", NULL); } static void handle_authenticate_remote (Greeter *greeter, const gchar *session_name, const gchar *username, guint32 sequence_number) { - gchar *service; + g_autofree gchar *service = NULL; if (username[0] == '\0') { @@ -621,8 +625,6 @@ handle_authenticate_remote (Greeter *greeter, const gchar *session_name, const g session_start (greeter->priv->authentication_session); } - g_free (service); - if (!greeter->priv->authentication_session) { send_end_authentication (greeter, sequence_number, "", PAM_USER_UNKNOWN); @@ -756,7 +758,7 @@ handle_set_language (Greeter *greeter, const gchar *language) static void handle_ensure_shared_dir (Greeter *greeter, const gchar *username) { - gchar *dir; + g_autofree gchar *dir = NULL; guint8 message[MAX_MESSAGE_LENGTH]; gsize offset = 0; @@ -767,8 +769,6 @@ handle_ensure_shared_dir (Greeter *greeter, const gchar *username) write_header (message, MAX_MESSAGE_LENGTH, SERVER_MESSAGE_SHARED_DIR_RESULT, string_length (dir), &offset); write_string (message, MAX_MESSAGE_LENGTH, dir, &offset); write_message (greeter, message, offset); - - g_free (dir); } static guint32 @@ -849,11 +849,14 @@ read_cb (GIOChannel *source, GIOCondition condition, gpointer data) GIOStatus status; int id, length, i; guint32 sequence_number, n_secrets, max_secrets; - gchar *version, *username, *session_name, *language; + g_autofree gchar *version = NULL; + g_autofree gchar *username = NULL; + g_autofree gchar *session_name = NULL; + g_autofree gchar *language = NULL; gchar **secrets; gboolean resettable = FALSE; guint32 api_version = 0; - GError *error = NULL; + g_autoptr(GError) error = NULL; if (condition == G_IO_HUP) { @@ -881,7 +884,6 @@ read_cb (GIOChannel *source, GIOCondition condition, gpointer data) &error); if (error) g_warning ("Error reading from greeter: %s", error->message); - g_clear_error (&error); if (status == G_IO_STATUS_EOF) { g_debug ("Greeter closed communication channel"); @@ -920,13 +922,11 @@ read_cb (GIOChannel *source, GIOCondition condition, gpointer data) if (offset < length) api_version = read_int (greeter, &offset); handle_connect (greeter, version, resettable, api_version); - g_free (version); break; case GREETER_MESSAGE_AUTHENTICATE: sequence_number = read_int (greeter, &offset); username = read_string (greeter, &offset); handle_authenticate (greeter, sequence_number, username); - g_free (username); break; case GREETER_MESSAGE_AUTHENTICATE_AS_GUEST: sequence_number = read_int (greeter, &offset); @@ -952,9 +952,7 @@ read_cb (GIOChannel *source, GIOCondition condition, gpointer data) secrets[i] = read_secret (greeter, &offset); secrets[i] = NULL; handle_continue_authentication (greeter, secrets); - for (i = 0; i < n_secrets; i++) - secure_free (greeter, secrets[i]); - g_free (secrets); + secure_freev (greeter, secrets); break; case GREETER_MESSAGE_CANCEL_AUTHENTICATION: handle_cancel_authentication (greeter); @@ -962,17 +960,14 @@ read_cb (GIOChannel *source, GIOCondition condition, gpointer data) case GREETER_MESSAGE_START_SESSION: session_name = read_string (greeter, &offset); handle_start_session (greeter, session_name); - g_free (session_name); break; case GREETER_MESSAGE_SET_LANGUAGE: language = read_string (greeter, &offset); handle_set_language (greeter, language); - g_free (language); break; case GREETER_MESSAGE_ENSURE_SHARED_DIR: username = read_string (greeter, &offset); handle_ensure_shared_dir (greeter, username); - g_free (username); break; default: g_warning ("Unknown message from greeter: %d", id); @@ -1055,12 +1050,12 @@ greeter_finalize (GObject *object) { Greeter *self = GREETER (object); - g_free (self->priv->pam_service); - g_free (self->priv->autologin_pam_service); + g_clear_pointer (&self->priv->pam_service, g_free); + g_clear_pointer (&self->priv->autologin_pam_service, g_free); secure_free (self, self->priv->read_buffer); g_hash_table_unref (self->priv->hints); - g_free (self->priv->remote_session); - g_free (self->priv->active_username); + g_clear_pointer (&self->priv->remote_session, g_free); + g_clear_pointer (&self->priv->active_username, g_free); if (self->priv->authentication_session) { g_signal_handlers_disconnect_matched (self->priv->authentication_session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self); diff --git a/src/guest-account.c b/src/guest-account.c index 77c4594b..1011ec6a 100644 --- a/src/guest-account.c +++ b/src/guest-account.c @@ -18,7 +18,7 @@ static gchar * get_setup_script (void) { - gchar *script; + g_autofree gchar *script = NULL; static gchar *setup_script = NULL; if (setup_script) @@ -29,7 +29,6 @@ get_setup_script (void) return NULL; setup_script = g_find_program_in_path (script); - g_free (script); return setup_script; } @@ -44,7 +43,7 @@ static gboolean run_script (const gchar *script, gchar **stdout_text, gint *exit_status, GError **error) { gint argc; - gchar **argv; + g_auto(GStrv) argv = NULL; gboolean result; if (!g_shell_parse_argv (script, &argc, &argv, error)) @@ -54,7 +53,6 @@ run_script (const gchar *script, gchar **stdout_text, gint *exit_status, GError G_SPAWN_SEARCH_PATH, NULL, NULL, stdout_text, NULL, exit_status, error); - g_strfreev (argv); return result; } @@ -62,25 +60,25 @@ run_script (const gchar *script, gchar **stdout_text, gint *exit_status, GError gchar * guest_account_setup (void) { - gchar *command, *stdout_text, *username, **lines; + g_autofree gchar *command = NULL; + g_autofree gchar *stdout_text = NULL; + g_autofree gchar *username = NULL; + g_auto(GStrv) lines = NULL; gint exit_status; gboolean result; - GError *error = NULL; + g_autoptr(GError) error = NULL; command = g_strdup_printf ("%s add", get_setup_script ()); g_debug ("Opening guest account with command '%s'", command); result = run_script (command, &stdout_text, &exit_status, &error); - g_free (command); if (error) g_warning ("Error running guest account setup script '%s': %s", get_setup_script (), error->message); - g_clear_error (&error); if (!result) return NULL; if (exit_status != 0) { g_debug ("Guest account setup script returns %d: %s", exit_status, stdout_text); - g_free (stdout_text); return NULL; } @@ -90,37 +88,33 @@ guest_account_setup (void) username = g_strdup (g_strstrip (lines[g_strv_length (lines) - 1])); else username = g_strdup (""); - g_free (stdout_text); if (strcmp (username, "") == 0) { - g_free (username); g_debug ("Guest account setup script didn't return a username"); return NULL; } g_debug ("Guest account %s setup", username); - return username; + return g_steal_pointer (&username); } void guest_account_cleanup (const gchar *username) { - gchar *command; + g_autofree gchar *command = NULL; gboolean result; gint exit_status; - GError *error = NULL; + g_autoptr(GError) error = NULL; command = g_strdup_printf ("%s remove %s", get_setup_script (), username); g_debug ("Closing guest account %s with command '%s'", username, command); result = run_script (command, NULL, &exit_status, &error); - g_free (command); if (error) g_warning ("Error running guest account cleanup script '%s': %s", get_setup_script (), error->message); - g_clear_error (&error); if (result && exit_status != 0) g_debug ("Guest account cleanup script returns %d", exit_status); diff --git a/src/lightdm.c b/src/lightdm.c index 0ad438f6..f853565b 100644 --- a/src/lightdm.c +++ b/src/lightdm.c @@ -54,7 +54,7 @@ static void log_cb (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer data) { const gchar *prefix; - gchar *text; + g_autofree gchar *text = NULL; switch (log_level & G_LOG_LEVEL_MASK) { @@ -97,14 +97,13 @@ log_cb (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, g_printerr ("%s", text); else g_log_default_handler (log_domain, log_level, message, data); - - g_free (text); } static void log_init (void) { - gchar *log_dir, *path; + g_autofree gchar *log_dir = NULL; + g_autofree gchar *path = NULL; gboolean backup_logs; log_timer = g_timer_new (); @@ -112,7 +111,6 @@ log_init (void) /* Log to a file */ log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory"); path = g_build_filename (log_dir, "lightdm.log", NULL); - g_free (log_dir); backup_logs = config_get_boolean (config_get_instance (), "LightDM", "backup-logs"); log_fd = log_file_open (path, backup_logs ? LOG_MODE_BACKUP_AND_TRUNCATE : LOG_MODE_APPEND); @@ -120,13 +118,13 @@ log_init (void) g_log_set_default_handler (log_cb, NULL); g_debug ("Logging to %s", path); - g_free (path); } static GList* get_config_sections (const gchar *seat_name) { - gchar **groups, **i; + g_auto(GStrv) groups = NULL; + gchar **i; GList *config_sections = NULL; /* Load seat defaults first */ @@ -142,7 +140,6 @@ get_config_sections (const gchar *seat_name) config_sections = g_list_append (config_sections, g_strdup (*i)); } } - g_strfreev (groups); return config_sections; } @@ -151,23 +148,22 @@ static void set_seat_properties (Seat *seat, const gchar *seat_name) { GList *sections, *link; - gchar **keys; gint i; sections = get_config_sections (seat_name); for (link = sections; link; link = link->next) { const gchar *section = link->data; + g_auto(GStrv) keys = NULL; + keys = config_get_keys (config_get_instance (), section); l_debug (seat, "Loading properties from config section %s", section); for (i = 0; keys && keys[i]; i++) { - gchar *value = config_get_string (config_get_instance (), section, keys[i]); + g_autofree gchar *value = config_get_string (config_get_instance (), section, keys[i]); seat_set_property (seat, keys[i], value); - g_free (value); } - g_strfreev (keys); } g_list_free_full (sections, g_free); } @@ -211,8 +207,8 @@ create_seat (const gchar *module_name, const gchar *name) static Seat * service_add_xlocal_seat_cb (DisplayManagerService *service, gint display_number) { - Seat *seat; - gchar *display_number_string; + g_autoptr(Seat) seat = NULL; + g_autofree gchar *display_number_string = NULL; g_debug ("Adding local X seat :%d", display_number); @@ -223,24 +219,20 @@ service_add_xlocal_seat_cb (DisplayManagerService *service, gint display_number) set_seat_properties (seat, NULL); display_number_string = g_strdup_printf ("%d", display_number); seat_set_property (seat, "xserver-display-number", display_number_string); - g_free (display_number_string); if (!display_manager_add_seat (display_manager, seat)) - { - g_object_unref (seat); return NULL; - } - return seat; + return g_steal_pointer (&seat); } static void display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat) { - gchar **types; + g_auto(GStrv) types = NULL; gchar **iter; - Seat *next_seat = NULL; - GString *next_types; + g_autoptr(Seat) next_seat = NULL; + g_autoptr(GString) next_types = NULL; /* If we have fallback types registered for the seat, let's try them before giving up. */ @@ -263,7 +255,6 @@ display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat) g_string_append (next_types, *iter); } } - g_strfreev (types); if (next_seat) { @@ -276,7 +267,6 @@ display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat) seat_set_property (next_seat, "type", next_types->str); display_manager_add_seat (display_manager, next_seat); - g_object_unref (next_seat); } else if (seat_get_boolean_property (seat, "exit-on-failure")) { @@ -284,20 +274,17 @@ display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat) exit_code = EXIT_FAILURE; display_manager_stop (display_manager); } - - g_string_free (next_types, TRUE); } static gboolean xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session) { - SeatXDMCPSession *seat; + g_autoptr(SeatXDMCPSession) seat = NULL; gboolean result; seat = seat_xdmcp_session_new (session); set_seat_properties (SEAT (seat), NULL); result = display_manager_add_seat (display_manager, SEAT (seat)); - g_object_unref (seat); return result; } @@ -305,12 +292,11 @@ xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session) static void vnc_connection_cb (VNCServer *server, GSocket *connection) { - SeatXVNC *seat; + g_autoptr(SeatXVNC) seat = NULL; seat = seat_xvnc_new (connection); set_seat_properties (SEAT (seat), NULL); display_manager_add_seat (display_manager, SEAT (seat)); - g_object_unref (seat); } static void @@ -321,7 +307,10 @@ start_display_manager (void) /* Start the XDMCP server */ if (config_get_boolean (config_get_instance (), "XDMCPServer", "enabled")) { - gchar *key_name, *key = NULL, *listen_address, *hostname; + g_autofree gchar *hostname = NULL; + g_autofree gchar *key_name = NULL; + g_autofree gchar *key = NULL; + g_autofree gchar *listen_address = NULL; xdmcp_server = xdmcp_server_new (); if (config_has_key (config_get_instance (), "XDMCPServer", "port")) @@ -333,19 +322,17 @@ start_display_manager (void) } listen_address = config_get_string (config_get_instance (), "XDMCPServer", "listen-address"); xdmcp_server_set_listen_address (xdmcp_server, listen_address); - g_free (listen_address); hostname = config_get_string (config_get_instance (), "XDMCPServer", "hostname"); xdmcp_server_set_hostname (xdmcp_server, hostname); - g_free (hostname); g_signal_connect (xdmcp_server, XDMCP_SERVER_SIGNAL_NEW_SESSION, G_CALLBACK (xdmcp_session_cb), NULL); key_name = config_get_string (config_get_instance (), "XDMCPServer", "key"); if (key_name) { - gchar *path; - GKeyFile *keys; + g_autofree gchar *path = NULL; + g_autoptr(GKeyFile) keys = NULL; gboolean result; - GError *error = NULL; + g_autoptr(GError) error = NULL; path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL); @@ -353,7 +340,6 @@ start_display_manager (void) result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error); if (error) g_warning ("Unable to load keys from %s: %s", path, error->message); - g_clear_error (&error); if (result) { @@ -362,13 +348,9 @@ start_display_manager (void) else g_warning ("Key %s not defined", key_name); } - g_free (path); - g_key_file_free (keys); } if (key) xdmcp_server_set_key (xdmcp_server, key); - g_free (key_name); - g_free (key); if (key_name && !key) { @@ -386,12 +368,12 @@ start_display_manager (void) /* Start the VNC server */ if (config_get_boolean (config_get_instance (), "VNCServer", "enabled")) { - gchar *path; + g_autofree gchar *path = NULL; path = g_find_program_in_path ("Xvnc"); if (path) { - gchar *listen_address; + g_autofree gchar *listen_address = NULL; vnc_server = vnc_server_new (); if (config_has_key (config_get_instance (), "VNCServer", "port")) @@ -403,13 +385,10 @@ start_display_manager (void) } listen_address = config_get_string (config_get_instance (), "VNCServer", "listen-address"); vnc_server_set_listen_address (vnc_server, listen_address); - g_free (listen_address); g_signal_connect (vnc_server, VNC_SERVER_SIGNAL_NEW_CONNECTION, G_CALLBACK (vnc_connection_cb), NULL); g_debug ("Starting VNC server on TCP/IP port %d", vnc_server_get_port (vnc_server)); vnc_server_start (vnc_server); - - g_free (path); } else g_warning ("Can't start VNC server, Xvnc is not in the path"); @@ -431,9 +410,10 @@ static gboolean add_login1_seat (Login1Seat *login1_seat) { const gchar *seat_name = login1_seat_get_id (login1_seat); - gchar **types = NULL, **type; + g_auto(GStrv) types = NULL; + gchar **type; GList *config_sections = NULL, *link; - Seat *seat = NULL; + g_autoptr(Seat) seat = NULL; gboolean is_seat0, started = FALSE; g_debug ("New seat added from logind: %s", seat_name); @@ -451,7 +431,6 @@ add_login1_seat (Login1Seat *login1_seat) for (type = types; !seat && type && *type; type++) seat = create_seat (*type, seat_name); - g_strfreev (types); if (seat) { @@ -476,7 +455,6 @@ add_login1_seat (Login1Seat *login1_seat) started = display_manager_add_seat (display_manager, seat); if (!started) g_debug ("Failed to start seat: %s", seat_name); - g_object_unref (seat); return started; } @@ -643,7 +621,7 @@ main (int argc, char **argv) N_("Show release version"), NULL }, { NULL } }; - GError *error = NULL; + g_autoptr(GError) error = NULL; /* Disable the SIGPIPE handler - this is a stupid Unix hangover behaviour. * We will handle pipes / sockets being closed instead of having the whole daemon be killed... @@ -675,7 +653,6 @@ main (int argc, char **argv) result = g_option_context_parse (option_context, &argc, &argv, &error); if (error) g_printerr ("%s\n", error->message); - g_clear_error (&error); g_option_context_free (option_context); if (!result) { @@ -689,7 +666,8 @@ main (int argc, char **argv) if (show_config) { GList *sources, *link; - gchar **groups, *last_source, *empty_source; + g_auto(GStrv) groups = NULL; + gchar *last_source, *empty_source; GHashTable *source_ids; int i; @@ -720,7 +698,7 @@ main (int argc, char **argv) groups = config_get_groups (config_get_instance ()); for (i = 0; groups[i]; i++) { - gchar **keys; + g_auto(GStrv) keys = NULL; int j; if (i != 0) @@ -731,18 +709,15 @@ main (int argc, char **argv) for (j = 0; keys && keys[j]; j++) { const gchar *source, *id; - gchar *value; + g_autofree gchar *value = NULL; source = config_get_source (config_get_instance (), groups[i], keys[j]); id = source ? g_hash_table_lookup (source_ids, source) : empty_source; value = config_get_string (config_get_instance (), groups[i], keys[j]); g_printerr ("%s %s=%s\n", id, keys[j], value); - g_free (value); } - g_strfreev (keys); } - g_strfreev (groups); /* Show mapping from source number to path */ g_printerr ("\n"); @@ -777,7 +752,7 @@ main (int argc, char **argv) /* If running inside an X server use Xephyr for display */ if (getenv ("DISPLAY") && getuid () != 0) { - gchar *x_server_path; + g_autofree gchar *x_server_path = NULL; x_server_path = g_find_program_in_path ("Xephyr"); if (!x_server_path) @@ -785,21 +760,19 @@ main (int argc, char **argv) g_printerr ("Running inside an X server requires Xephyr to be installed but it cannot be found. Please install it or update your PATH environment variable.\n"); return EXIT_FAILURE; } - g_free (x_server_path); } /* Make sure the system binary directory (where the greeters are installed) is in the path */ if (test_mode) { const gchar *path = g_getenv ("PATH"); - gchar *new_path; + g_autofree gchar *new_path = NULL; if (path) new_path = g_strdup_printf ("%s:%s", path, SBIN_DIR); else new_path = g_strdup (SBIN_DIR); g_setenv ("PATH", new_path, TRUE); - g_free (new_path); } /* Write PID file */ @@ -888,9 +861,9 @@ main (int argc, char **argv) config_set_string (config_get_instance (), "LightDM", "remote-sessions-directory", REMOTE_SESSIONS_DIR); if (!config_has_key (config_get_instance (), "LightDM", "greeters-directory")) { - GPtrArray *dirs; + g_autoptr(GPtrArray) dirs = NULL; const gchar * const *data_dirs; - gchar *value; + g_autofree gchar *value = NULL; int i; dirs = g_ptr_array_new_with_free_func (g_free); @@ -902,8 +875,6 @@ main (int argc, char **argv) g_ptr_array_add (dirs, NULL); value = g_strjoinv (":", (gchar **) dirs->pdata); config_set_string (config_get_instance (), "LightDM", "greeters-directory", value); - g_free (value); - g_ptr_array_unref (dirs); } if (!config_has_key (config_get_instance (), "XDMCPServer", "hostname")) config_set_string (config_get_instance (), "XDMCPServer", "hostname", g_get_host_name ()); @@ -985,9 +956,9 @@ main (int argc, char **argv) { if (config_get_boolean (config_get_instance (), "LightDM", "start-default-seat")) { - gchar **types; + g_auto(GStrv) types = NULL; gchar **type; - Seat *seat = NULL; + g_autoptr(Seat) seat = NULL; g_debug ("Adding default seat"); @@ -998,14 +969,12 @@ main (int argc, char **argv) if (seat) break; } - g_strfreev (types); if (seat) { set_seat_properties (seat, NULL); seat_set_property (seat, "exit-on-failure", "true"); if (!display_manager_add_seat (display_manager, seat)) return EXIT_FAILURE; - g_object_unref (seat); } else { diff --git a/src/log-file.c b/src/log-file.c index 36d1e8ff..edf5ac85 100644 --- a/src/log-file.c +++ b/src/log-file.c @@ -24,11 +24,10 @@ log_file_open (const gchar *log_filename, LogMode log_mode) if (log_mode == LOG_MODE_BACKUP_AND_TRUNCATE) { /* Move old file out of the way */ - gchar *old_filename; + g_autofree gchar *old_filename = NULL; old_filename = g_strdup_printf ("%s.old", log_filename); rename (log_filename, old_filename); - g_free (old_filename); open_flags |= O_TRUNC; } diff --git a/src/login1.c b/src/login1.c index 73d2d8b9..fdcb438a 100644 --- a/src/login1.c +++ b/src/login1.c @@ -72,6 +72,8 @@ struct Login1SeatPrivate G_DEFINE_TYPE (Login1Service, login1_service, G_TYPE_OBJECT); G_DEFINE_TYPE (Login1Seat, login1_seat, G_TYPE_OBJECT); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (Login1Seat, g_object_unref); + static Login1Service *singleton = NULL; Login1Service * @@ -119,8 +121,8 @@ seat_properties_changed_cb (GDBusConnection *connection, g_variant_iter_free (iter); while (g_variant_iter_loop (invalidated_properties, "&s", &name)) { - GVariant *result; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; result = g_dbus_connection_call_sync (connection, LOGIN1_SERVICE_NAME, @@ -135,13 +137,11 @@ seat_properties_changed_cb (GDBusConnection *connection, &error); if (error) g_warning ("Error updating seat property %s: %s", name, error->message); - g_clear_error (&error); if (result) { - g_variant_get (result, "(v)", &value); - update_property (seat, name, value); - g_variant_unref (value); - g_variant_unref (result); + g_autoptr(GVariant) v = NULL; + g_variant_get (result, "(v)", &v); + update_property (seat, name, v); } } g_variant_iter_free (invalidated_properties); @@ -151,8 +151,8 @@ static Login1Seat * add_seat (Login1Service *service, const gchar *id, const gchar *path) { Login1Seat *seat; - GVariant *result; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; seat = g_object_new (LOGIN1_SEAT_TYPE, NULL); seat->priv->connection = g_object_ref (service->priv->connection); @@ -184,7 +184,6 @@ add_seat (Login1Service *service, const gchar *id, const gchar *path) &error); if (error) g_warning ("Failed to get seat properties: %s", error->message); - g_clear_error (&error); if (result) { GVariantIter *properties; @@ -200,7 +199,6 @@ add_seat (Login1Service *service, const gchar *id, const gchar *path) seat->priv->can_multi_session = g_variant_get_boolean (value); } g_variant_iter_free (properties); - g_variant_unref (result); } service->priv->seats = g_list_append (service->priv->seats, seat); @@ -235,7 +233,7 @@ signal_cb (GDBusConnection *connection, else if (strcmp (signal_name, "SeatRemoved") == 0) { const gchar *id, *path; - Login1Seat *seat; + g_autoptr(Login1Seat) seat = NULL; g_variant_get (parameters, "(&s&o)", &id, &path); seat = login1_service_get_seat (service, id); @@ -243,7 +241,6 @@ signal_cb (GDBusConnection *connection, { service->priv->seats = g_list_remove (service->priv->seats, seat); g_signal_emit (service, service_signals[SEAT_REMOVED], 0, seat); - g_object_unref (seat); } } } @@ -251,10 +248,10 @@ signal_cb (GDBusConnection *connection, gboolean login1_service_connect (Login1Service *service) { - GVariant *result; + g_autoptr(GVariant) result = NULL; GVariantIter *seat_iter; const gchar *id, *path; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_return_val_if_fail (service != NULL, FALSE); @@ -264,7 +261,6 @@ login1_service_connect (Login1Service *service) service->priv->connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error) g_warning ("Failed to get system bus: %s", error->message); - g_clear_error (&error); if (!service->priv->connection) return FALSE; @@ -292,7 +288,6 @@ login1_service_connect (Login1Service *service) &error); if (error) g_warning ("Failed to get list of logind seats: %s", error->message); - g_clear_error (&error); if (!result) return FALSE; @@ -300,7 +295,6 @@ login1_service_connect (Login1Service *service) while (g_variant_iter_loop (seat_iter, "(&s&o)", &id, &path)) add_seat (service, id, path); g_variant_iter_free (seat_iter); - g_variant_unref (result); service->priv->connected = TRUE; @@ -341,133 +335,117 @@ login1_service_get_seat (Login1Service *service, const gchar *id) void login1_service_lock_session (Login1Service *service, const gchar *session_id) { - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; g_return_if_fail (service != NULL); g_return_if_fail (session_id != NULL); g_debug ("Locking login1 session %s", session_id); - if (session_id) - { - GVariant *result; + if (!session_id) + return; - result = g_dbus_connection_call_sync (service->priv->connection, - LOGIN1_SERVICE_NAME, - LOGIN1_OBJECT_NAME, - LOGIN1_MANAGER_INTERFACE_NAME, - "LockSession", - g_variant_new ("(s)", session_id), - G_VARIANT_TYPE ("()"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (error) - g_warning ("Error locking login1 session: %s", error->message); - g_clear_error (&error); - if (result) - g_variant_unref (result); - } + result = g_dbus_connection_call_sync (service->priv->connection, + LOGIN1_SERVICE_NAME, + LOGIN1_OBJECT_NAME, + LOGIN1_MANAGER_INTERFACE_NAME, + "LockSession", + g_variant_new ("(s)", session_id), + G_VARIANT_TYPE ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error locking login1 session: %s", error->message); } void login1_service_unlock_session (Login1Service *service, const gchar *session_id) { - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; g_return_if_fail (service != NULL); g_return_if_fail (session_id != NULL); g_debug ("Unlocking login1 session %s", session_id); - if (session_id) - { - GVariant *result; + if (!session_id) + return; - result = g_dbus_connection_call_sync (service->priv->connection, - LOGIN1_SERVICE_NAME, - LOGIN1_OBJECT_NAME, - LOGIN1_MANAGER_INTERFACE_NAME, - "UnlockSession", - g_variant_new ("(s)", session_id), - G_VARIANT_TYPE ("()"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (error) - g_warning ("Error unlocking login1 session: %s", error->message); - g_clear_error (&error); - if (result) - g_variant_unref (result); - } + result = g_dbus_connection_call_sync (service->priv->connection, + LOGIN1_SERVICE_NAME, + LOGIN1_OBJECT_NAME, + LOGIN1_MANAGER_INTERFACE_NAME, + "UnlockSession", + g_variant_new ("(s)", session_id), + G_VARIANT_TYPE ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error unlocking login1 session: %s", error->message); } void login1_service_activate_session (Login1Service *service, const gchar *session_id) { - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; g_return_if_fail (service != NULL); g_return_if_fail (session_id != NULL); g_debug ("Activating login1 session %s", session_id); - if (session_id) - { - GVariant *result; + if (!session_id) + return; - result = g_dbus_connection_call_sync (service->priv->connection, - LOGIN1_SERVICE_NAME, - LOGIN1_OBJECT_NAME, - LOGIN1_MANAGER_INTERFACE_NAME, - "ActivateSession", - g_variant_new ("(s)", session_id), - G_VARIANT_TYPE ("()"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (error) - g_warning ("Error activating login1 session: %s", error->message); - g_clear_error (&error); - if (result) - g_variant_unref (result); - } + result = g_dbus_connection_call_sync (service->priv->connection, + LOGIN1_SERVICE_NAME, + LOGIN1_OBJECT_NAME, + LOGIN1_MANAGER_INTERFACE_NAME, + "ActivateSession", + g_variant_new ("(s)", session_id), + G_VARIANT_TYPE ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error activating login1 session: %s", error->message); } void login1_service_terminate_session (Login1Service *service, const gchar *session_id) { - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; g_return_if_fail (service != NULL); g_return_if_fail (session_id != NULL); g_debug ("Terminating login1 session %s", session_id); - if (session_id) - { - GVariant *result; + if (!session_id) + return; - result = g_dbus_connection_call_sync (service->priv->connection, - LOGIN1_SERVICE_NAME, - LOGIN1_OBJECT_NAME, - LOGIN1_MANAGER_INTERFACE_NAME, - "TerminateSession", - g_variant_new ("(s)", session_id), - G_VARIANT_TYPE ("()"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (error) - g_warning ("Error terminating login1 session: %s", error->message); - g_clear_error (&error); - if (result) - g_variant_unref (result); - } + result = g_dbus_connection_call_sync (service->priv->connection, + LOGIN1_SERVICE_NAME, + LOGIN1_OBJECT_NAME, + LOGIN1_MANAGER_INTERFACE_NAME, + "TerminateSession", + g_variant_new ("(s)", session_id), + G_VARIANT_TYPE ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error terminating login1 session: %s", error->message); } static void @@ -547,10 +525,10 @@ login1_seat_finalize (GObject *object) { Login1Seat *self = LOGIN1_SEAT (object); - g_free (self->priv->id); - g_free (self->priv->path); + g_clear_pointer (&self->priv->id, g_free); + g_clear_pointer (&self->priv->path, g_free); g_dbus_connection_signal_unsubscribe (self->priv->connection, self->priv->signal_id); - g_object_unref (self->priv->connection); + g_clear_object (&self->priv->connection); G_OBJECT_CLASS (login1_seat_parent_class)->finalize (object); } diff --git a/src/plymouth.c b/src/plymouth.c index 03b31127..ec9377c8 100644 --- a/src/plymouth.c +++ b/src/plymouth.c @@ -24,18 +24,15 @@ static gboolean has_active_vt = FALSE; static gboolean plymouth_run_command (const gchar *command, gint *exit_status) { - gchar *command_line; + g_autofree gchar *command_line = NULL; gboolean result; - GError *error = NULL; + g_autoptr(GError) error = NULL; command_line = g_strdup_printf ("plymouth %s", command); result = g_spawn_command_line_sync (command_line, NULL, NULL, exit_status, &error); if (error) g_debug ("Could not run %s: %s", command_line, error->message); - g_clear_error (&error); - - g_free (command_line); return result; } diff --git a/src/process.c b/src/process.c index 4815e460..b6ff0c9f 100644 --- a/src/process.c +++ b/src/process.c @@ -177,13 +177,14 @@ gboolean process_start (Process *process, gboolean block) { gint argc; - gchar **argv; - gchar **env_keys, **env_values; + g_auto(GStrv) argv = NULL; + g_autofree gchar **env_keys = NULL; + g_autofree gchar **env_values = NULL; guint i, env_length; GList *keys, *link; pid_t pid; int log_fd = -1; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_return_val_if_fail (process != NULL, FALSE); g_return_val_if_fail (process->priv->command != NULL, FALSE); @@ -244,9 +245,6 @@ process_start (Process *process, gboolean block) } close (log_fd); - g_strfreev (argv); - g_free (env_keys); - g_free (env_values); if (pid < 0) { @@ -358,8 +356,8 @@ process_finalize (GObject *object) if (self->priv->pid > 0) g_hash_table_remove (processes, GINT_TO_POINTER (self->priv->pid)); - g_free (self->priv->log_file); - g_free (self->priv->command); + g_clear_pointer (&self->priv->log_file, g_free); + g_clear_pointer (&self->priv->command, g_free); g_hash_table_unref (self->priv->env); if (self->priv->quit_timeout) g_source_remove (self->priv->quit_timeout); diff --git a/src/process.h b/src/process.h index c358048a..11415630 100644 --- a/src/process.h +++ b/src/process.h @@ -43,6 +43,8 @@ typedef struct void (*stopped)(Process *process); } ProcessClass; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (Process, g_object_unref); + typedef void (*ProcessRunFunc)(Process *process, gpointer user_data); GType process_get_type (void); diff --git a/src/seat-local.c b/src/seat-local.c index 1eed76f1..e9433fef 100644 --- a/src/seat-local.c +++ b/src/seat-local.c @@ -100,10 +100,10 @@ seat_local_start (Seat *seat) key_name = seat_get_string_property (seat, "xdmcp-key"); if (key_name) { - gchar *path; - GKeyFile *keys; + g_autofree gchar *path = NULL; + g_autoptr(GKeyFile) keys = NULL; gboolean result; - GError *error = NULL; + g_autoptr(GError) error = NULL; path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL); @@ -111,11 +111,10 @@ seat_local_start (Seat *seat) result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error); if (error) l_debug (seat, "Error getting key %s", error->message); - g_clear_error (&error); if (result) { - gchar *key = NULL; + g_autofree gchar *key = NULL; if (g_key_file_has_key (keys, "keyring", key_name, NULL)) key = g_key_file_get_string (keys, "keyring", key_name, NULL); @@ -124,11 +123,7 @@ seat_local_start (Seat *seat) if (key) x_server_local_set_xdmcp_key (s->priv->xdmcp_x_server, key); - g_free (key); } - - g_free (path); - g_key_file_free (keys); } g_signal_connect (s->priv->xdmcp_x_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (xdmcp_x_server_stopped_cb), seat); @@ -221,8 +216,8 @@ create_x_server (SeatLocal *seat) { const gchar *x_server_backend; XServerLocal *x_server; - gchar *number; - XAuthority *cookie; + g_autofree gchar *number = NULL; + g_autoptr(XAuthority) cookie = NULL; const gchar *layout = NULL, *config_file = NULL; gboolean allow_tcp; gint vt; @@ -232,7 +227,7 @@ create_x_server (SeatLocal *seat) { UnitySystemCompositor *compositor; const gchar *command; - gchar *id; + g_autofree gchar *id = NULL; compositor = get_unity_system_compositor (SEAT_LOCAL (seat)); x_server = X_SERVER_LOCAL (x_server_xmir_new (compositor)); @@ -245,7 +240,6 @@ create_x_server (SeatLocal *seat) seat->priv->next_xmir_id++; x_server_xmir_set_mir_id (X_SERVER_XMIR (x_server), id); x_server_xmir_set_mir_socket (X_SERVER_XMIR (x_server), unity_system_compositor_get_socket (compositor)); - g_free (id); } else { @@ -274,8 +268,6 @@ create_x_server (SeatLocal *seat) number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server))); cookie = x_authority_new_local_cookie (number); x_server_set_authority (X_SERVER (x_server), cookie); - g_free (number); - g_object_unref (cookie); layout = seat_get_string_property (SEAT (seat), "xserver-layout"); if (layout) @@ -487,10 +479,8 @@ seat_local_finalize (GObject *object) g_clear_object (&seat->priv->compositor); g_clear_object (&seat->priv->active_compositor_session); if (seat->priv->xdmcp_x_server) - { g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat); - g_object_unref (seat->priv->xdmcp_x_server); - } + g_clear_object (&seat->priv->xdmcp_x_server); G_OBJECT_CLASS (seat_local_parent_class)->finalize (object); } diff --git a/src/seat-unity.c b/src/seat-unity.c index c9a5ad6b..f9d30526 100644 --- a/src/seat-unity.c +++ b/src/seat-unity.c @@ -93,10 +93,10 @@ compositor_ready_cb (UnitySystemCompositor *compositor, SeatUnity *seat) key_name = seat_get_string_property (SEAT (seat), "xdmcp-key"); if (key_name) { - gchar *path; - GKeyFile *keys; + g_autofree gchar *path = NULL; + g_autoptr(GKeyFile) keys = NULL; gboolean result; - GError *error = NULL; + g_autoptr(GError) error = NULL; path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL); @@ -104,11 +104,10 @@ compositor_ready_cb (UnitySystemCompositor *compositor, SeatUnity *seat) result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error); if (error) l_debug (seat, "Error getting key %s", error->message); - g_clear_error (&error); if (result) { - gchar *key = NULL; + g_autofree gchar *key = NULL; if (g_key_file_has_key (keys, "keyring", key_name, NULL)) key = g_key_file_get_string (keys, "keyring", key_name, NULL); @@ -117,11 +116,7 @@ compositor_ready_cb (UnitySystemCompositor *compositor, SeatUnity *seat) if (key) x_server_local_set_xdmcp_key (X_SERVER_LOCAL (seat->priv->xdmcp_x_server), key); - g_free (key); } - - g_free (path); - g_key_file_free (keys); } g_signal_connect (seat->priv->xdmcp_x_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (xdmcp_x_server_stopped_cb), seat); @@ -191,11 +186,11 @@ static XServerXmir * create_x_server (Seat *seat) { XServerXmir *x_server; - gchar *number; - XAuthority *cookie; + g_autoptr(XAuthority) cookie = NULL; const gchar *command = NULL, *layout = NULL, *config_file = NULL; + g_autofree gchar *id = NULL; + g_autofree gchar *number = NULL; gboolean allow_tcp; - gchar *id; l_debug (seat, "Starting X server on Unity compositor"); @@ -208,13 +203,10 @@ create_x_server (Seat *seat) SEAT_UNITY (seat)->priv->next_x_server_id++; x_server_xmir_set_mir_id (x_server, id); x_server_xmir_set_mir_socket (x_server, unity_system_compositor_get_socket (SEAT_UNITY (seat)->priv->compositor)); - g_free (id); number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server))); cookie = x_authority_new_local_cookie (number); x_server_set_authority (X_SERVER (x_server), cookie); - g_free (number); - g_object_unref (cookie); layout = seat_get_string_property (seat, "xserver-layout"); if (layout) @@ -270,9 +262,8 @@ seat_unity_create_greeter_session (Seat *seat) vt = display_server_get_vt (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor)); if (vt >= 0) { - gchar *value = g_strdup_printf ("%d", vt); + g_autofree gchar *value = g_strdup_printf ("%d", vt); session_set_env (SESSION (greeter_session), "XDG_VTNR", value); - g_free (value); } return greeter_session; @@ -290,9 +281,8 @@ seat_unity_create_session (Seat *seat) vt = display_server_get_vt (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor)); if (vt >= 0) { - gchar *value = g_strdup_printf ("%d", vt); + g_autofree gchar *value = g_strdup_printf ("%d", vt); session_set_env (SESSION (session), "XDG_VTNR", value); - g_free (value); } return session; diff --git a/src/seat-xdmcp-session.h b/src/seat-xdmcp-session.h index 5f244205..941083d4 100644 --- a/src/seat-xdmcp-session.h +++ b/src/seat-xdmcp-session.h @@ -33,6 +33,8 @@ typedef struct SeatClass parent_class; } SeatXDMCPSessionClass; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SeatXDMCPSession, g_object_unref); + GType seat_xdmcp_session_get_type (void); SeatXDMCPSession *seat_xdmcp_session_new (XDMCPSession *session); diff --git a/src/seat-xvnc.c b/src/seat-xvnc.c index 6026859e..61697e26 100644 --- a/src/seat-xvnc.c +++ b/src/seat-xvnc.c @@ -37,8 +37,8 @@ static DisplayServer * seat_xvnc_create_display_server (Seat *seat, Session *session) { XServerXVNC *x_server; - gchar *number; - XAuthority *cookie; + g_autofree gchar *number = NULL; + g_autoptr(XAuthority) cookie = NULL; const gchar *command = NULL; if (strcmp (session_get_session_type (session), "x") != 0) @@ -48,8 +48,6 @@ seat_xvnc_create_display_server (Seat *seat, Session *session) number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server))); cookie = x_authority_new_local_cookie (number); x_server_set_authority (X_SERVER (x_server), cookie); - g_free (number); - g_object_unref (cookie); x_server_xvnc_set_socket (x_server, g_socket_get_fd (SEAT_XVNC (seat)->priv->connection)); command = config_get_string (config_get_instance (), "VNCServer", "command"); @@ -81,7 +79,7 @@ seat_xvnc_run_script (Seat *seat, DisplayServer *display_server, Process *script { XServerXVNC *x_server; GInetSocketAddress *address; - gchar *hostname; + g_autofree gchar *hostname = NULL; const gchar *path; x_server = X_SERVER_XVNC (display_server); @@ -94,8 +92,6 @@ seat_xvnc_run_script (Seat *seat, DisplayServer *display_server, Process *script process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server))); process_set_env (script, "XAUTHORITY", path); - g_free (hostname); - SEAT_CLASS (seat_xvnc_parent_class)->run_script (seat, display_server, script); } diff --git a/src/seat-xvnc.h b/src/seat-xvnc.h index f96e590a..a87d7767 100644 --- a/src/seat-xvnc.h +++ b/src/seat-xvnc.h @@ -33,6 +33,8 @@ typedef struct SeatClass parent_class; } SeatXVNCClass; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SeatXVNC, g_object_unref); + GType seat_xvnc_get_type (void); SeatXVNC *seat_xvnc_new (GSocket *connection); @@ -354,7 +354,7 @@ seat_get_allow_guest (Seat *seat) static gboolean run_script (Seat *seat, DisplayServer *display_server, const gchar *script_name, User *user) { - Process *script; + g_autoptr(Process) script = NULL; gboolean result = FALSE; script = process_new (NULL, NULL); @@ -399,8 +399,6 @@ run_script (Seat *seat, DisplayServer *display_server, const gchar *script_name, } } - g_object_unref (script); - return result; } @@ -414,14 +412,13 @@ emit_upstart_signal (const gchar *signal) { g_return_if_fail (signal != NULL); g_return_if_fail (signal[0] != 0); - GSubprocess *p; + g_autoptr(GSubprocess) p = NULL; if (getuid () != 0) return; /* OK if it fails, probably not installed or not running upstart */ p = g_subprocess_new (G_SUBPROCESS_FLAGS_STDERR_SILENCE, NULL, "initctl", "-q", "emit", signal, "DISPLAY_MANAGER=lightdm", NULL); - g_object_unref (p); } static void @@ -930,7 +927,7 @@ get_session_argv (Seat *seat, SessionConfig *session_config, const gchar *sessio gboolean result; int argc; gchar **argv, *path; - GError *error = NULL; + g_autoptr(GError) error = NULL; /* If configured, run sessions through a wrapper */ if (session_wrapper) @@ -947,7 +944,6 @@ get_session_argv (Seat *seat, SessionConfig *session_config, const gchar *sessio result = g_shell_parse_argv (session_config_get_command (session_config), &argc, &argv, &error); if (error) l_debug (seat, "Invalid session command '%s': %s", session_config_get_command (session_config), error->message); - g_clear_error (&error); if (!result) return NULL; path = g_find_program_in_path (argv[0]); @@ -963,10 +959,8 @@ get_session_argv (Seat *seat, SessionConfig *session_config, const gchar *sessio static SessionConfig * find_session_config (Seat *seat, const gchar *sessions_dir, const gchar *session_name) { - gchar **dirs; - SessionConfig *session_config = NULL; + g_auto(GStrv) dirs = NULL; int i; - GError *error = NULL; g_return_val_if_fail (sessions_dir != NULL, NULL); g_return_val_if_fail (session_name != NULL, NULL); @@ -974,27 +968,25 @@ find_session_config (Seat *seat, const gchar *sessions_dir, const gchar *session dirs = g_strsplit (sessions_dir, ":", -1); for (i = 0; dirs[i]; i++) { - gchar *filename, *path; + g_autofree gchar *filename = NULL; + g_autofree gchar *path = NULL; const gchar *default_session_type = "x"; + SessionConfig *session_config; + g_autoptr(GError) error = NULL; if (strcmp (dirs[i], WAYLAND_SESSIONS_DIR) == 0) default_session_type = "wayland"; filename = g_strdup_printf ("%s.desktop", session_name); path = g_build_filename (dirs[i], filename, NULL); - g_free (filename); session_config = session_config_new_from_file (path, default_session_type, &error); - g_free (path); if (session_config) - break; - - if (dirs[i+1] == NULL) - l_debug (seat, "Failed to find session configuration %s", session_name); - g_clear_error (&error); + return session_config; } - g_strfreev (dirs); - return session_config; + l_debug (seat, "Failed to find session configuration %s", session_name); + + return NULL; } static void @@ -1024,11 +1016,12 @@ configure_session (Session *session, SessionConfig *config, const gchar *session static Session * create_user_session (Seat *seat, const gchar *username, gboolean autostart) { - User *user; - gchar *sessions_dir; + g_autoptr(User) user = NULL; + g_autofree gchar *sessions_dir = NULL; const gchar *session_name, *language; - SessionConfig *session_config; - Session *session = NULL; + g_autoptr(SessionConfig) session_config = NULL; + g_autoptr(Session) session = NULL; + g_auto(GStrv) argv = NULL; l_debug (seat, "Creating user session"); @@ -1054,26 +1047,20 @@ create_user_session (Seat *seat, const gchar *username, gboolean autostart) session_name = seat_get_string_property (seat, "user-session"); sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory"); session_config = find_session_config (seat, sessions_dir, session_name); - g_free (sessions_dir); - if (session_config) + if (!session_config) { - gchar **argv; - - session = create_session (seat, autostart); - configure_session (session, session_config, session_name, language); - session_set_username (session, username); - session_set_do_authenticate (session, TRUE); - argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper")); - session_set_argv (session, argv); - g_strfreev (argv); - g_object_unref (session_config); - } - else l_debug (seat, "Can't find session '%s'", session_name); + return NULL; + } - g_object_unref (user); + session = create_session (seat, autostart); + configure_session (session, session_config, session_name, language); + session_set_username (session, username); + session_set_do_authenticate (session, TRUE); + argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper")); + session_set_argv (session, argv); - return session; + return g_steal_pointer (&session); } static void @@ -1097,9 +1084,10 @@ static Session * create_guest_session (Seat *seat, const gchar *session_name) { const gchar *guest_wrapper; - gchar *sessions_dir, **argv; - SessionConfig *session_config; - Session *session; + g_autofree gchar *sessions_dir = NULL; + g_auto(GStrv) argv = NULL; + g_autoptr(SessionConfig) session_config = NULL; + g_autoptr(Session) session = NULL; if (!session_name) session_name = seat_get_string_property (seat, "guest-session"); @@ -1107,7 +1095,6 @@ create_guest_session (Seat *seat, const gchar *session_name) session_name = seat_get_string_property (seat, "user-session"); sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory"); session_config = find_session_config (seat, sessions_dir, session_name); - g_free (sessions_dir); if (!session_config) { l_debug (seat, "Can't find session '%s'", session_name); @@ -1129,10 +1116,8 @@ create_guest_session (Seat *seat, const gchar *session_name) } session_set_argv (session, argv); - g_strfreev (argv); - g_object_unref (session_config); - return session; + return g_steal_pointer (&session); } // FIXME: This is inefficient and we already know the greeter session when we set the callbacks... @@ -1184,10 +1169,10 @@ greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *sessi else { const gchar *language = NULL; - SessionConfig *session_config; + g_autoptr(SessionConfig) session_config = NULL; User *user; gchar *sessions_dir = NULL; - gchar **argv; + g_auto(GStrv) argv = NULL; session = greeter_take_authentication_session (greeter); @@ -1234,8 +1219,6 @@ greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *sessi configure_session (session, session_config, session_name, language); argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper")); session_set_argv (session, argv); - g_strfreev (argv); - g_object_unref (session_config); } /* Switch to this session when it is ready */ @@ -1291,7 +1274,8 @@ greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *sessi static GreeterSession * create_greeter_session (Seat *seat) { - gchar *sessions_dir, **argv; + gchar *sessions_dir; + g_auto(GStrv) argv = NULL; SessionConfig *session_config; GreeterSession *greeter_session; Greeter *greeter; @@ -1344,7 +1328,6 @@ create_greeter_session (Seat *seat) session_set_username (SESSION (greeter_session), user_get_name (accounts_get_current_user ())); } session_set_argv (SESSION (greeter_session), argv); - g_strfreev (argv); greeter_set_pam_services (greeter, seat_get_string_property (seat, "pam-service"), @@ -1380,8 +1363,6 @@ create_greeter_session (Seat *seat) greeter_set_hint (greeter, "autologin-guest", "true"); } - g_object_unref (session_config); - return greeter_session; } @@ -61,6 +61,8 @@ typedef struct void (*stopped)(Seat *seat); } SeatClass; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (Seat, g_object_unref); + GType seat_get_type (void); void seat_register_module (const gchar *name, GType type); diff --git a/src/session-child.c b/src/session-child.c index 28986d31..0d689330 100644 --- a/src/session-child.c +++ b/src/session-child.c @@ -258,7 +258,7 @@ session_child_run (int argc, char **argv) int i, version, fd, result; gboolean auth_complete = TRUE; User *user = NULL; - gchar *log_filename; + g_autofree gchar *log_filename = NULL; LogMode log_mode = LOG_MODE_BACKUP_AND_TRUNCATE; gsize env_length; gsize command_argc; @@ -278,7 +278,6 @@ session_child_run (int argc, char **argv) const gchar *login1_session_id = NULL; gchar *console_kit_cookie = NULL; const gchar *locale_value; - gchar *locale_var; static const gchar * const locale_var_names[] = { "LC_PAPER", "LC_NAME", @@ -299,7 +298,7 @@ session_child_run (int argc, char **argv) gid_t gid; uid_t uid; const gchar *home_directory; - GError *error = NULL; + g_autoptr(GError) error = NULL; #if !defined(GLIB_VERSION_2_36) g_type_init (); @@ -460,9 +459,8 @@ session_child_run (int argc, char **argv) { if ((locale_value = g_getenv (locale_var_names[i])) != NULL) { - locale_var = g_strdup_printf ("%s=%s", locale_var_names[i], locale_value); + g_autofree gchar *locale_var = g_strdup_printf ("%s=%s", locale_var_names[i], locale_value); pam_putenv (pam_handle, locale_var); - g_free (locale_var); } } } @@ -505,8 +503,7 @@ session_child_run (int argc, char **argv) { g_free (xdisplay); xdisplay = read_string (); - if (x_authority) - g_object_unref (x_authority); + g_clear_object (&x_authority); x_authority = read_xauth (); } read_data (&env_length, sizeof (env_length)); @@ -534,8 +531,7 @@ session_child_run (int argc, char **argv) fd = log_file_open (log_filename, log_mode); dup2 (fd, STDERR_FILENO); close (fd); - g_free (log_filename); - log_filename = NULL; + g_clear_pointer (&log_filename, g_free); } } else @@ -617,20 +613,16 @@ session_child_run (int argc, char **argv) write_string (console_kit_cookie); if (console_kit_cookie) { - gchar *value; - gchar *runtime_dir; + g_autofree gchar *value = NULL; + g_autofree gchar *runtime_dir = NULL; value = g_strdup_printf ("XDG_SESSION_COOKIE=%s", console_kit_cookie); pam_putenv (pam_handle, value); - g_free (value); runtime_dir = ck_get_xdg_runtime_dir (console_kit_cookie); if (runtime_dir) { - gchar *value; - value = g_strdup_printf ("XDG_RUNTIME_DIR=%s", runtime_dir); - pam_putenv (pam_handle, value); - g_free (value); - g_free (runtime_dir); + g_autofree gchar *v = g_strdup_printf ("XDG_RUNTIME_DIR=%s", runtime_dir); + pam_putenv (pam_handle, v); } } } @@ -639,8 +631,8 @@ session_child_run (int argc, char **argv) if (x_authority) { gboolean drop_privileges, result; - gchar *value; - GError *error = NULL; + g_autofree gchar *value = NULL; + g_autoptr(GError) error = NULL; drop_privileges = geteuid () == 0; if (drop_privileges) @@ -651,7 +643,6 @@ session_child_run (int argc, char **argv) if (error) g_printerr ("Error writing X authority: %s\n", error->message); - g_clear_error (&error); if (!result) { pam_end (pam_handle, 0); @@ -660,7 +651,6 @@ session_child_run (int argc, char **argv) value = g_strdup_printf ("XAUTHORITY=%s", x_authority_filename); pam_putenv (pam_handle, value); - g_free (value); } /* Catch terminate signal and pass it to the child */ @@ -804,7 +794,7 @@ session_child_run (int argc, char **argv) if (x_authority) { gboolean drop_privileges, result; - GError *error = NULL; + g_autoptr(GError) error = NULL; drop_privileges = geteuid () == 0; if (drop_privileges) @@ -815,7 +805,6 @@ session_child_run (int argc, char **argv) if (error) g_printerr ("Error removing X authority: %s\n", error->message); - g_clear_error (&error); if (!result) _exit (EXIT_FAILURE); } diff --git a/src/session-config.c b/src/session-config.c index c0476d15..67012c47 100644 --- a/src/session-config.c +++ b/src/session-config.c @@ -113,9 +113,9 @@ session_config_finalize (GObject *object) { SessionConfig *self = SESSION_CONFIG (object); - g_free (self->priv->session_type); - g_strfreev (self->priv->desktop_names); - g_free (self->priv->command); + g_clear_pointer (&self->priv->session_type, g_free); + g_clear_pointer (&self->priv->desktop_names, g_strfreev); + g_clear_pointer (&self->priv->command, g_free); G_OBJECT_CLASS (session_config_parent_class)->finalize (object); } diff --git a/src/session-config.h b/src/session-config.h index 8a86a5fb..fac72eb5 100644 --- a/src/session-config.h +++ b/src/session-config.h @@ -34,6 +34,8 @@ typedef struct GObjectClass parent_class; } SessionConfigClass; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SessionConfig, g_object_unref); + GType session_config_get_type (void); SessionConfig *session_config_new_from_file (const gchar *filename, const gchar *default_session_type, GError **error); diff --git a/src/session.c b/src/session.c index 9b31bcdc..0b3e839c 100644 --- a/src/session.c +++ b/src/session.c @@ -222,10 +222,8 @@ session_set_display_server (Session *session, DisplayServer *display_server) return; if (session->priv->display_server) - { display_server_disconnect_session (session->priv->display_server, session); - g_object_unref (session->priv->display_server); - } + g_clear_object (&session->priv->display_server); session->priv->display_server = g_object_ref (display_server); } @@ -473,7 +471,7 @@ static gboolean from_child_cb (GIOChannel *source, GIOCondition condition, gpointer data) { Session *session = data; - gchar *username; + g_autofree gchar *username = NULL; ssize_t n_read; gboolean auth_complete; @@ -489,11 +487,9 @@ from_child_cb (GIOChannel *source, GIOCondition condition, gpointer data) if (g_strcmp0 (username, session->priv->username) != 0) { g_free (session->priv->username); - session->priv->username = username; + session->priv->username = g_steal_pointer (&username); g_clear_object (&session->priv->user); } - else - g_free (username); /* Check if authentication completed */ n_read = read_from_child (session, &auth_complete, sizeof (auth_complete)); @@ -570,7 +566,8 @@ session_real_start (Session *session) int version; int to_child_pipe[2], from_child_pipe[2]; int to_child_output, from_child_input; - gchar *arg0, *arg1; + g_autofree gchar *arg0 = NULL; + g_autofree gchar *arg1 = NULL; g_return_val_if_fail (session->priv->pid == 0, FALSE); @@ -615,8 +612,6 @@ session_real_start (Session *session) arg0, arg1, NULL); _exit (EXIT_FAILURE); } - g_free (arg0); - g_free (arg1); if (session->priv->pid < 0) { @@ -764,7 +759,8 @@ static void session_real_run (Session *session) { gsize i, argc; - gchar *command, *x_authority_filename; + g_autofree gchar *command = NULL; + g_autofree gchar *x_authority_filename = NULL; GList *link; g_return_if_fail (session != NULL); @@ -779,16 +775,15 @@ session_real_run (Session *session) command = g_strjoinv (" ", session->priv->argv); l_debug (session, "Running command %s", command); - g_free (command); /* Create authority location */ if (session->priv->x_authority_use_system_location) { - gchar *run_dir, *dir; + g_autofree gchar *run_dir = NULL; + g_autofree gchar *dir = NULL; run_dir = config_get_string (config_get_instance (), "LightDM", "run-directory"); dir = g_build_filename (run_dir, session->priv->username, NULL); - g_free (run_dir); if (g_mkdir_with_parents (dir, S_IRWXU) < 0) l_warning (session, "Failed to set create system authority dir %s: %s", dir, strerror (errno)); @@ -799,7 +794,6 @@ session_real_run (Session *session) } x_authority_filename = g_build_filename (dir, "xauthority", NULL); - g_free (dir); } else x_authority_filename = g_build_filename (user_get_home_directory (session_get_user (session)), ".Xauthority", NULL); @@ -807,23 +801,21 @@ session_real_run (Session *session) /* Make sure shared user directory for this user exists */ if (!session->priv->remote_host_name) { - gchar *data_dir = shared_data_manager_ensure_user_dir (shared_data_manager_get_instance (), session->priv->username); + g_autofree gchar *data_dir = shared_data_manager_ensure_user_dir (shared_data_manager_get_instance (), session->priv->username); if (data_dir) - { session_set_env (session, "XDG_GREETER_DATA_DIR", data_dir); - g_free (data_dir); - } } /* Open socket to allow in-session greeter */ if (session->priv->config && session_config_get_allow_greeter (session->priv->config)) { - gchar *run_dir, *dir, *path; - GError *error = NULL; + g_autofree gchar *run_dir = NULL; + g_autofree gchar *dir = NULL; + g_autofree gchar *path = NULL; + g_autoptr(GError) error = NULL; run_dir = config_get_string (config_get_instance (), "LightDM", "run-directory"); dir = g_build_filename (run_dir, session->priv->username, NULL); - g_free (run_dir); if (g_mkdir_with_parents (dir, S_IRWXU) < 0) l_warning (session, "Failed to create greeter socket dir %s: %s", dir, strerror (errno)); @@ -837,14 +829,9 @@ session_real_run (Session *session) session->priv->greeter_socket = greeter_socket_new (path); g_signal_connect (session->priv->greeter_socket, GREETER_SOCKET_SIGNAL_CREATE_GREETER, G_CALLBACK (create_greeter_cb), session); session_set_env (session, "LIGHTDM_GREETER_PIPE", path); - g_free (path); - g_free (dir); if (!greeter_socket_start (session->priv->greeter_socket, &error)) - { l_warning (session, "Failed to start greeter socket: %s\n", error->message); - g_clear_error (&error); - } } if (session->priv->log_filename) @@ -853,7 +840,6 @@ session_real_run (Session *session) write_data (session, &session->priv->log_mode, sizeof (session->priv->log_mode)); write_string (session, session->priv->tty); write_string (session, x_authority_filename); - g_free (x_authority_filename); write_string (session, session->priv->xdisplay); write_xauth (session, session->priv->x_authority); argc = g_list_length (session->priv->env); @@ -987,28 +973,27 @@ session_finalize (GObject *object) kill (self->priv->pid, SIGKILL); close (self->priv->to_child_input); close (self->priv->from_child_output); - if (self->priv->from_child_channel) - g_io_channel_unref (self->priv->from_child_channel); + g_clear_pointer (&self->priv->from_child_channel, g_io_channel_unref); if (self->priv->from_child_watch) g_source_remove (self->priv->from_child_watch); if (self->priv->child_watch) g_source_remove (self->priv->child_watch); - g_free (self->priv->username); + g_clear_pointer (&self->priv->username, g_free); g_clear_object (&self->priv->user); - g_free (self->priv->pam_service); + g_clear_pointer (&self->priv->pam_service, g_free); for (i = 0; i < self->priv->messages_length; i++) g_free ((char *) self->priv->messages[i].msg); - g_free (self->priv->messages); - g_free (self->priv->authentication_result_string); - g_free (self->priv->log_filename); - g_free (self->priv->tty); - g_free (self->priv->xdisplay); + g_clear_pointer (&self->priv->messages, g_free); + g_clear_pointer (&self->priv->authentication_result_string, g_free); + g_clear_pointer (&self->priv->log_filename, g_free); + g_clear_pointer (&self->priv->tty, g_free); + g_clear_pointer (&self->priv->xdisplay, g_free); g_clear_object (&self->priv->x_authority); - g_free (self->priv->remote_host_name); - g_free (self->priv->login1_session_id); - g_free (self->priv->console_kit_cookie); + g_clear_pointer (&self->priv->remote_host_name, g_free); + g_clear_pointer (&self->priv->login1_session_id, g_free); + g_clear_pointer (&self->priv->console_kit_cookie, g_free); g_list_free_full (self->priv->env, g_free); - g_strfreev (self->priv->argv); + g_clear_pointer (&self->priv->argv, g_strfreev); G_OBJECT_CLASS (session_parent_class)->finalize (object); } diff --git a/src/session.h b/src/session.h index 320ac496..aa883a9d 100644 --- a/src/session.h +++ b/src/session.h @@ -66,6 +66,8 @@ typedef struct void (*stopped)(Session *session); } SessionClass; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (Session, g_object_unref); + GType session_get_type (void); Session *session_new (void); diff --git a/src/shared-data-manager.c b/src/shared-data-manager.c index fd7427e3..8b106921 100644 --- a/src/shared-data-manager.c +++ b/src/shared-data-manager.c @@ -56,35 +56,33 @@ static void delete_unused_user (gpointer key, gpointer value, gpointer user_data) { const gchar *user = (const gchar *)key; - GError *error = NULL; + g_autofree gchar *path = NULL; + g_autofree gchar *quoted_path = NULL; + g_autofree gchar *cmd = NULL; + g_autoptr(GError) error = NULL; /* For this operation, we just need a fire and forget rm -rf. Since recursively deleting in GIO is a huge pain in the butt, we'll just drop to shell for this. */ - gchar *path = g_build_filename (USERS_DIR, user, NULL); - gchar *quoted_path = g_shell_quote (path); - gchar *cmd = g_strdup_printf ("/bin/rm -rf %s", quoted_path); + path = g_build_filename (USERS_DIR, user, NULL); + quoted_path = g_shell_quote (path); + cmd = g_strdup_printf ("/bin/rm -rf %s", quoted_path); g_spawn_command_line_async (cmd, &error); if (error) g_warning ("Could not delete unused user data directory %s: %s", path, error->message); - g_clear_error (&error); - - g_free (cmd); - g_free (quoted_path); - g_free (path); } gchar * shared_data_manager_ensure_user_dir (SharedDataManager *manager, const gchar *user) { struct passwd *entry; - gchar *path; - GFile *file; + g_autofree gchar *path = NULL; + g_autoptr(GFile) file = NULL; gboolean result; - GFileInfo *info; - GError *error = NULL; + g_autoptr(GFileInfo) info = NULL; + g_autoptr(GError) error = NULL; entry = getpwnam (user); if (!entry) @@ -103,11 +101,9 @@ shared_data_manager_ensure_user_dir (SharedDataManager *manager, const gchar *us else g_warning ("Could not create user data directory %s: %s", path, error->message); } - g_clear_error (&error); if (!result) { - g_object_unref (file); - g_free (path); + g_clear_pointer (&path, g_free); return NULL; } @@ -119,17 +115,12 @@ shared_data_manager_ensure_user_dir (SharedDataManager *manager, const gchar *us g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, manager->priv->greeter_gid); g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, 0770); result = g_file_set_attributes_from_info (file, info, G_FILE_QUERY_INFO_NONE, NULL, &error); - g_object_unref (info); - g_object_unref (file); if (error) g_warning ("Could not chown user data directory %s: %s", path, error->message); if (!result) - { - g_free (path); return NULL; - } - return path; + return g_steal_pointer (&path); } static void @@ -138,13 +129,12 @@ next_user_dirs_cb (GObject *object, GAsyncResult *res, gpointer user_data) GFileEnumerator *enumerator = G_FILE_ENUMERATOR (object); SharedDataManager *manager = SHARED_DATA_MANAGER (user_data); GList *link; - GError *error = NULL; + g_autoptr(GError) error = NULL; GList *files = g_file_enumerator_next_files_finish (enumerator, res, &error); if (error) { g_warning ("Could not enumerate user data directory %s: %s", USERS_DIR, error->message); - g_clear_error (&error); g_object_unref (manager); return; } @@ -185,12 +175,11 @@ list_user_dirs_cb (GObject *object, GAsyncResult *res, gpointer user_data) GFile *file = G_FILE (object); SharedDataManager *manager = SHARED_DATA_MANAGER (user_data); GFileEnumerator *enumerator; - GError *error = NULL; + g_autoptr(GError) error = NULL; enumerator = g_file_enumerate_children_finish (file, res, &error); if (error) g_warning ("Could not enumerate user data directory %s: %s", USERS_DIR, error->message); - g_clear_error (&error); if (!enumerator) { g_object_unref (manager); @@ -212,7 +201,7 @@ user_removed_cb (CommonUserList *list, CommonUser *user, SharedDataManager *mana void shared_data_manager_start (SharedDataManager *manager) { - GFile *file; + g_autoptr(GFile) file = NULL; /* Grab list of all current directories, so we know if any exist that we no longer need. */ file = g_file_new_for_path (USERS_DIR); @@ -220,7 +209,6 @@ shared_data_manager_start (SharedDataManager *manager) G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT, NULL, list_user_dirs_cb, g_object_ref (manager)); - g_object_unref (file); /* And listen for user removals. */ g_signal_connect (common_user_list_get_instance (), USER_LIST_SIGNAL_USER_REMOVED, G_CALLBACK (user_removed_cb), manager); @@ -252,7 +240,7 @@ shared_data_manager_finalize (GObject *object) if (self->priv->starting_dirs) g_hash_table_destroy (self->priv->starting_dirs); - g_free (self->priv->greeter_user); + g_clear_pointer (&self->priv->greeter_user, g_free); G_OBJECT_CLASS (shared_data_manager_parent_class)->finalize (object); } diff --git a/src/unity-system-compositor.c b/src/unity-system-compositor.c index 83eacbac..815be498 100644 --- a/src/unity-system-compositor.c +++ b/src/unity-system-compositor.c @@ -136,7 +136,7 @@ unity_system_compositor_set_timeout (UnitySystemCompositor *compositor, gint tim static void write_message (UnitySystemCompositor *compositor, guint16 id, const guint8 *payload, guint16 payload_length) { - guint8 *data; + g_autofree guint8 *data = NULL; gsize data_length = 4 + payload_length; data = g_malloc (data_length); @@ -150,8 +150,6 @@ write_message (UnitySystemCompositor *compositor, guint16 id, const guint8 *payl errno = 0; if (write (compositor->priv->to_compositor_pipe[1], data, data_length) != data_length) l_warning (compositor, "Failed to write to compositor: %s", strerror (errno)); - - g_free (data); } void @@ -187,7 +185,8 @@ unity_system_compositor_connect_session (DisplayServer *display_server, Session if (!session_get_env (session, "MIR_SERVER_NAME")) { - gchar *name; + g_autofree gchar *name = NULL; + if (IS_GREETER_SESSION (session)) { name = g_strdup_printf ("greeter-%d", compositor->priv->next_greeter_id); @@ -199,14 +198,12 @@ unity_system_compositor_connect_session (DisplayServer *display_server, Session compositor->priv->next_session_id++; } session_set_env (session, "MIR_SERVER_NAME", name); - g_free (name); } if (compositor->priv->vt >= 0) { - gchar *value = g_strdup_printf ("%d", compositor->priv->vt); + g_autofree gchar *value = g_strdup_printf ("%d", compositor->priv->vt); session_set_env (session, "XDG_VTNR", value); - g_free (value); } } @@ -222,8 +219,9 @@ unity_system_compositor_disconnect_session (DisplayServer *display_server, Sessi static gchar * get_absolute_command (const gchar *command) { - gchar **tokens; - gchar *absolute_binary, *absolute_command = NULL; + g_auto(GStrv) tokens = NULL; + g_autofree gchar *absolute_binary = NULL; + gchar *absolute_command = NULL; tokens = g_strsplit (command, " ", 2); @@ -235,9 +233,6 @@ get_absolute_command (const gchar *command) else absolute_command = g_strdup (absolute_binary); } - g_free (absolute_binary); - - g_strfreev (tokens); return absolute_command; } @@ -271,7 +266,7 @@ read_cb (GIOChannel *source, GIOCondition condition, gpointer data) { gsize n_total, n_read = 0; GIOStatus status; - GError *error = NULL; + g_autoptr(GError) error = NULL; n_total = compositor->priv->read_buffer_n_used + n_to_read; if (compositor->priv->read_buffer_length < n_total) @@ -286,7 +281,6 @@ read_cb (GIOChannel *source, GIOCondition condition, gpointer data) l_warning (compositor, "Failed to read from compositor: %s", error->message); if (status != G_IO_STATUS_NORMAL) return TRUE; - g_clear_error (&error); compositor->priv->read_buffer_n_used += n_read; } @@ -383,8 +377,11 @@ unity_system_compositor_start (DisplayServer *server) { UnitySystemCompositor *compositor = UNITY_SYSTEM_COMPOSITOR (server); gboolean result, backup_logs; - GString *command; - gchar *dir, *log_file, *absolute_command, *value; + g_autoptr(GString) command = NULL; + g_autofree gchar *dir = NULL; + g_autofree gchar *log_file = NULL; + g_autofree gchar *value = NULL; + g_autofree gchar *absolute_command = NULL; g_return_val_if_fail (compositor->priv->process == NULL, FALSE); @@ -411,18 +408,15 @@ unity_system_compositor_start (DisplayServer *server) dir = config_get_string (config_get_instance (), "LightDM", "log-directory"); log_file = g_build_filename (dir, "unity-system-compositor.log", NULL); l_debug (compositor, "Logging to %s", log_file); - g_free (dir); /* Setup environment */ compositor->priv->process = process_new (run_cb, compositor); backup_logs = config_get_boolean (config_get_instance (), "LightDM", "backup-logs"); process_set_log_file (compositor->priv->process, log_file, TRUE, backup_logs ? LOG_MODE_BACKUP_AND_TRUNCATE : LOG_MODE_APPEND); - g_free (log_file); process_set_clear_environment (compositor->priv->process, TRUE); process_set_env (compositor->priv->process, "XDG_SEAT", "seat0"); value = g_strdup_printf ("%d", compositor->priv->vt); process_set_env (compositor->priv->process, "XDG_VTNR", value); - g_free (value); /* Variable required for regression tests */ if (g_getenv ("LIGHTDM_TEST_ROOT")) { @@ -439,13 +433,11 @@ unity_system_compositor_start (DisplayServer *server) return FALSE; } command = g_string_new (absolute_command); - g_free (absolute_command); g_string_append_printf (command, " --file '%s'", compositor->priv->socket); g_string_append_printf (command, " --from-dm-fd %d --to-dm-fd %d", compositor->priv->to_compositor_pipe[0], compositor->priv->from_compositor_pipe[1]); if (compositor->priv->vt > 0) g_string_append_printf (command, " --vt %d", compositor->priv->vt); process_set_command (compositor->priv->process, command->str); - g_string_free (command, TRUE); /* Start the compositor */ g_signal_connect (compositor->priv->process, PROCESS_SIGNAL_STOPPED, G_CALLBACK (stopped_cb), compositor); @@ -496,22 +488,20 @@ unity_system_compositor_finalize (GObject *object) UnitySystemCompositor *self = UNITY_SYSTEM_COMPOSITOR (object); if (self->priv->process) - { g_signal_handlers_disconnect_matched (self->priv->process, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self); - g_object_unref (self->priv->process); - } - g_free (self->priv->command); - g_free (self->priv->socket); + g_clear_object (&self->priv->process); + g_clear_pointer (&self->priv->command, g_free); + g_clear_pointer (&self->priv->socket, g_free); if (self->priv->have_vt_ref) vt_unref (self->priv->vt); close (self->priv->to_compositor_pipe[0]); close (self->priv->to_compositor_pipe[1]); close (self->priv->from_compositor_pipe[0]); close (self->priv->from_compositor_pipe[1]); - g_io_channel_unref (self->priv->from_compositor_channel); + g_clear_pointer (&self->priv->from_compositor_channel, g_io_channel_unref); if (self->priv->from_compositor_watch) g_source_remove (self->priv->from_compositor_watch); - g_free (self->priv->read_buffer); + g_clear_pointer (&self->priv->read_buffer, g_free); if (self->priv->timeout_source) g_source_remove (self->priv->timeout_source); diff --git a/src/vnc-server.c b/src/vnc-server.c index 914a5ebd..1b29f93b 100644 --- a/src/vnc-server.c +++ b/src/vnc-server.c @@ -72,23 +72,21 @@ vnc_server_get_listen_address (VNCServer *server) static gboolean read_cb (GSocket *socket, GIOCondition condition, VNCServer *server) { - GError *error = NULL; GSocket *client_socket; + g_autoptr(GError) error = NULL; client_socket = g_socket_accept (socket, NULL, &error); if (error) g_warning ("Failed to get connection from from VNC socket: %s", error->message); - g_clear_error (&error); if (client_socket) { GInetSocketAddress *address; - gchar *hostname; + g_autofree gchar *hostname = NULL; address = G_INET_SOCKET_ADDRESS (g_socket_get_remote_address (client_socket, NULL)); hostname = g_inet_address_to_string (g_inet_socket_address_get_address (address)); g_debug ("Got VNC connection from %s:%d", hostname, g_inet_socket_address_get_port (address)); - g_free (hostname); g_signal_emit (server, signals[NEW_CONNECTION], 0, client_socket); } @@ -99,8 +97,8 @@ read_cb (GSocket *socket, GIOCondition condition, VNCServer *server) static GSocket * open_tcp_socket (GSocketFamily family, guint port, const gchar *listen_address, GError **error) { - GSocket *socket; - GSocketAddress *address; + g_autoptr(GSocket) socket = NULL; + g_autoptr(GSocketAddress) address = NULL; socket = g_socket_new (family, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, error); if (!socket) @@ -112,10 +110,7 @@ open_tcp_socket (GSocketFamily family, guint port, const gchar *listen_address, addresses = g_resolver_lookup_by_name (g_resolver_get_default (), listen_address, NULL, error); if (!addresses) - { - g_object_unref (socket); return NULL; - } address = g_inet_socket_address_new (addresses->data, port); g_resolver_free_addresses (addresses); } @@ -123,26 +118,23 @@ open_tcp_socket (GSocketFamily family, guint port, const gchar *listen_address, address = g_inet_socket_address_new (g_inet_address_new_any (family), port); if (!g_socket_bind (socket, address, TRUE, error) || !g_socket_listen (socket, error)) - { - g_object_unref (socket); return NULL; - } - return socket; + return g_steal_pointer (&socket); } gboolean vnc_server_start (VNCServer *server) { GSource *source; - GError *error = NULL; + g_autoptr(GError) ipv4_error = NULL; + g_autoptr(GError) ipv6_error = NULL; g_return_val_if_fail (server != NULL, FALSE); - server->priv->socket = open_tcp_socket (G_SOCKET_FAMILY_IPV4, server->priv->port, server->priv->listen_address, &error); - if (error) - g_warning ("Failed to create IPv4 VNC socket: %s", error->message); - g_clear_error (&error); + server->priv->socket = open_tcp_socket (G_SOCKET_FAMILY_IPV4, server->priv->port, server->priv->listen_address, &ipv4_error); + if (ipv4_error) + g_warning ("Failed to create IPv4 VNC socket: %s", ipv4_error->message); if (server->priv->socket) { @@ -151,10 +143,9 @@ vnc_server_start (VNCServer *server) g_source_attach (source, NULL); } - server->priv->socket6 = open_tcp_socket (G_SOCKET_FAMILY_IPV6, server->priv->port, server->priv->listen_address, &error); - if (error) - g_warning ("Failed to create IPv6 VNC socket: %s", error->message); - g_clear_error (&error); + server->priv->socket6 = open_tcp_socket (G_SOCKET_FAMILY_IPV6, server->priv->port, server->priv->listen_address, &ipv6_error); + if (ipv6_error) + g_warning ("Failed to create IPv6 VNC socket: %s", ipv6_error->message); if (server->priv->socket6) { @@ -181,7 +172,7 @@ vnc_server_finalize (GObject *object) { VNCServer *self = VNC_SERVER (object); - g_free (self->priv->listen_address); + g_clear_pointer (&self->priv->listen_address, g_free); g_clear_object (&self->priv->socket); g_clear_object (&self->priv->socket6); diff --git a/src/wayland-session.c b/src/wayland-session.c index 639893ea..331ad252 100644 --- a/src/wayland-session.c +++ b/src/wayland-session.c @@ -59,9 +59,8 @@ wayland_session_connect_session (DisplayServer *display_server, Session *session if (wayland_session->priv->vt >= 0) { - gchar *value = g_strdup_printf ("%d", wayland_session->priv->vt); + g_autofree gchar *value = g_strdup_printf ("%d", wayland_session->priv->vt); session_set_env (session, "XDG_VTNR", value); - g_free (value); } } diff --git a/src/x-authority.c b/src/x-authority.c index 2f919e1f..f74c245a 100644 --- a/src/x-authority.c +++ b/src/x-authority.c @@ -246,10 +246,9 @@ write_string (int fd, const gchar *value) gboolean x_authority_write (XAuthority *auth, XAuthWriteMode mode, const gchar *filename, GError **error) { - gchar *input = NULL; + g_autofree gchar *input = NULL; gsize input_length = 0, input_offset = 0; GList *link, *records = NULL; - XAuthority *a; gboolean result = TRUE; gboolean matched = FALSE; int output_fd; @@ -260,15 +259,15 @@ x_authority_write (XAuthority *auth, XAuthWriteMode mode, const gchar *filename, /* Read out existing records */ if (mode != XAUTH_WRITE_MODE_SET) { - GError *read_error = NULL; + g_autoptr(GError) read_error = NULL; g_file_get_contents (filename, &input, &input_length, &read_error); if (read_error && !g_error_matches (read_error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) g_warning ("Error reading existing Xauthority: %s", read_error->message); - g_clear_error (&read_error); } while (input_offset != input_length) { + g_autoptr(XAuthority) a = NULL; gboolean address_matches = FALSE; guint16 address_length = 0; guint16 authorization_data_length = 0; @@ -286,10 +285,7 @@ x_authority_write (XAuthority *auth, XAuthWriteMode mode, const gchar *filename, a->priv->authorization_data_length = authorization_data_length; if (!result) - { - g_object_unref (a); break; - } if (auth->priv->address_length == a->priv->address_length) { @@ -306,17 +302,13 @@ x_authority_write (XAuthority *auth, XAuthWriteMode mode, const gchar *filename, { matched = TRUE; if (mode == XAUTH_WRITE_MODE_REMOVE) - { - g_object_unref (a); continue; - } else x_authority_set_authorization_data (a, auth->priv->authorization_data, auth->priv->authorization_data_length); } - records = g_list_append (records, a); + records = g_list_append (records, g_steal_pointer (&a)); } - g_free (input); /* If didn't exist, then add a new one */ if (!matched) @@ -349,10 +341,8 @@ x_authority_write (XAuthority *auth, XAuthWriteMode mode, const gchar *filename, write_string (output_fd, a->priv->authorization_name) && write_uint16 (output_fd, a->priv->authorization_data_length) && write_data (output_fd, a->priv->authorization_data, a->priv->authorization_data_length); - - g_object_unref (a); } - g_list_free (records); + g_list_free_full (records, g_object_unref); fsync (output_fd); close (output_fd); @@ -383,10 +373,10 @@ x_authority_finalize (GObject *object) { XAuthority *self = X_AUTHORITY (object); - g_free (self->priv->address); - g_free (self->priv->number); - g_free (self->priv->authorization_name); - g_free (self->priv->authorization_data); + g_clear_pointer (&self->priv->address, g_free); + g_clear_pointer (&self->priv->number, g_free); + g_clear_pointer (&self->priv->authorization_name, g_free); + g_clear_pointer (&self->priv->authorization_data, g_free); G_OBJECT_CLASS (x_authority_parent_class)->finalize (object); } diff --git a/src/x-authority.h b/src/x-authority.h index d908422f..7e2c0108 100644 --- a/src/x-authority.h +++ b/src/x-authority.h @@ -32,6 +32,8 @@ typedef struct GObjectClass parent_class; } XAuthorityClass; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (XAuthority, g_object_unref); + #define XAUTH_FAMILY_INTERNET 0 #define XAUTH_FAMILY_DECNET 1 #define XAUTH_FAMILY_CHAOS 2 diff --git a/src/x-server-local.c b/src/x-server-local.c index 8c28b382..5597085c 100644 --- a/src/x-server-local.c +++ b/src/x-server-local.c @@ -91,9 +91,9 @@ find_version (const gchar *line) const gchar * x_server_local_get_version (void) { - gchar *stderr_text; + g_autofree gchar *stderr_text = NULL; gint exit_status; - gchar **tokens; + g_auto(GStrv) tokens = NULL; guint n_tokens; if (version) @@ -103,21 +103,18 @@ x_server_local_get_version (void) return NULL; if (exit_status == EXIT_SUCCESS) { - gchar **lines; + g_auto(GStrv) lines = NULL; int i; lines = g_strsplit (stderr_text, "\n", -1); for (i = 0; lines[i] && !version; i++) version = find_version (lines[i]); - g_strfreev (lines); } - g_free (stderr_text); tokens = g_strsplit (version, ".", 3); n_tokens = g_strv_length (tokens); version_major = n_tokens > 0 ? atoi (tokens[0]) : 0; version_minor = n_tokens > 1 ? atoi (tokens[1]) : 0; - g_strfreev (tokens); return version; } @@ -136,9 +133,9 @@ static gboolean display_number_in_use (guint display_number) { GList *link; - gchar *path; + g_autofree gchar *path = NULL; gboolean in_use; - gchar *data; + g_autofree gchar *data = NULL; /* See if we know we are managing a server with that number */ for (link = display_numbers; link; link = link->next) @@ -158,15 +155,12 @@ display_number_in_use (guint display_number) int pid; pid = atoi (g_strstrip (data)); - g_free (data); errno = 0; if (pid < 0 || (kill (pid, 0) < 0 && errno == ESRCH)) in_use = FALSE; } - g_free (path); - return in_use; } @@ -327,8 +321,9 @@ x_server_local_get_authority_file_path (XServerLocal *server) static gchar * get_absolute_command (const gchar *command) { - gchar **tokens; - gchar *absolute_binary, *absolute_command = NULL; + g_auto(GStrv) tokens = NULL; + g_autofree gchar *absolute_binary = NULL; + gchar *absolute_command = NULL; tokens = g_strsplit (command, " ", 2); @@ -340,9 +335,6 @@ get_absolute_command (const gchar *command) else absolute_command = g_strdup (absolute_binary); } - g_free (absolute_binary); - - g_strfreev (tokens); return absolute_command; } @@ -416,7 +408,7 @@ static void write_authority_file (XServerLocal *server) { XAuthority *authority; - GError *error = NULL; + g_autoptr(GError) error = NULL; authority = x_server_get_authority (X_SERVER (server)); if (!authority) @@ -425,16 +417,15 @@ write_authority_file (XServerLocal *server) /* Get file to write to if have authority */ if (!server->priv->authority_file) { - gchar *run_dir, *dir; + g_autofree gchar *run_dir = NULL; + g_autofree gchar *dir = NULL; run_dir = config_get_string (config_get_instance (), "LightDM", "run-directory"); dir = g_build_filename (run_dir, "root", NULL); - g_free (run_dir); if (g_mkdir_with_parents (dir, S_IRWXU) < 0) l_warning (server, "Failed to make authority directory %s: %s", dir, strerror (errno)); server->priv->authority_file = g_build_filename (dir, x_server_get_address (X_SERVER (server)), NULL); - g_free (dir); } l_debug (server, "Writing X server authority to %s", server->priv->authority_file); @@ -442,7 +433,6 @@ write_authority_file (XServerLocal *server) x_authority_write (authority, XAUTH_WRITE_MODE_REPLACE, server->priv->authority_file, &error); if (error) l_warning (server, "Failed to write authority: %s", error->message); - g_clear_error (&error); } static gboolean @@ -451,8 +441,11 @@ x_server_local_start (DisplayServer *display_server) XServerLocal *server = X_SERVER_LOCAL (display_server); ProcessRunFunc run_cb; gboolean result, backup_logs; - gchar *filename, *dir, *log_file, *absolute_command; - GString *command; + g_autofree gchar *filename = NULL; + g_autofree gchar *dir = NULL; + g_autofree gchar *log_file = NULL; + g_autofree gchar *absolute_command = NULL; + g_autoptr(GString) command = NULL; g_return_val_if_fail (server->priv->x_server_process == NULL, FALSE); @@ -473,9 +466,6 @@ x_server_local_start (DisplayServer *display_server) backup_logs = config_get_boolean (config_get_instance (), "LightDM", "backup-logs"); process_set_log_file (server->priv->x_server_process, log_file, X_SERVER_LOCAL_GET_CLASS (server)->get_log_stdout (server), backup_logs ? LOG_MODE_BACKUP_AND_TRUNCATE : LOG_MODE_APPEND); l_debug (display_server, "Logging to %s", log_file); - g_free (log_file); - g_free (filename); - g_free (dir); absolute_command = get_absolute_command (server->priv->command); if (!absolute_command) @@ -485,7 +475,6 @@ x_server_local_start (DisplayServer *display_server) return FALSE; } command = g_string_new (absolute_command); - g_free (absolute_command); g_string_append_printf (command, " :%d", server->priv->display_number); @@ -530,7 +519,6 @@ x_server_local_start (DisplayServer *display_server) X_SERVER_LOCAL_GET_CLASS (server)->add_args (server, command); process_set_command (server->priv->x_server_process, command->str); - g_string_free (command, TRUE); l_debug (display_server, "Launching X Server"); @@ -542,10 +530,8 @@ x_server_local_start (DisplayServer *display_server) process_set_env (server->priv->x_server_process, "XAUTHORITY", g_getenv ("XAUTHORITY")); else { - gchar *path; - path = g_build_filename (g_get_home_dir (), ".Xauthority", NULL); + g_autofree gchar *path = g_build_filename (g_get_home_dir (), ".Xauthority", NULL); process_set_env (server->priv->x_server_process, "XAUTHORITY", path); - g_free (path); } } @@ -593,20 +579,18 @@ x_server_local_finalize (GObject *object) XServerLocal *self = X_SERVER_LOCAL (object); if (self->priv->x_server_process) - { g_signal_handlers_disconnect_matched (self->priv->x_server_process, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self); - g_object_unref (self->priv->x_server_process); - } - g_free (self->priv->command); - g_free (self->priv->config_file); - g_free (self->priv->layout); - g_free (self->priv->xdg_seat); - g_free (self->priv->xdmcp_server); - g_free (self->priv->xdmcp_key); - g_free (self->priv->authority_file); + g_clear_object (&self->priv->x_server_process); + g_clear_pointer (&self->priv->command, g_free); + g_clear_pointer (&self->priv->config_file, g_free); + g_clear_pointer (&self->priv->layout, g_free); + g_clear_pointer (&self->priv->xdg_seat, g_free); + g_clear_pointer (&self->priv->xdmcp_server, g_free); + g_clear_pointer (&self->priv->xdmcp_key, g_free); + g_clear_pointer (&self->priv->authority_file, g_free); if (self->priv->have_vt_ref) vt_unref (self->priv->vt); - g_free (self->priv->background); + g_clear_pointer (&self->priv->background, g_free); G_OBJECT_CLASS (x_server_local_parent_class)->finalize (object); } diff --git a/src/x-server-xmir.c b/src/x-server-xmir.c index fac57362..3ec7dedb 100644 --- a/src/x-server-xmir.c +++ b/src/x-server-xmir.c @@ -141,12 +141,10 @@ x_server_xmir_finalize (GObject *object) XServerXmir *self = X_SERVER_XMIR (object); if (self->priv->compositor) - { g_signal_handlers_disconnect_matched (self->priv->compositor, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self); - g_object_unref (self->priv->compositor); - } - g_free (self->priv->mir_id); - g_free (self->priv->mir_socket); + g_clear_object (&self->priv->compositor); + g_clear_pointer (&self->priv->mir_id, g_free); + g_clear_pointer (&self->priv->mir_socket, g_free); G_OBJECT_CLASS (x_server_xmir_parent_class)->finalize (object); } diff --git a/src/x-server.c b/src/x-server.c index 1073841e..d5936190 100644 --- a/src/x-server.c +++ b/src/x-server.c @@ -141,15 +141,14 @@ x_server_connect_session (DisplayServer *display_server, Session *session) vt = display_server_get_vt (display_server); if (vt > 0) { - gchar *t; + g_autofree gchar *tty_text = NULL; + g_autofree gchar *vt_text = NULL; - t = g_strdup_printf ("/dev/tty%d", vt); - session_set_tty (session, t); - g_free (t); + tty_text = g_strdup_printf ("/dev/tty%d", vt); + session_set_tty (session, tty_text); - t = g_strdup_printf ("%d", vt); - session_set_env (session, "XDG_VTNR", t); - g_free (t); + vt_text = g_strdup_printf ("%d", vt); + session_set_env (session, "XDG_VTNR", vt_text); } else l_debug (session, "Not setting XDG_VTNR"); @@ -191,11 +190,12 @@ x_server_finalize (GObject *object) { XServer *self = X_SERVER (object); - g_free (self->priv->hostname); - g_free (self->priv->address); + g_clear_pointer (&self->priv->hostname, g_free); + g_clear_pointer (&self->priv->address, g_free); g_clear_object (&self->priv->authority); if (self->priv->connection) xcb_disconnect (self->priv->connection); + self->priv->connection = NULL; G_OBJECT_CLASS (x_server_parent_class)->finalize (object); } diff --git a/src/xdmcp-protocol.c b/src/xdmcp-protocol.c index 2901837b..bedcc1a0 100644 --- a/src/xdmcp-protocol.c +++ b/src/xdmcp-protocol.c @@ -408,24 +408,20 @@ xdmcp_packet_encode (XDMCPPacket *packet, guint8 *data, gsize max_length) static gchar * data_tostring (XDMCPData *data) { - GString *s; + g_autoptr(GString) s = NULL; guint16 i; - gchar *string; s = g_string_new (""); for (i = 0; i < data->length; i++) g_string_append_printf (s, "%02X", data->data[i]); - string = s->str; - g_string_free (s, FALSE); - return string; + return g_steal_pointer (&s->str); } static gchar * string_list_tostring (gchar **strings) { - GString *s; - gchar *string; + g_autoptr(GString) s = NULL; gchar **i; s = g_string_new (""); @@ -435,46 +431,38 @@ string_list_tostring (gchar **strings) g_string_append (s, " "); g_string_append_printf (s, "'%s'", *i); } - string = s->str; - g_string_free (s, FALSE); - return string; + return g_steal_pointer (&s->str); } gchar * xdmcp_packet_tostring (XDMCPPacket *packet) { - gchar *string, *t, *t2, *t5; - gint i; - GString *t3; - switch (packet->opcode) { case XDMCP_BroadcastQuery: - t = string_list_tostring (packet->Query.authentication_names); - string = g_strdup_printf ("BroadcastQuery(authentication_names=[%s])", t); - g_free (t); - return string; + { + g_autofree gchar *names = string_list_tostring (packet->Query.authentication_names); + return g_strdup_printf ("BroadcastQuery(authentication_names=[%s])", names); + } case XDMCP_Query: - t = string_list_tostring (packet->Query.authentication_names); - string = g_strdup_printf ("Query(authentication_names=[%s])", t); - g_free (t); - return string; + { + g_autofree gchar *names = string_list_tostring (packet->Query.authentication_names); + return g_strdup_printf ("Query(authentication_names=[%s])", names); + } case XDMCP_IndirectQuery: - t = string_list_tostring (packet->Query.authentication_names); - string = g_strdup_printf ("IndirectQuery(authentication_names=[%s])", t); - g_free (t); - return string; + { + g_autofree gchar *names = string_list_tostring (packet->Query.authentication_names); + return g_strdup_printf ("IndirectQuery(authentication_names=[%s])", names); + } case XDMCP_ForwardQuery: - t = data_tostring (&packet->ForwardQuery.client_address); - t2 = data_tostring (&packet->ForwardQuery.client_port); - t5 = string_list_tostring (packet->ForwardQuery.authentication_names); - string = g_strdup_printf ("ForwardQuery(client_address=%s client_port=%s authentication_names=[%s])", - t, t2, t5); - g_free (t); - g_free (t2); - g_free (t5); - return string; + { + g_autofree gchar *address_text = data_tostring (&packet->ForwardQuery.client_address); + g_autofree gchar *port_text = data_tostring (&packet->ForwardQuery.client_port); + g_autofree gchar *names_text = string_list_tostring (packet->ForwardQuery.authentication_names); + return g_strdup_printf ("ForwardQuery(client_address=%s client_port=%s authentication_names=[%s])", + address_text, port_text, names_text); + } case XDMCP_Willing: return g_strdup_printf ("Willing(authentication_name='%s' hostname='%s' status='%s')", packet->Willing.authentication_name, packet->Willing.hostname, packet->Willing.status); @@ -482,16 +470,19 @@ xdmcp_packet_tostring (XDMCPPacket *packet) return g_strdup_printf ("Unwilling(hostname='%s' status='%s')", packet->Unwilling.hostname, packet->Unwilling.status); case XDMCP_Request: - t = string_list_tostring (packet->Request.authorization_names); - t2 = data_tostring (&packet->Request.authentication_data); - t3 = g_string_new (""); + { + g_autofree gchar *names_text = string_list_tostring (packet->Request.authorization_names); + g_autofree gchar *data_text = data_tostring (&packet->Request.authentication_data); + g_autoptr(GString) connections_text = g_string_new (""); + gint i; + for (i = 0; i < packet->Request.n_connections; i++) { XDMCPConnection *connection = &packet->Request.connections[i]; GSocketFamily family = G_SOCKET_FAMILY_INVALID; if (i != 0) - g_string_append (t3, " "); + g_string_append (connections_text, " "); if (connection->type == XAUTH_FAMILY_INTERNET && connection->address.length == 4) family = G_SOCKET_FAMILY_IPV4; @@ -500,41 +491,34 @@ xdmcp_packet_tostring (XDMCPPacket *packet) if (family != G_SOCKET_FAMILY_INVALID) { - GInetAddress *address = g_inet_address_new_from_bytes (connection->address.data, family); - gchar *t4 = g_inet_address_to_string (address); - g_string_append (t3, t4); - g_free (t4); - g_object_unref (address); + g_autoptr(GInetAddress) address = g_inet_address_new_from_bytes (connection->address.data, family); + g_autofree gchar *text = g_inet_address_to_string (address); + g_string_append (connections_text, text); } else { - gchar *t4 = data_tostring (&connection->address); - g_string_append_printf (t3, "(%d, %s)", connection->type, t4); - g_free (t4); + g_autofree gchar *text = data_tostring (&connection->address); + g_string_append_printf (connections_text, "(%d, %s)", connection->type, text); } } - string = g_strdup_printf ("Request(display_number=%d connections=[%s] authentication_name='%s' authentication_data=%s authorization_names=[%s] manufacturer_display_id='%s')", - packet->Request.display_number, t3->str, packet->Request.authentication_name, t2, - t, packet->Request.manufacturer_display_id); - g_free (t); - g_free (t2); - g_string_free (t3, TRUE); - return string; + return g_strdup_printf ("Request(display_number=%d connections=[%s] authentication_name='%s' authentication_data=%s authorization_names=[%s] manufacturer_display_id='%s')", + packet->Request.display_number, connections_text->str, packet->Request.authentication_name, data_text, + names_text, packet->Request.manufacturer_display_id); + } case XDMCP_Accept: - t = data_tostring (&packet->Accept.authentication_data); - t2 = data_tostring (&packet->Accept.authorization_data); - string = g_strdup_printf ("Accept(session_id=%d authentication_name='%s' authentication_data=%s authorization_name='%s' authorization_data=%s)", - packet->Accept.session_id, packet->Accept.authentication_name, t, - packet->Accept.authorization_name, t2); - g_free (t); - g_free (t2); - return string; + { + g_autofree gchar *authentication_text = data_tostring (&packet->Accept.authentication_data); + g_autofree gchar *authorization_text = data_tostring (&packet->Accept.authorization_data); + return g_strdup_printf ("Accept(session_id=%d authentication_name='%s' authentication_data=%s authorization_name='%s' authorization_data=%s)", + packet->Accept.session_id, packet->Accept.authentication_name, authentication_text, + packet->Accept.authorization_name, authorization_text); + } case XDMCP_Decline: - t = data_tostring (&packet->Decline.authentication_data); - string = g_strdup_printf ("Decline(status='%s' authentication_name='%s' authentication_data=%s)", - packet->Decline.status, packet->Decline.authentication_name, t); - g_free (t); - return string; + { + g_autofree gchar *t = data_tostring (&packet->Decline.authentication_data); + return g_strdup_printf ("Decline(status='%s' authentication_name='%s' authentication_data=%s)", + packet->Decline.status, packet->Decline.authentication_name, t); + } case XDMCP_Manage: return g_strdup_printf ("Manage(session_id=%d display_number=%d display_class='%s')", packet->Manage.session_id, packet->Manage.display_number, packet->Manage.display_class); diff --git a/src/xdmcp-server.c b/src/xdmcp-server.c index b1247a81..ea254654 100644 --- a/src/xdmcp-server.c +++ b/src/xdmcp-server.c @@ -177,37 +177,32 @@ get_session (XDMCPServer *server, guint16 id) static gchar * socket_address_to_string (GSocketAddress *address) { - gchar *inet_text, *text; + g_autofree gchar *inet_text = NULL; inet_text = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address))); - text = g_strdup_printf ("%s:%d", inet_text, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address))); - g_free (inet_text); - - return text; + return g_strdup_printf ("%s:%d", inet_text, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address))); } static void send_packet (GSocket *socket, GSocketAddress *address, XDMCPPacket *packet) { - gchar *address_string; + g_autofree gchar *address_string = NULL; guint8 data[1024]; gssize n_written; address_string = socket_address_to_string (address); g_debug ("Send %s to %s", xdmcp_packet_tostring (packet), address_string); - g_free (address_string); n_written = xdmcp_packet_encode (packet, data, 1024); if (n_written < 0) g_critical ("Failed to encode XDMCP packet"); else { - GError *error = NULL; + g_autoptr(GError) error = NULL; g_socket_send_to (socket, address, (gchar *) data, n_written, NULL, &error); if (error) g_warning ("Error sending packet: %s", error->message); - g_clear_error (&error); } } @@ -266,8 +261,8 @@ static void handle_forward_query (XDMCPServer *server, GSocket *socket, GSocketAddress *address, XDMCPPacket *packet) { GSocketFamily family; - GInetAddress *client_inet_address; - GSocketAddress *client_address; + g_autoptr(GInetAddress) client_inet_address = NULL; + g_autoptr(GSocketAddress) client_address = NULL; gint i; guint16 port = 0; @@ -298,11 +293,8 @@ handle_forward_query (XDMCPServer *server, GSocket *socket, GSocketAddress *addr client_inet_address = g_inet_address_new_from_bytes (packet->ForwardQuery.client_address.data, family); client_address = g_inet_socket_address_new (client_inet_address, port); - g_object_unref (client_inet_address); handle_query (server, socket, client_address, packet->ForwardQuery.authentication_names); - - g_object_unref (client_address); } static guint8 @@ -465,7 +457,8 @@ handle_request (XDMCPServer *server, GSocket *socket, GSocketAddress *address, X { XDMCPPacket *response; XDMCPSession *session; - gchar *authentication_name = NULL, *decline_status = NULL, *authorization_name, *display_number; + gchar *authentication_name = NULL, *decline_status = NULL, *authorization_name; + g_autofree gchar *display_number = NULL; guint8 *authentication_data = NULL, *authorization_data = NULL, *session_authorization_data = NULL; gsize authentication_data_length = 0, authorization_data_length = 0, session_authorization_data_length = 0; XDMCPConnection *connection; @@ -569,7 +562,7 @@ handle_request (XDMCPServer *server, GSocket *socket, GSocketAddress *address, X } else { - XAuthority *auth; + g_autoptr(XAuthority) auth = NULL; /* Data is the cookie */ auth = x_authority_new_cookie (XAUTH_FAMILY_WILD, NULL, 0, ""); @@ -578,8 +571,6 @@ handle_request (XDMCPServer *server, GSocket *socket, GSocketAddress *address, X authorization_name = g_strdup ("MIT-MAGIC-COOKIE-1"); session_authorization_data = x_authority_copy_authorization_data (auth); session_authorization_data_length = x_authority_get_authorization_data_length (auth); - - g_object_unref (auth); } session = add_session (server); @@ -611,7 +602,6 @@ handle_request (XDMCPServer *server, GSocket *socket, GSocketAddress *address, X authorization_name, session_authorization_data, session_authorization_data_length); - g_free (display_number); response = xdmcp_packet_alloc (XDMCP_Accept); response->Accept.session_id = xdmcp_session_get_id (session); @@ -711,13 +701,12 @@ read_cb (GSocket *socket, GIOCondition condition, XDMCPServer *server) { GSocketAddress *address; gchar data[1024]; - GError *error = NULL; + g_autoptr(GError) error = NULL; gssize n_read; n_read = g_socket_receive_from (socket, &address, data, 1024, NULL, &error); if (error) g_warning ("Failed to read from XDMCP socket: %s", error->message); - g_clear_error (&error); if (n_read > 0) { @@ -726,13 +715,12 @@ read_cb (GSocket *socket, GIOCondition condition, XDMCPServer *server) packet = xdmcp_packet_decode ((guint8 *)data, n_read); if (packet) { - gchar *packet_string, *address_string; + g_autofree gchar *packet_string = NULL; + g_autofree gchar *address_string = NULL; packet_string = xdmcp_packet_tostring (packet); address_string = socket_address_to_string (address); g_debug ("Got %s from %s", packet_string, address_string); - g_free (packet_string); - g_free (address_string); switch (packet->opcode) { @@ -768,8 +756,8 @@ read_cb (GSocket *socket, GIOCondition condition, XDMCPServer *server) static GSocket * open_udp_socket (GSocketFamily family, guint port, const gchar *listen_address, GError **error) { - GSocket *socket; - GSocketAddress *address; + g_autoptr(GSocket) socket = NULL; + g_autoptr(GSocketAddress) address = NULL; gboolean result; socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, error); @@ -782,10 +770,7 @@ open_udp_socket (GSocketFamily family, guint port, const gchar *listen_address, addresses = g_resolver_lookup_by_name (g_resolver_get_default (), listen_address, NULL, error); if (!addresses) - { - g_object_unref (socket); return NULL; - } address = g_inet_socket_address_new (addresses->data, port); g_resolver_free_addresses (addresses); } @@ -793,26 +778,23 @@ open_udp_socket (GSocketFamily family, guint port, const gchar *listen_address, address = g_inet_socket_address_new (g_inet_address_new_any (family), port); result = g_socket_bind (socket, address, TRUE, error); if (!result) - { - g_object_unref (socket); return NULL; - } - return socket; + return g_steal_pointer (&socket); } gboolean xdmcp_server_start (XDMCPServer *server) { GSource *source; - GError *error = NULL; + g_autoptr(GError) ipv4_error = NULL; + g_autoptr(GError) ipv6_error = NULL; g_return_val_if_fail (server != NULL, FALSE); - server->priv->socket = open_udp_socket (G_SOCKET_FAMILY_IPV4, server->priv->port, server->priv->listen_address, &error); - if (error) - g_warning ("Failed to create IPv4 XDMCP socket: %s", error->message); - g_clear_error (&error); + server->priv->socket = open_udp_socket (G_SOCKET_FAMILY_IPV4, server->priv->port, server->priv->listen_address, &ipv4_error); + if (ipv4_error) + g_warning ("Failed to create IPv4 XDMCP socket: %s", ipv4_error->message); if (server->priv->socket) { @@ -821,10 +803,9 @@ xdmcp_server_start (XDMCPServer *server) g_source_attach (source, NULL); } - server->priv->socket6 = open_udp_socket (G_SOCKET_FAMILY_IPV6, server->priv->port, server->priv->listen_address, &error); - if (error) - g_warning ("Failed to create IPv6 XDMCP socket: %s", error->message); - g_clear_error (&error); + server->priv->socket6 = open_udp_socket (G_SOCKET_FAMILY_IPV6, server->priv->port, server->priv->listen_address, &ipv6_error); + if (ipv6_error) + g_warning ("Failed to create IPv6 XDMCP socket: %s", ipv6_error->message); if (server->priv->socket6) { @@ -857,11 +838,11 @@ xdmcp_server_finalize (GObject *object) g_clear_object (&self->priv->socket); g_clear_object (&self->priv->socket6); - g_free (self->priv->listen_address); - g_free (self->priv->hostname); - g_free (self->priv->status); - g_free (self->priv->key); - g_hash_table_unref (self->priv->sessions); + g_clear_pointer (&self->priv->listen_address, g_free); + g_clear_pointer (&self->priv->hostname, g_free); + g_clear_pointer (&self->priv->status, g_free); + g_clear_pointer (&self->priv->key, g_free); + g_clear_pointer (&self->priv->sessions, g_hash_table_unref); G_OBJECT_CLASS (xdmcp_server_parent_class)->finalize (object); } diff --git a/src/xdmcp-session.c b/src/xdmcp-session.c index bb78ac66..967cdf25 100644 --- a/src/xdmcp-session.c +++ b/src/xdmcp-session.c @@ -79,10 +79,10 @@ xdmcp_session_finalize (GObject *object) { XDMCPSession *self = XDMCP_SESSION (object); - g_free (self->priv->manufacturer_display_id); + g_clear_pointer (&self->priv->manufacturer_display_id, g_free); g_clear_object (&self->priv->address); g_clear_object (&self->priv->authority); - g_free (self->priv->display_class); + g_clear_pointer (&self->priv->display_class, g_free); G_OBJECT_CLASS (xdmcp_session_parent_class)->finalize (object); } diff --git a/tests/src/X.c b/tests/src/X.c index a367a9d4..cda567a2 100644 --- a/tests/src/X.c +++ b/tests/src/X.c @@ -67,10 +67,8 @@ cleanup (void) { if (lock_path) unlink (lock_path); - if (xserver) - g_object_unref (xserver); - if (xdmcp_client) - g_object_unref (xdmcp_client); + g_clear_object (&xserver); + g_clear_object (&xdmcp_client); } static void @@ -136,14 +134,13 @@ data_to_string (guint8 *data, gsize data_length) static void xdmcp_accept_cb (XDMCPClient *client, XDMCPAccept *message) { - gchar *authentication_data_text, *authorization_data_text; + g_autofree gchar *authentication_data_text = NULL; + g_autofree gchar *authorization_data_text = NULL; authentication_data_text = data_to_string (message->authentication_data, message->authentication_data_length); authorization_data_text = data_to_string (message->authorization_data, message->authorization_data_length); status_notify ("%s GOT-ACCEPT SESSION-ID=%d AUTHENTICATION-NAME=\"%s\" AUTHENTICATION-DATA=%s AUTHORIZATION-NAME=\"%s\" AUTHORIZATION-DATA=%s", id, message->session_id, message->authentication_name, authentication_data_text, message->authorization_name, authorization_data_text); - g_free (authentication_data_text); - g_free (authorization_data_text); xdmcp_session_id = message->session_id; @@ -156,11 +153,10 @@ xdmcp_accept_cb (XDMCPClient *client, XDMCPAccept *message) static void xdmcp_decline_cb (XDMCPClient *client, XDMCPDecline *message) { - gchar *authentication_data_text; + g_autofree gchar *authentication_data_text = NULL; authentication_data_text = data_to_string (message->authentication_data, message->authentication_data_length); status_notify ("%s GOT-DECLINE STATUS=\"%s\" AUTHENTICATION-NAME=\"%s\" AUTHENTICATION-DATA=%s", id, message->status, message->authentication_name, authentication_data_text); - g_free (authentication_data_text); } static void @@ -232,7 +228,7 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "SEND-QUERY") == 0) { const gchar *authentication_names_list; - gchar **authentication_names; + g_auto(GStrv) authentication_names = NULL; if (!xdmcp_client_start (xdmcp_client)) quit (EXIT_FAILURE); @@ -243,15 +239,15 @@ request_cb (const gchar *name, GHashTable *params) authentication_names = g_strsplit (authentication_names_list, " ", -1); xdmcp_client_send_query (xdmcp_client, authentication_names); - g_strfreev (authentication_names); } else if (strcmp (name, "SEND-REQUEST") == 0) { const gchar *text, *addresses_list, *authentication_name, *authentication_data_text, *authorization_names_list, *mfid; int request_display_number = display_number; - gchar **list, **authorization_names; - guint8 *authentication_data; + g_auto(GStrv) list = NULL; + g_auto(GStrv) authorization_names = NULL; + g_autofree guint8 *authentication_data = NULL; gsize authentication_data_length, list_length; gint i; GInetAddress **addresses; @@ -281,7 +277,6 @@ request_cb (const gchar *name, GHashTable *params) for (i = 0; i < list_length; i++) addresses[i] = g_inet_address_new_from_string (list[i]); addresses[i] = NULL; - g_strfreev (list); authentication_data_length = strlen (authentication_data_text) / 2; authentication_data = malloc (authentication_data_length); @@ -296,8 +291,6 @@ request_cb (const gchar *name, GHashTable *params) authentication_name, authentication_data, authentication_data_length, authorization_names, mfid); - g_free (authentication_data); - g_strfreev (authorization_names); } else if (strcmp (name, "SEND-MANAGE") == 0) @@ -352,16 +345,16 @@ int main (int argc, char **argv) { int i; - gchar **tokens; - char *pid_string; + g_auto(GStrv) tokens = NULL; + g_autofree gchar *pid_string = NULL; gboolean do_xdmcp = FALSE; guint xdmcp_port = 0; - gchar *xdmcp_host = NULL; - gchar *seat = NULL; - gchar *mir_id = NULL; - gchar *lock_filename; + const gchar *xdmcp_host = NULL; + const gchar *seat = NULL; + const gchar *mir_id = NULL; + g_autofree gchar *lock_filename = NULL; int lock_file; - GString *status_text; + g_autoptr(GString) status_text = NULL; #if !defined(GLIB_VERSION_2_36) g_type_init (); @@ -382,7 +375,6 @@ main (int argc, char **argv) tokens = g_strsplit (xorg_version, ".", -1); xorg_version_major = g_strv_length (tokens) > 0 ? atoi (tokens[0]) : 0; xorg_version_minor = g_strv_length (tokens) > 1 ? atoi (tokens[1]) : 0; - g_strfreev (tokens); /* TCP listening default changed in 1.17.0 */ listen_tcp = version_compare (1, 17) < 0; @@ -530,7 +522,6 @@ main (int argc, char **argv) if (mir_id != NULL) g_string_append_printf (status_text, " MIR-ID=%s", mir_id); status_notify ("%s", status_text->str); - g_string_free (status_text, TRUE); if (g_key_file_has_key (config, "test-xserver-config", "return-value", NULL)) { @@ -541,26 +532,24 @@ main (int argc, char **argv) lock_filename = g_strdup_printf (".X%d-lock", display_number); lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL); - g_free (lock_filename); lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); if (lock_file < 0) { - char *lock_contents = NULL; + g_autofree gchar *lock_contents = NULL; if (g_file_get_contents (lock_path, &lock_contents, NULL, NULL)) { - gchar *proc_filename; + g_autofree gchar *proc_filename = NULL; pid_t pid; pid = atol (lock_contents); - g_free (lock_contents); proc_filename = g_strdup_printf ("/proc/%d", pid); if (!g_file_test (proc_filename, G_FILE_TEST_EXISTS)) { - gchar *socket_dir; - gchar *socket_filename; - gchar *socket_path; + g_autofree gchar *socket_dir = NULL; + g_autofree gchar *socket_filename = NULL; + g_autofree gchar *socket_path = NULL; socket_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", ".X11-unix", NULL); g_mkdir_with_parents (socket_dir, 0755); @@ -571,12 +560,7 @@ main (int argc, char **argv) g_printerr ("Breaking lock on non-existant process %d\n", pid); unlink (lock_path); unlink (socket_path); - - g_free (socket_dir); - g_free (socket_filename); - g_free (socket_path); } - g_free (proc_filename); lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); } @@ -588,8 +572,7 @@ main (int argc, char **argv) "Server is already active for display %d\n" " If this server is no longer running, remove %s\n" " and start again.\n", display_number, lock_path); - g_free (lock_path); - lock_path = NULL; + g_clear_pointer (&lock_path, g_free); return EXIT_FAILURE; } pid_string = g_strdup_printf ("%10ld", (long) getpid ()); @@ -598,7 +581,6 @@ main (int argc, char **argv) g_warning ("Error writing PID file: %s", strerror (errno)); return EXIT_FAILURE; } - g_free (pid_string); if (!x_server_start (xserver)) return EXIT_FAILURE; diff --git a/tests/src/Xmir.c b/tests/src/Xmir.c index 5baa4921..6e8a0f7b 100644 --- a/tests/src/Xmir.c +++ b/tests/src/Xmir.c @@ -41,8 +41,7 @@ cleanup (void) { if (lock_path) unlink (lock_path); - if (xserver) - g_object_unref (xserver); + g_clear_object (&xserver); } static void @@ -115,12 +114,12 @@ int main (int argc, char **argv) { int i; - char *pid_string; + g_autofree gchar *pid_string = NULL; gchar *seat = NULL; gchar *mir_id = NULL; - gchar *lock_filename; + g_autofree gchar *lock_filename = NULL; int lock_file; - GString *status_text; + g_autoptr(GString) status_text = NULL; #if !defined(GLIB_VERSION_2_36) g_type_init (); @@ -219,7 +218,6 @@ main (int argc, char **argv) if (mir_id != NULL) g_string_append_printf (status_text, " MIR-ID=%s", mir_id); status_notify ("%s", status_text->str); - g_string_free (status_text, TRUE); config = g_key_file_new (); g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL); @@ -233,26 +231,24 @@ main (int argc, char **argv) lock_filename = g_strdup_printf (".X%d-lock", display_number); lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL); - g_free (lock_filename); lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); if (lock_file < 0) { - char *lock_contents = NULL; + g_autofree gchar *lock_contents = NULL; if (g_file_get_contents (lock_path, &lock_contents, NULL, NULL)) { - gchar *proc_filename; + g_autofree gchar *proc_filename = NULL; pid_t pid; pid = atol (lock_contents); - g_free (lock_contents); proc_filename = g_strdup_printf ("/proc/%d", pid); if (!g_file_test (proc_filename, G_FILE_TEST_EXISTS)) { - gchar *socket_dir; - gchar *socket_filename; - gchar *socket_path; + g_autofree gchar *socket_dir = NULL; + g_autofree gchar *socket_filename = NULL; + g_autofree gchar *socket_path = NULL; socket_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", ".X11-unix", NULL); g_mkdir_with_parents (socket_dir, 0755); @@ -263,12 +259,7 @@ main (int argc, char **argv) g_printerr ("Breaking lock on non-existant process %d\n", pid); unlink (lock_path); unlink (socket_path); - - g_free (socket_dir); - g_free (socket_filename); - g_free (socket_path); } - g_free (proc_filename); lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); } @@ -280,8 +271,7 @@ main (int argc, char **argv) "Server is already active for display %d\n" " If this server is no longer running, remove %s\n" " and start again.\n", display_number, lock_path); - g_free (lock_path); - lock_path = NULL; + g_clear_pointer (&lock_path, g_free); return EXIT_FAILURE; } pid_string = g_strdup_printf ("%10ld", (long) getpid ()); @@ -290,7 +280,6 @@ main (int argc, char **argv) g_warning ("Error writing PID file: %s", strerror (errno)); return EXIT_FAILURE; } - g_free (pid_string); if (!x_server_start (xserver)) return EXIT_FAILURE; diff --git a/tests/src/Xvnc.c b/tests/src/Xvnc.c index 56dada4f..9459c224 100644 --- a/tests/src/Xvnc.c +++ b/tests/src/Xvnc.c @@ -38,8 +38,7 @@ cleanup (void) { if (lock_path) unlink (lock_path); - if (xserver) - g_object_unref (xserver); + g_clear_object (&xserver); } static void @@ -91,12 +90,11 @@ vnc_data_cb (GIOChannel *channel, GIOCondition condition, gpointer data) gchar buffer[1024]; gsize n_read; GIOStatus status; - GError *error = NULL; + g_autoptr(GError) error = NULL; status = g_io_channel_read_chars (channel, buffer, 1023, &n_read, &error); if (error) g_warning ("Error reading from VNC client: %s", error->message); - g_clear_error (&error); if (status == G_IO_STATUS_NORMAL) { @@ -142,12 +140,12 @@ int main (int argc, char **argv) { int i; - char *pid_string; + g_autofree gchar *pid_string = NULL; gboolean use_inetd = FALSE; gboolean has_option = FALSE; - gchar *geometry = g_strdup ("640x480"); + const gchar *geometry = "640x480"; gint depth = 8; - gchar *lock_filename; + g_autofree gchar *lock_filename = NULL; int lock_file; #if !defined(GLIB_VERSION_2_36) @@ -184,8 +182,7 @@ main (int argc, char **argv) } else if (strcmp (arg, "-geometry") == 0) { - g_free (geometry); - geometry = g_strdup (argv[i+1]); + geometry = argv[i+1]; i++; } else if (strcmp (arg, "-depth") == 0) @@ -241,26 +238,24 @@ main (int argc, char **argv) lock_filename = g_strdup_printf (".X%d-lock", display_number); lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL); - g_free (lock_filename); lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); if (lock_file < 0) { - char *lock_contents = NULL; + g_autofree gchar *lock_contents = NULL; if (g_file_get_contents (lock_path, &lock_contents, NULL, NULL)) { - gchar *proc_filename; + g_autofree gchar *proc_filename = NULL; pid_t pid; pid = atol (lock_contents); - g_free (lock_contents); proc_filename = g_strdup_printf ("/proc/%d", pid); if (!g_file_test (proc_filename, G_FILE_TEST_EXISTS)) { - gchar *socket_dir; - gchar *socket_filename; - gchar *socket_path; + g_autofree gchar *socket_dir = NULL; + g_autofree gchar *socket_filename = NULL; + g_autofree gchar *socket_path = NULL; socket_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", ".X11-unix", NULL); g_mkdir_with_parents (socket_dir, 0755); @@ -271,12 +266,7 @@ main (int argc, char **argv) g_printerr ("Breaking lock on non-existant process %d\n", pid); unlink (lock_path); unlink (socket_path); - - g_free (socket_dir); - g_free (socket_filename); - g_free (socket_path); } - g_free (proc_filename); lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); } @@ -288,8 +278,7 @@ main (int argc, char **argv) "Server is already active for display %d\n" " If this server is no longer running, remove %s\n" " and start again.\n", display_number, lock_path); - g_free (lock_path); - lock_path = NULL; + g_clear_pointer (&lock_path, g_free); return EXIT_FAILURE; } pid_string = g_strdup_printf ("%10ld", (long) getpid ()); @@ -298,7 +287,6 @@ main (int argc, char **argv) g_warning ("Error writing PID file: %s", strerror (errno)); return EXIT_FAILURE; } - g_free (pid_string); if (!x_server_start (xserver)) return EXIT_FAILURE; diff --git a/tests/src/dbus-env.c b/tests/src/dbus-env.c index d99d73ae..a988b68b 100644 --- a/tests/src/dbus-env.c +++ b/tests/src/dbus-env.c @@ -13,7 +13,7 @@ create_bus (const gchar *config_file, GPid *pid) gchar *command, address[1024]; gchar **argv; ssize_t n_read; - GError *error = NULL; + g_autoptr(GError) error = NULL; if (pipe (name_pipe) < 0) { @@ -26,7 +26,6 @@ create_bus (const gchar *config_file, GPid *pid) g_warning ("Error parsing command line: %s", error->message); exit (EXIT_FAILURE); } - g_clear_error (&error); if (!g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_LEAVE_DESCRIPTORS_OPEN, NULL, NULL, pid, &error)) { g_warning ("Error launching D-Bus: %s", error->message); @@ -50,18 +49,19 @@ create_bus (const gchar *config_file, GPid *pid) int main (int argc, char **argv) { - gchar *conf_file, *system_bus_address, *session_bus_address; + g_autofree gchar *system_conf_file = NULL; + g_autofree gchar *session_conf_file = NULL; + g_autofree gchar *system_bus_address = NULL; + g_autofree gchar *session_bus_address = NULL; GPid system_bus_pid, session_bus_pid, child_pid; int status; - conf_file = g_build_filename (DATADIR, "system.conf", NULL); - system_bus_address = create_bus (conf_file, &system_bus_pid); - g_free (conf_file); + system_conf_file = g_build_filename (DATADIR, "system.conf", NULL); + system_bus_address = create_bus (system_conf_file, &system_bus_pid); g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", system_bus_address, TRUE); - conf_file = g_build_filename (DATADIR, "session.conf", NULL); - session_bus_address = create_bus (conf_file, &session_bus_pid); - g_free (conf_file); + session_conf_file = g_build_filename (DATADIR, "session.conf", NULL); + session_bus_address = create_bus (session_conf_file, &session_bus_pid); g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_address, TRUE); child_pid = fork (); diff --git a/tests/src/guest-account.c b/tests/src/guest-account.c index 655f34b9..0df3f092 100644 --- a/tests/src/guest-account.c +++ b/tests/src/guest-account.c @@ -46,14 +46,13 @@ main (int argc, char **argv) { while (fgets (line, 1024, passwd)) { - gchar **tokens = g_strsplit (line, ":", -1); + g_auto(GStrv) tokens = g_strsplit (line, ":", -1); if (g_strv_length (tokens) >= 3) { gint uid = atoi (tokens[2]); if (uid > max_uid) max_uid = uid; } - g_strfreev (tokens); } fclose (passwd); } diff --git a/tests/src/initctl.c b/tests/src/initctl.c index 600cea98..f7732c0b 100644 --- a/tests/src/initctl.c +++ b/tests/src/initctl.c @@ -16,14 +16,13 @@ main (int argc, char **argv) if (g_key_file_get_boolean (config, "test-initctl-config", "report-events", NULL)) { - GString *status_text; + g_autoptr(GString) status_text = NULL; int i; status_text = g_string_new ("INIT"); for (i = 1; i < argc; i++) g_string_append_printf (status_text, " %s", argv[i]); status_notify ("%s", status_text->str); - g_string_free (status_text, TRUE); } return EXIT_SUCCESS; diff --git a/tests/src/libsystem.c b/tests/src/libsystem.c index dadcaf59..5959995e 100644 --- a/tests/src/libsystem.c +++ b/tests/src/libsystem.c @@ -107,7 +107,7 @@ int getgroups (int size, gid_t list[]) { const gchar *group_list; - gchar **groups; + g_auto(GStrv) groups = NULL; gint groups_length; /* Get groups we are a member of */ @@ -129,7 +129,6 @@ getgroups (int size, gid_t list[]) for (i = 0; groups[i]; i++) list[i] = atoi (groups[i]); } - g_free (groups); return groups_length; } @@ -138,7 +137,7 @@ int setgroups (size_t size, const gid_t *list) { size_t i; - GString *group_list; + g_autoptr(GString) group_list = NULL; group_list = g_string_new (""); for (i = 0; i < size; i++) @@ -148,7 +147,6 @@ setgroups (size_t size, const gid_t *list) g_string_append_printf (group_list, "%d", list[i]); } g_setenv ("LIGHTDM_TEST_GROUPS", group_list->str, TRUE); - g_string_free (group_list, TRUE); return 0; } @@ -229,7 +227,7 @@ static int open_wrapper (const char *func, const char *pathname, int flags, mode_t mode) { int (*_open) (const char *pathname, int flags, mode_t mode); - gchar *new_path = NULL; + g_autofree gchar *new_path = NULL; int fd; _open = (int (*)(const char *pathname, int flags, mode_t mode)) dlsym (RTLD_NEXT, func); @@ -246,7 +244,6 @@ open_wrapper (const char *func, const char *pathname, int flags, mode_t mode) new_path = redirect_path (pathname); fd = _open (new_path, flags, mode); - g_free (new_path); return fd; } @@ -283,14 +280,13 @@ FILE * fopen (const char *path, const char *mode) { FILE *(*_fopen) (const char *pathname, const char *mode); - gchar *new_path = NULL; + g_autofree gchar *new_path = NULL; FILE *result; _fopen = (FILE *(*)(const char *pathname, const char *mode)) dlsym (RTLD_NEXT, "fopen"); new_path = redirect_path (path); result = _fopen (new_path, mode); - g_free (new_path); return result; } @@ -299,14 +295,13 @@ int unlinkat (int dirfd, const char *pathname, int flags) { int (*_unlinkat) (int dirfd, const char *pathname, int flags); - gchar *new_path = NULL; + g_autofree gchar *new_path = NULL; int result; _unlinkat = (int (*)(int dirfd, const char *pathname, int flags)) dlsym (RTLD_NEXT, "unlinkat"); new_path = redirect_path (pathname); result = _unlinkat (dirfd, new_path, flags); - g_free (new_path); return result; } @@ -315,14 +310,13 @@ int creat (const char *pathname, mode_t mode) { int (*_creat) (const char *pathname, mode_t mode); - gchar *new_path = NULL; + g_autofree gchar *new_path = NULL; int result; _creat = (int (*)(const char *pathname, mode_t mode)) dlsym (RTLD_NEXT, "creat"); new_path = redirect_path (pathname); result = _creat (new_path, mode); - g_free (new_path); return result; } @@ -331,14 +325,13 @@ int creat64 (const char *pathname, mode_t mode) { int (*_creat64) (const char *pathname, mode_t mode); - gchar *new_path = NULL; + g_autofree gchar *new_path = NULL; int result; _creat64 = (int (*)(const char *pathname, mode_t mode)) dlsym (RTLD_NEXT, "creat64"); new_path = redirect_path (pathname); result = _creat64 (new_path, mode); - g_free (new_path); return result; } @@ -347,8 +340,7 @@ int access (const char *pathname, int mode) { int (*_access) (const char *pathname, int mode); - gchar *new_path = NULL; - int ret; + g_autofree gchar *new_path = NULL; _access = (int (*)(const char *pathname, int mode)) dlsym (RTLD_NEXT, "access"); @@ -358,138 +350,103 @@ access (const char *pathname, int mode) return F_OK; new_path = redirect_path (pathname); - ret = _access (new_path, mode); - g_free (new_path); - - return ret; + return _access (new_path, mode); } int stat (const char *path, struct stat *buf) { int (*_stat) (const char *path, struct stat *buf); - gchar *new_path = NULL; - int ret; + g_autofree gchar *new_path = NULL; _stat = (int (*)(const char *path, struct stat *buf)) dlsym (RTLD_NEXT, "stat"); new_path = redirect_path (path); - ret = _stat (new_path, buf); - g_free (new_path); - - return ret; + return _stat (new_path, buf); } int stat64 (const char *path, struct stat64 *buf) { int (*_stat64) (const char *path, struct stat64 *buf); - gchar *new_path = NULL; - int ret; + g_autofree gchar *new_path = NULL; _stat64 = (int (*)(const char *path, struct stat64 *buf)) dlsym (RTLD_NEXT, "stat64"); new_path = redirect_path (path); - ret = _stat64 (new_path, buf); - g_free (new_path); - - return ret; + return _stat64 (new_path, buf); } int __xstat (int version, const char *path, struct stat *buf) { int (*___xstat) (int version, const char *path, struct stat *buf); - gchar *new_path = NULL; - int ret; + g_autofree gchar *new_path = NULL; ___xstat = (int (*)(int version, const char *path, struct stat *buf)) dlsym (RTLD_NEXT, "__xstat"); new_path = redirect_path (path); - ret = ___xstat (version, new_path, buf); - g_free (new_path); - - return ret; + return ___xstat (version, new_path, buf); } int __xstat64 (int version, const char *path, struct stat64 *buf) { int (*___xstat64) (int version, const char *path, struct stat64 *buf); - gchar *new_path = NULL; - int ret; + g_autofree gchar *new_path = NULL; ___xstat64 = (int (*)(int version, const char *path, struct stat64 *buf)) dlsym (RTLD_NEXT, "__xstat64"); new_path = redirect_path (path); - ret = ___xstat64 (version, new_path, buf); - g_free (new_path); - - return ret; + return ___xstat64 (version, new_path, buf); } int __fxstatat(int ver, int dirfd, const char *pathname, struct stat *buf, int flags) { int (*___fxstatat) (int ver, int dirfd, const char *pathname, struct stat *buf, int flags); - gchar *new_path = NULL; - int ret; + g_autofree gchar *new_path = NULL; ___fxstatat = (int (*)(int ver, int dirfd, const char *pathname, struct stat *buf, int flags)) dlsym (RTLD_NEXT, "__fxstatat"); new_path = redirect_path (pathname); - ret = ___fxstatat (ver, dirfd, new_path, buf, flags); - g_free (new_path); - - return ret; + return ___fxstatat (ver, dirfd, new_path, buf, flags); } int __fxstatat64(int ver, int dirfd, const char *pathname, struct stat64 *buf, int flags) { int (*___fxstatat64) (int ver, int dirfd, const char *pathname, struct stat64 *buf, int flags); - gchar *new_path = NULL; - int ret; + g_autofree gchar *new_path = NULL; ___fxstatat64 = (int (*)(int ver, int dirfd, const char *pathname, struct stat64 *buf, int flags)) dlsym (RTLD_NEXT, "__fxstatat64"); new_path = redirect_path (pathname); - ret = ___fxstatat64 (ver, dirfd, new_path, buf, flags); - g_free (new_path); - - return ret; + return ___fxstatat64 (ver, dirfd, new_path, buf, flags); } DIR * opendir (const char *name) { DIR *(*_opendir) (const char *name); - gchar *new_path = NULL; - DIR *result; + g_autofree gchar *new_path = NULL; _opendir = (DIR *(*)(const char *name)) dlsym (RTLD_NEXT, "opendir"); new_path = redirect_path (name); - result = _opendir (new_path); - g_free (new_path); - - return result; + return _opendir (new_path); } int mkdir (const char *pathname, mode_t mode) { int (*_mkdir) (const char *pathname, mode_t mode); - gchar *new_path = NULL; - int result; + g_autofree gchar *new_path = NULL; _mkdir = (int (*)(const char *pathname, mode_t mode)) dlsym (RTLD_NEXT, "mkdir"); new_path = redirect_path (pathname); - result = _mkdir (new_path, mode); - g_free (new_path); - - return result; + return _mkdir (new_path, mode); } int @@ -503,16 +460,12 @@ int chmod (const char *path, mode_t mode) { int (*_chmod) (const char *path, mode_t mode); - gchar *new_path = NULL; - int result; + g_autofree gchar *new_path = NULL; _chmod = (int (*)(const char *path, mode_t mode)) dlsym (RTLD_NEXT, "chmod"); new_path = redirect_path (path); - result = _chmod (new_path, mode); - g_free (new_path); - - return result; + return _chmod (new_path, mode); } int @@ -566,8 +519,10 @@ ioctl (int d, unsigned long request, ...) static void add_port_redirect (int requested_port, int redirected_port) { - GKeyFile *file; - gchar *path, *name, *data; + g_autoptr(GKeyFile) file = NULL; + g_autofree gchar *path = NULL; + g_autofree gchar *name = NULL; + g_autofree gchar *data = NULL; file = g_key_file_new (); path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".port-redirects", NULL); @@ -575,32 +530,25 @@ add_port_redirect (int requested_port, int redirected_port) name = g_strdup_printf ("%d", requested_port); g_key_file_set_integer (file, name, "redirected", redirected_port); - g_free (name); data = g_key_file_to_data (file, NULL, NULL); g_file_set_contents (path, data, -1, NULL); - g_free (data); - g_free (path); - - g_key_file_free (file); } static int find_port_redirect (int port) { - GKeyFile *file; - gchar *path, *name; + g_autoptr(GKeyFile) file = NULL; + g_autofree gchar *path = NULL; + g_autofree gchar *name = NULL; int redirected_port; file = g_key_file_new (); path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".port-redirects", NULL); g_key_file_load_from_file (file, path, G_KEY_FILE_NONE, NULL); - g_free (path); name = g_strdup_printf ("%d", port); redirected_port = g_key_file_get_integer (file, name, "redirected", NULL); - g_free (name); - g_key_file_free (file); return redirected_port; } @@ -625,10 +573,9 @@ bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen) path = ((const struct sockaddr_un *) addr)->sun_path; if (path[0] != '\0') { - gchar *new_path = redirect_path (path); + g_autofree gchar *new_path = redirect_path (path); memcpy (&temp_addr_un, addr, sizeof (struct sockaddr_un)); strncpy (temp_addr_un.sun_path, new_path, sizeof (temp_addr_un.sun_path) - 1); - g_free (new_path); modified_addr = (struct sockaddr *) &temp_addr_un; } break; @@ -703,10 +650,9 @@ connect (int sockfd, const struct sockaddr *addr, socklen_t addrlen) path = ((const struct sockaddr_un *) addr)->sun_path; if (path[0] != '\0') { - gchar *new_path = redirect_path (path); + g_autofree gchar *new_path = redirect_path (path); memcpy (&temp_addr_un, addr, addrlen); strncpy (temp_addr_un.sun_path, new_path, sizeof (temp_addr_un.sun_path) - 1); - g_free (new_path); modified_addr = (struct sockaddr *) &temp_addr_un; } break; @@ -754,10 +700,9 @@ sendto (int sockfd, const void *buf, size_t len, int flags, const struct sockadd path = ((const struct sockaddr_un *) dest_addr)->sun_path; if (path[0] != '\0') { - gchar *new_path = redirect_path (path); + g_autofree gchar *new_path = redirect_path (path); memcpy (&temp_addr_un, dest_addr, sizeof (struct sockaddr_un)); strncpy (temp_addr_un.sun_path, new_path, sizeof (temp_addr_un.sun_path) - 1); - g_free (new_path); modified_addr = (struct sockaddr *) &temp_addr_un; } break; @@ -815,30 +760,29 @@ free_user (gpointer data) static void load_passwd_file (void) { - gchar *path, *data = NULL, **lines; + g_autofree gchar *path = NULL; + g_autofree gchar *data = NULL; + g_auto(GStrv) lines = NULL; gint i; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_list_free_full (user_entries, free_user); user_entries = NULL; getpwent_link = NULL; path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL); - g_file_get_contents (path, &data, NULL, &error); - g_free (path); - if (error) + if (!g_file_get_contents (path, &data, NULL, &error)) + { g_warning ("Error loading passwd file: %s", error->message); - g_clear_error (&error); - - if (!data) return; + } lines = g_strsplit (data, "\n", -1); - g_free (data); for (i = 0; lines[i]; i++) { - gchar *line, **fields; + const gchar *line; + g_auto(GStrv) fields = NULL; line = g_strstrip (lines[i]); fields = g_strsplit (line, ":", -1); @@ -855,9 +799,7 @@ load_passwd_file (void) entry->pw_shell = g_strdup (fields[6]); user_entries = g_list_append (user_entries, entry); } - g_strfreev (fields); } - g_strfreev (lines); } struct passwd * @@ -944,29 +886,28 @@ free_group (gpointer data) static void load_group_file (void) { - gchar *path, *data = NULL, **lines; + g_autofree gchar *path = NULL; + g_autofree gchar *data = NULL; + g_auto(GStrv) lines = NULL; gint i; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_list_free_full (group_entries, free_group); group_entries = NULL; path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "group", NULL); - g_file_get_contents (path, &data, NULL, &error); - g_free (path); - if (error) + if (!g_file_get_contents (path, &data, NULL, &error)) + { g_warning ("Error loading group file: %s", error->message); - g_clear_error (&error); - - if (!data) return; + } lines = g_strsplit (data, "\n", -1); - g_free (data); for (i = 0; lines[i]; i++) { - gchar *line, **fields; + const gchar *line; + g_auto(GStrv) fields = NULL; line = g_strstrip (lines[i]); fields = g_strsplit (line, ":", -1); @@ -980,9 +921,7 @@ load_group_file (void) entry->gr_mem = g_strsplit (fields[3], ",", -1); group_entries = g_list_append (group_entries, entry); } - g_strfreev (fields); } - g_strfreev (lines); } struct group * @@ -1040,7 +979,7 @@ pam_start (const char *service_name, const char *user, const struct pam_conv *co connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new (""); g_string_append_printf (status, "%s START", handle->id); @@ -1048,7 +987,6 @@ pam_start (const char *service_name, const char *user, const struct pam_conv *co if (user) g_string_append_printf (status, " USER=%s", user); status_notify ("%s", status->str); - g_string_free (status, TRUE); } handle->service_name = strdup (service_name); @@ -1073,7 +1011,7 @@ pam_authenticate (pam_handle_t *pamh, int flags) connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new (""); g_string_append_printf (status, "%s AUTHENTICATE", pamh->id); @@ -1083,7 +1021,6 @@ pam_authenticate (pam_handle_t *pamh, int flags) g_string_append (status, " DISALLOW_NULL_AUTHTOK"); status_notify ("%s", status->str); - g_string_free (status, TRUE); } if (strcmp (pamh->service_name, "test-remote") == 0) @@ -1437,13 +1374,13 @@ pam_get_item (const pam_handle_t *pamh, int item_type, const void **item) int pam_open_session (pam_handle_t *pamh, int flags) { - GVariant *result; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GError) error = NULL; connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new (""); g_string_append_printf (status, "%s OPEN-SESSION", pamh->id); @@ -1451,7 +1388,6 @@ pam_open_session (pam_handle_t *pamh, int flags) g_string_append (status, " SILENT"); status_notify ("%s", status->str); - g_string_free (status, TRUE); } if (strcmp (pamh->user, "session-error") == 0) @@ -1478,18 +1414,15 @@ pam_open_session (pam_handle_t *pamh, int flags) &error); if (result) { - gchar *e; + g_autofree gchar *e = NULL; const gchar *id; g_variant_get (result, "(&so)", &id, NULL); e = g_strdup_printf ("XDG_SESSION_ID=%s", id); pam_putenv (pamh, e); - g_free (e); - g_variant_unref (result); } else g_printerr ("Failed to create logind session: %s\n", error->message); - g_clear_error (&error); return PAM_SUCCESS; } @@ -1500,7 +1433,7 @@ pam_close_session (pam_handle_t *pamh, int flags) connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new (""); g_string_append_printf (status, "%s CLOSE-SESSION", pamh->id); @@ -1508,7 +1441,6 @@ pam_close_session (pam_handle_t *pamh, int flags) g_string_append (status, " SILENT"); status_notify ("%s", status->str); - g_string_free (status, TRUE); } return PAM_SUCCESS; @@ -1520,7 +1452,7 @@ pam_acct_mgmt (pam_handle_t *pamh, int flags) connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new (""); g_string_append_printf (status, "%s ACCT-MGMT", pamh->id); @@ -1530,7 +1462,6 @@ pam_acct_mgmt (pam_handle_t *pamh, int flags) g_string_append (status, " DISALLOW_NULL_AUTHTOK"); status_notify ("%s", status->str); - g_string_free (status, TRUE); } if (!pamh->user) @@ -1557,7 +1488,7 @@ pam_chauthtok (pam_handle_t *pamh, int flags) connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new (""); g_string_append_printf (status, "%s CHAUTHTOK", pamh->id); @@ -1567,7 +1498,6 @@ pam_chauthtok (pam_handle_t *pamh, int flags) g_string_append (status, " CHANGE_EXPIRED_AUTHTOK"); status_notify ("%s", status->str); - g_string_free (status, TRUE); } msg = malloc (sizeof (struct pam_message *) * 1); @@ -1603,12 +1533,12 @@ pam_chauthtok (pam_handle_t *pamh, int flags) int pam_setcred (pam_handle_t *pamh, int flags) { - gchar *e; + g_autofree gchar *e = NULL; connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new (""); g_string_append_printf (status, "%s SETCRED", pamh->id); @@ -1624,13 +1554,11 @@ pam_setcred (pam_handle_t *pamh, int flags) g_string_append (status, " REFRESH_CRED"); status_notify ("%s", status->str); - g_string_free (status, TRUE); } /* Put the test directories into the path */ e = g_strdup_printf ("PATH=%s/tests/src/.libs:%s/tests/src:%s/tests/src:%s/src:%s", BUILDDIR, BUILDDIR, SRCDIR, BUILDDIR, pam_getenv (pamh, "PATH")); pam_putenv (pamh, e); - g_free (e); if (strcmp (pamh->user, "cred-error") == 0) return PAM_CRED_ERR; @@ -1675,12 +1603,11 @@ pam_end (pam_handle_t *pamh, int pam_status) connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new (""); g_string_append_printf (status, "%s END", pamh->id); status_notify ("%s", status->str); - g_string_free (status, TRUE); } free (pamh->id); @@ -1786,7 +1713,7 @@ pututxline (const struct utmpx *ut) connect_status (); if (g_key_file_get_boolean (config, "test-utmp-config", "check-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new ("UTMP"); switch (ut->ut_type) @@ -1815,7 +1742,6 @@ pututxline (const struct utmpx *ut) if (ut->ut_host) g_string_append_printf (status, " HOST=%s", ut->ut_host); status_notify ("%s", status->str); - g_string_free (status, TRUE); } return (struct utmpx *)ut; @@ -1832,7 +1758,7 @@ updwtmp (const char *wtmp_file, const struct utmp *ut) connect_status (); if (g_key_file_get_boolean (config, "test-utmp-config", "check-events", NULL)) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new ("WTMP"); g_string_append_printf (status, " FILE=%s", wtmp_file); @@ -1862,7 +1788,6 @@ updwtmp (const char *wtmp_file, const struct utmp *ut) if (ut->ut_host) g_string_append_printf (status, " HOST=%s", ut->ut_host); status_notify ("%s", status->str); - g_string_free (status, TRUE); } } @@ -1877,8 +1802,6 @@ xcb_connection_t * xcb_connect_to_display_with_auth_info (const char *display, xcb_auth_info_t *auth, int *screen) { xcb_connection_t *c; - gchar *socket_path; - GError *error = NULL; c = malloc (sizeof (xcb_connection_t)); c->display = g_strdup (display); @@ -1891,32 +1814,32 @@ xcb_connect_to_display_with_auth_info (const char *display, xcb_auth_info_t *aut if (c->error == 0) { + g_autoptr(GError) error = NULL; + c->socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); - if (error) - g_printerr ("%s\n", error->message); - g_clear_error (&error); if (c->socket == NULL) + { + g_printerr ("%s\n", error->message); c->error = XCB_CONN_ERROR; + } } if (c->error == 0) { - gchar *d; - GSocketAddress *address; + g_autofree gchar *d = NULL; + g_autofree gchar *socket_path = NULL; + g_autoptr(GSocketAddress) address = NULL; + g_autoptr(GError) error = NULL; /* Skip the hostname, we'll assume it's localhost */ d = g_strdup_printf (".x%s", strchr (display, ':')); - socket_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), d, NULL); - g_free (d); address = g_unix_socket_address_new (socket_path); if (!g_socket_connect (c->socket, address, NULL, &error)) - c->error = XCB_CONN_ERROR; - g_object_unref (address); - if (error) + { g_printerr ("Failed to connect to X socket %s: %s\n", socket_path, error->message); - g_free (socket_path); - g_clear_error (&error); + c->error = XCB_CONN_ERROR; + } } // FIXME: Send auth info @@ -1964,7 +1887,7 @@ audit_log_acct_message (int audit_fd, int type, const char *pgname, const char *op, const char *name, unsigned int id, const char *host, const char *addr, const char *tty, int result) { - gchar *type_string; + g_autofree gchar *type_string = NULL; connect_status (); if (!g_key_file_get_boolean (config, "test-audit-config", "check-events", NULL)) @@ -1986,8 +1909,6 @@ audit_log_acct_message (int audit_fd, int type, const char *pgname, status_notify ("AUDIT LOG-ACCT TYPE=%s PGNAME=%s OP=%s NAME=%s ID=%u HOST=%s ADDR=%s TTY=%s RESULT=%d", type_string, pgname ? pgname : "", op ? op : "", name ? name : "", id, host ? host : "", addr ? addr : "", tty ? tty : "", result); - g_free (type_string); - return 1; } diff --git a/tests/src/status.c b/tests/src/status.c index 08258685..02d66e40 100644 --- a/tests/src/status.c +++ b/tests/src/status.c @@ -21,10 +21,11 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) ssize_t n_read; const gchar *c, *start; int l; - gchar *id, *name = NULL; + g_autofree gchar *id = NULL; + g_autofree gchar *name = NULL; gboolean id_matches; - GHashTable *params; - GError *error = NULL; + g_autoptr(GHashTable) params = NULL; + g_autoptr(GError) error = NULL; n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error); if (n_read > 0) @@ -36,7 +37,6 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) else g_warning ("Error reading from socket: %s", error->message); } - g_clear_error (&error); if (n_read == 0) { if (request_func) @@ -58,7 +58,6 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) } id = g_strdup_printf ("%.*s", l, start); id_matches = g_strcmp0 (id, filter_id) == 0; - g_free (id); if (!id_matches) return TRUE; @@ -77,7 +76,8 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) while (TRUE) { const gchar *start; - gchar *param_name, *param_value; + g_autofree gchar *param_name = NULL; + g_autofree gchar *param_value = NULL; while (isspace (*c)) c++; @@ -97,7 +97,7 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) if (*c == '\"') { gboolean escaped = FALSE; - GString *value; + g_autoptr(GString) value = NULL; c++; value = g_string_new (""); @@ -119,8 +119,7 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) g_string_append_c (value, *c); c++; } - param_value = value->str; - g_string_free (value, FALSE); + param_value = g_strdup (value->str); if (*c == '\"') c++; } @@ -135,48 +134,38 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) else param_value = g_strdup (""); - g_hash_table_insert (params, param_name, param_value); + g_hash_table_insert (params, g_steal_pointer (¶m_name), g_steal_pointer (¶m_value)); } request_func (name, params); - g_free (name); - g_hash_table_unref (params); - return TRUE; } gboolean status_connect (StatusRequestFunc request_cb, const gchar *id) { - gchar *path; - GSocketAddress *address; - gboolean result; + g_autofree gchar *path = NULL; + g_autoptr(GSocketAddress) address = NULL; GSource *source; - GError *error = NULL; + g_autoptr(GError) error = NULL; request_func = request_cb; filter_id = g_strdup (id); status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); - if (error) - g_printerr ("Unable to open socket for status: %s\n", error->message); - g_clear_error (&error); if (!status_socket) + { + g_printerr ("Unable to open socket for status: %s\n", error->message); return FALSE; + } path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".s", NULL); address = g_unix_socket_address_new (path); - result = g_socket_connect (status_socket, address, NULL, &error); - g_object_unref (address); - if (error) - g_printerr ("Failed to connect to status socket %s: %s\n", path, error->message); - g_clear_error (&error); - g_free (path); - if (!result) + if (!g_socket_connect (status_socket, address, NULL, &error)) { - g_object_unref (status_socket); - status_socket = NULL; + g_printerr ("Failed to connect to status socket %s: %s\n", path, error->message); + g_clear_object (&status_socket); return FALSE; } @@ -190,8 +179,6 @@ status_connect (StatusRequestFunc request_cb, const gchar *id) void status_notify (const gchar *format, ...) { - gboolean written = FALSE; - gchar status[1024]; va_list ap; @@ -201,17 +188,16 @@ status_notify (const gchar *format, ...) if (status_socket) { - GError *error = NULL; + g_autoptr(GError) error = NULL; int length; length = strlen (status); - written = g_socket_send (status_socket, (gchar *) &length, sizeof (length), NULL, &error) == sizeof (length) && - g_socket_send (status_socket, status, strlen (status), NULL, &error) == strlen (status); - if (error) + if (g_socket_send (status_socket, (gchar *) &length, sizeof (length), NULL, &error) < 0 || + g_socket_send (status_socket, status, strlen (status), NULL, &error) < 0) g_printerr ("Failed to write to status socket: %s\n", error->message); - g_clear_error (&error); + else + return; } - if (!written) - g_printerr ("%s\n", status); + g_printerr ("%s\n", status); } diff --git a/tests/src/test-gobject-greeter.c b/tests/src/test-gobject-greeter.c index c7535ac6..455a1321 100644 --- a/tests/src/test-gobject-greeter.c +++ b/tests/src/test-gobject-greeter.c @@ -114,56 +114,52 @@ static void start_session_finished (GObject *object, GAsyncResult *result, gpointer data) { LightDMGreeter *greeter = LIGHTDM_GREETER (object); - GError *error = NULL; + g_autoptr(GError) error = NULL; if (!lightdm_greeter_start_session_finish (greeter, result, &error)) status_notify ("%s SESSION-FAILED ERROR=%s", greeter_id, error->message); - g_clear_error (&error); } static void write_shared_data_finished (GObject *object, GAsyncResult *result, gpointer data) { LightDMGreeter *greeter = LIGHTDM_GREETER (object); - gchar *dir, *path, *test_data; + g_autofree gchar *dir = NULL; + g_autofree gchar *path = NULL; + g_autofree gchar *test_data = data; FILE *f; - GError *error = NULL; + g_autoptr(GError) error = NULL; dir = lightdm_greeter_ensure_shared_data_dir_finish (greeter, result, &error); if (!dir) { status_notify ("%s WRITE-SHARED-DATA ERROR=%s", greeter_id, error->message); - g_clear_error (&error); return; } path = g_build_filename (dir, "data", NULL); - test_data = data; if (!(f = fopen (path, "w")) || fprintf (f, "%s", test_data) < 0) status_notify ("%s WRITE-SHARED-DATA ERROR=%s", greeter_id, strerror (errno)); else status_notify ("%s WRITE-SHARED-DATA RESULT=TRUE", greeter_id); - g_free (test_data); if (f) fclose (f); - g_free (path); - g_free (dir); } static void read_shared_data_finished (GObject *object, GAsyncResult *result, gpointer data) { LightDMGreeter *greeter = LIGHTDM_GREETER (object); - gchar *dir, *path; - gchar *contents = NULL; - GError *error = NULL; + g_autofree gchar *dir = NULL; + g_autofree gchar *path = NULL; + g_autofree gchar *contents = NULL; + g_autoptr(GError) error = NULL; dir = lightdm_greeter_ensure_shared_data_dir_finish (greeter, result, &error); if (!dir) { status_notify ("%s READ-SHARED-DATA ERROR=%s", greeter_id, error->message); - g_clear_error (&error); return; } @@ -172,16 +168,11 @@ read_shared_data_finished (GObject *object, GAsyncResult *result, gpointer data) status_notify ("%s READ-SHARED-DATA DATA=%s", greeter_id, contents); else status_notify ("%s READ-SHARED-DATA ERROR=%s", greeter_id, error->message); - g_free (path); - g_free (contents); - g_clear_error (&error); } static void request_cb (const gchar *name, GHashTable *params) { - GError *error = NULL; - if (!name) { g_main_loop_quit (loop); @@ -193,56 +184,44 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "AUTHENTICATE") == 0) { + g_autoptr(GError) error = NULL; if (!lightdm_greeter_authenticate (greeter, g_hash_table_lookup (params, "USERNAME"), &error)) - { status_notify ("%s FAIL-AUTHENTICATE ERROR=%s", greeter_id, error->message); - g_clear_error (&error); - } } else if (strcmp (name, "AUTHENTICATE-GUEST") == 0) { + g_autoptr(GError) error = NULL; if (!lightdm_greeter_authenticate_as_guest (greeter, &error)) - { status_notify ("%s FAIL-AUTHENTICATE-GUEST ERROR=%s", greeter_id, error->message); - g_clear_error (&error); - } } else if (strcmp (name, "AUTHENTICATE-AUTOLOGIN") == 0) { + g_autoptr(GError) error = NULL; if (!lightdm_greeter_authenticate_autologin (greeter, &error)) - { status_notify ("%s FAIL-AUTHENTICATE-AUTOLOGIN ERROR=%s", greeter_id, error->message); - g_clear_error (&error); - } } else if (strcmp (name, "AUTHENTICATE-REMOTE") == 0) { + g_autoptr(GError) error = NULL; if (!lightdm_greeter_authenticate_remote (greeter, g_hash_table_lookup (params, "SESSION"), NULL, &error)) - { status_notify ("%s FAIL-AUTHENTICATE-REMOTE ERROR=%s", greeter_id, error->message); - g_clear_error (&error); - } } else if (strcmp (name, "RESPOND") == 0) { + g_autoptr(GError) error = NULL; if (!lightdm_greeter_respond (greeter, g_hash_table_lookup (params, "TEXT"), &error)) - { status_notify ("%s FAIL-RESPOND ERROR=%s", greeter_id, error->message); - g_clear_error (&error); - } } else if (strcmp (name, "CANCEL-AUTHENTICATION") == 0) { + g_autoptr(GError) error = NULL; if (!lightdm_greeter_cancel_authentication (greeter, &error)) - { status_notify ("%s FAIL-CANCEL-AUTHENTICATION ERROR=%s", greeter_id, error->message); - g_clear_error (&error); - } } else if (strcmp (name, "START-SESSION") == 0) @@ -280,9 +259,9 @@ request_cb (const gchar *name, GHashTable *params) LightDMUser *user; const gchar *username, *image, *background, *language, *layout, *session; const gchar * const * layouts; - gchar **fields = NULL; - gchar *layouts_text; - GString *status_text; + g_auto(GStrv) fields = NULL; + g_autofree gchar *layouts_text = NULL; + g_autoptr(GString) status_text = NULL; int i; username = g_hash_table_lookup (params, "USERNAME"); @@ -330,11 +309,8 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (fields[i], "UID") == 0) g_string_append_printf (status_text, " UID=%d", lightdm_user_get_uid (user)); } - g_strfreev (fields); - g_free (layouts_text); status_notify ("%s", status_text->str); - g_string_free (status_text, TRUE); } else if (strcmp (name, "LOG-USER-LIST") == 0) @@ -369,10 +345,9 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "SUSPEND") == 0) { - GError *error = NULL; + g_autoptr(GError) error = NULL; if (!lightdm_suspend (&error)) status_notify ("%s FAIL-SUSPEND", greeter_id); - g_clear_error (&error); } else if (strcmp (name, "GET-CAN-HIBERNATE") == 0) @@ -383,10 +358,9 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "HIBERNATE") == 0) { - GError *error = NULL; + g_autoptr(GError) error = NULL; if (!lightdm_hibernate (&error)) status_notify ("%s FAIL-HIBERNATE", greeter_id); - g_clear_error (&error); } else if (strcmp (name, "GET-CAN-RESTART") == 0) @@ -397,10 +371,9 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "RESTART") == 0) { - GError *error = NULL; + g_autoptr(GError) error = NULL; if (!lightdm_restart (&error)) status_notify ("%s FAIL-RESTART", greeter_id); - g_clear_error (&error); } else if (strcmp (name, "GET-CAN-SHUTDOWN") == 0) @@ -411,10 +384,9 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "SHUTDOWN") == 0) { - GError *error = NULL; + g_autoptr(GError) error = NULL; if (!lightdm_shutdown (&error)) status_notify ("%s FAIL-SHUTDOWN", greeter_id); - g_clear_error (&error); } } @@ -434,12 +406,11 @@ static void connect_finished (GObject *object, GAsyncResult *result, gpointer data) { LightDMGreeter *greeter = LIGHTDM_GREETER (object); - GError *error = NULL; + g_autoptr(GError) error = NULL; if (!lightdm_greeter_connect_to_daemon_finish (greeter, result, &error)) { status_notify ("%s FAIL-CONNECT-DAEMON ERROR=%s", greeter_id, error->message); - g_clear_error (&error); exit_code = EXIT_FAILURE; g_main_loop_quit (loop); return; @@ -453,8 +424,9 @@ connect_finished (GObject *object, GAsyncResult *result, gpointer data) int main (int argc, char **argv) { - gchar *display, *xdg_seat, *xdg_vtnr, *xdg_session_cookie, *xdg_session_class, *xdg_session_type, *mir_server_host_socket, *mir_vt, *mir_id, *path; - GString *status_text; + const gchar *display, *xdg_seat, *xdg_vtnr, *xdg_session_cookie, *xdg_session_class, *xdg_session_type, *mir_server_host_socket, *mir_vt, *mir_id; + g_autofree gchar *path = NULL; + g_autoptr(GString) status_text = NULL; #if !defined(GLIB_VERSION_2_36) g_type_init (); @@ -505,12 +477,10 @@ main (int argc, char **argv) if (mir_vt > 0) g_string_append_printf (status_text, " MIR_SERVER_VT=%s", mir_vt); status_notify ("%s", status_text->str); - g_string_free (status_text, TRUE); config = g_key_file_new (); path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL); g_key_file_load_from_file (config, path, G_KEY_FILE_NONE, NULL); - g_free (path); if (g_key_file_get_boolean (config, "test-greeter-config", "exit-on-startup", NULL)) { diff --git a/tests/src/test-qt-greeter.cpp b/tests/src/test-qt-greeter.cpp index d43a9960..87edc729 100644 --- a/tests/src/test-qt-greeter.cpp +++ b/tests/src/test-qt-greeter.cpp @@ -250,7 +250,7 @@ int main(int argc, char *argv[]) { gchar *display, *xdg_seat, *xdg_vtnr, *xdg_session_cookie, *xdg_session_class; - GString *status_text; + g_autoptr(GString) status_text = NULL; #if !defined(GLIB_VERSION_2_36) g_type_init (); @@ -294,7 +294,6 @@ main(int argc, char *argv[]) if (xdg_session_class) g_string_append_printf (status_text, " XDG_SESSION_CLASS=%s", xdg_session_class); status_notify ("%s", status_text->str); - g_string_free (status_text, TRUE); config = new QSettings (g_build_filename (getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), QSettings::IniFormat); diff --git a/tests/src/test-runner.c b/tests/src/test-runner.c index 70d10a7a..4ad0bc13 100644 --- a/tests/src/test-runner.c +++ b/tests/src/test-runner.c @@ -321,12 +321,11 @@ get_script_line (const gchar *prefix) /* Ignore lines with other prefixes */ if (prefix) { - gchar *p; + g_autofree gchar *p = NULL; gboolean matches; p = get_prefix (line->text); matches = strcmp (prefix, p) == 0; - g_free (p); if (!matches) continue; @@ -349,74 +348,60 @@ stop_loop (gpointer user_data) static void switch_to_greeter_done_cb (GObject *bus, GAsyncResult *result, gpointer data) { - GVariant *r; - GError *error = NULL; + g_autoptr(GVariant) r = NULL; + g_autoptr(GError) error = NULL; r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error); - if (error) - g_warning ("Failed to switch to greeter: %s\n", error->message); - g_clear_error (&error); - if (r) - { check_status ("RUNNER SWITCH-TO-GREETER"); - g_variant_unref (r); - } else + { + g_warning ("Failed to switch to greeter: %s\n", error->message); check_status ("RUNNER SWITCH-TO-GREETER FAILED"); + } } static void switch_to_user_done_cb (GObject *bus, GAsyncResult *result, gpointer data) { - GVariant *r; - GError *error = NULL; - gchar *username = data, *status_text; + g_autoptr(GVariant) r = NULL; + g_autofree gchar *username = data; + g_autofree gchar *status_text = NULL; + g_autoptr(GError) error = NULL; r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error); - if (error) - g_warning ("Failed to switch to user: %s\n", error->message); - g_clear_error (&error); - if (r) - { status_text = g_strdup_printf ("RUNNER SWITCH-TO-USER USERNAME=%s", username); - g_variant_unref (r); - } else + { + g_warning ("Failed to switch to user: %s\n", error->message); status_text = g_strdup_printf ("RUNNER SWITCH-TO-USER USERNAME=%s FAILED", username); + } check_status (status_text); - - g_free (status_text); - g_free (username); } static void switch_to_guest_done_cb (GObject *bus, GAsyncResult *result, gpointer data) { - GVariant *r; - GError *error = NULL; + g_autoptr(GVariant) r = NULL; + g_autoptr(GError) error = NULL; r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error); - if (error) - g_warning ("Failed to switch to guest: %s\n", error->message); - g_clear_error (&error); - if (r) - { check_status ("RUNNER SWITCH-TO-GUEST"); - g_variant_unref (r); - } else + { + g_warning ("Failed to switch to guest: %s\n", error->message); check_status ("RUNNER SWITCH-TO-GUEST FAILED"); + } } static void handle_command (const gchar *command) { const gchar *c; - gchar *name = NULL; - GHashTable *params; + g_autofree gchar *name = NULL; + g_autoptr(GHashTable) params = NULL; c = command; while (*c && !isspace (*c)) @@ -447,7 +432,7 @@ handle_command (const gchar *command) if (*c == '\"') { gboolean escaped = FALSE; - GString *value; + g_autoptr(GString) value = NULL; c++; value = g_string_new (""); @@ -469,8 +454,7 @@ handle_command (const gchar *command) g_string_append_c (value, *c); c++; } - param_value = value->str; - g_string_free (value, FALSE); + param_value = g_strdup (value->str); if (*c == '\"') c++; } @@ -493,7 +477,7 @@ handle_command (const gchar *command) GString *command_line; gchar **lightdm_argv; pid_t lightdm_pid; - GError *error = NULL; + g_autoptr(GError) error = NULL; command_line = g_string_new ("lightdm"); if (getenv ("DEBUG")) @@ -509,20 +493,19 @@ handle_command (const gchar *command) g_warning ("Error parsing command line: %s", error->message); quit (EXIT_FAILURE); } - g_clear_error (&error); if (!g_spawn_async (NULL, lightdm_argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, &lightdm_pid, &error)) { g_warning ("Error launching LightDM: %s", error->message); quit (EXIT_FAILURE); } - g_clear_error (&error); lightdm_process = watch_process (lightdm_pid); check_status ("RUNNER DAEMON-START"); } else if (strcmp (name, "WAIT") == 0) { + g_autoptr(GMainLoop) loop = NULL; const gchar *v; int duration; @@ -532,14 +515,13 @@ handle_command (const gchar *command) status_timeout = 0; /* Use a main loop so that our DBus functions are still responsive */ - GMainLoop *loop = g_main_loop_new (NULL, FALSE); + loop = g_main_loop_new (NULL, FALSE); v = g_hash_table_lookup (params, "DURATION"); duration = v ? atoi (v) : 1; if (duration < 1) duration = 1; g_timeout_add_seconds (duration, stop_loop, loop); g_main_loop_run (loop); - g_main_loop_unref (loop); /* Restart status timeout */ status_timeout = g_timeout_add (status_timeout_ms, status_timeout_cb, NULL); @@ -560,7 +542,7 @@ handle_command (const gchar *command) } else if (strcmp (name, "ADD-LOCAL-X-SEAT") == 0) { - GVariant *result; + g_autoptr(GVariant) result = NULL; const gchar *v; v = g_hash_table_lookup (params, "DISPLAY"); @@ -575,7 +557,6 @@ handle_command (const gchar *command) G_MAXINT, NULL, NULL); - g_variant_unref (result); } else if (strcmp (name, "UPDATE-SEAT") == 0) { @@ -588,7 +569,7 @@ handle_command (const gchar *command) { const gchar *v; GVariantBuilder invalidated_properties; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_variant_builder_init (&invalidated_properties, G_VARIANT_TYPE_ARRAY); @@ -612,16 +593,14 @@ handle_command (const gchar *command) g_variant_builder_add (&invalidated_properties, "s", "ActiveSession"); } - g_dbus_connection_emit_signal (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), - NULL, - seat->path, - "org.freedesktop.DBus.Properties", - "PropertiesChanged", - g_variant_new ("(sa{sv}as)", "org.freedesktop.login1.Seat", NULL, &invalidated_properties), - &error); - if (error) + if (!g_dbus_connection_emit_signal (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), + NULL, + seat->path, + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + g_variant_new ("(sa{sv}as)", "org.freedesktop.login1.Seat", NULL, &invalidated_properties), + &error)) g_warning ("Failed to emit PropertiesChanged: %s", error->message); - g_clear_error (&error); } } else if (strcmp (name, "REMOVE-SEAT") == 0) @@ -632,9 +611,9 @@ handle_command (const gchar *command) } else if (strcmp (name, "LIST-SEATS") == 0) { - GVariant *result, *value; - GString *status; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GString) status = NULL; + g_autoptr(GError) error = NULL; result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), "org.freedesktop.DisplayManager", @@ -651,6 +630,7 @@ handle_command (const gchar *command) status = g_string_new ("RUNNER LIST-SEATS"); if (result) { + g_autoptr(GVariant) value = NULL; GVariantIter *iter; const gchar *path; int i = 0; @@ -665,8 +645,6 @@ handle_command (const gchar *command) g_string_append (status, path); i++; } - g_variant_unref (value); - g_variant_unref (result); } else { @@ -674,17 +652,15 @@ handle_command (const gchar *command) g_string_append_printf (status, " ERROR=SERVICE_UNKNOWN"); else g_string_append_printf (status, " ERROR=%s", error->message); - g_clear_error (&error); } check_status (status->str); - g_string_free (status, TRUE); } else if (strcmp (name, "LIST-SESSIONS") == 0) { - GVariant *result, *value; - GString *status; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GString) status = NULL; + g_autoptr(GError) error = NULL; result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), "org.freedesktop.DisplayManager", @@ -701,6 +677,7 @@ handle_command (const gchar *command) status = g_string_new ("RUNNER LIST-SESSIONS"); if (result) { + g_autoptr(GVariant) value = NULL; GVariantIter *iter; const gchar *path; int i = 0; @@ -716,8 +693,6 @@ handle_command (const gchar *command) g_string_append (status, path); i++; } - g_variant_unref (value); - g_variant_unref (result); } else { @@ -725,17 +700,15 @@ handle_command (const gchar *command) g_string_append_printf (status, " ERROR=SERVICE_UNKNOWN"); else g_string_append_printf (status, " ERROR=%s", error->message); - g_clear_error (&error); } check_status (status->str); - g_string_free (status, TRUE); } else if (strcmp (name, "SEAT-CAN-SWITCH") == 0) { - GVariant *result, *value; - GString *status; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GString) status = NULL; + g_autoptr(GError) error = NULL; result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), "org.freedesktop.DisplayManager", @@ -752,10 +725,9 @@ handle_command (const gchar *command) status = g_string_new ("RUNNER SEAT-CAN-SWITCH"); if (result) { + g_autoptr(GVariant) value = NULL; g_variant_get (result, "(v)", &value); g_string_append_printf (status, " CAN-SWITCH=%s", g_variant_get_boolean (value) ? "TRUE" : "FALSE"); - g_variant_unref (value); - g_variant_unref (result); } else { @@ -763,17 +735,15 @@ handle_command (const gchar *command) g_string_append_printf (status, " ERROR=SERVICE_UNKNOWN"); else g_string_append_printf (status, " ERROR=%s", error->message); - g_clear_error (&error); } check_status (status->str); - g_string_free (status, TRUE); } else if (strcmp (name, "SEAT-HAS-GUEST-ACCOUNT") == 0) { - GVariant *result, *value; - GString *status; - GError *error = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GString) status = NULL; + g_autoptr(GError) error = NULL; result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), "org.freedesktop.DisplayManager", @@ -790,10 +760,9 @@ handle_command (const gchar *command) status = g_string_new ("RUNNER SEAT-HAS-GUEST-ACCOUNT"); if (result) { + g_autoptr(GVariant) value = NULL; g_variant_get (result, "(v)", &value); g_string_append_printf (status, " HAS-GUEST-ACCOUNT=%s", g_variant_get_boolean (value) ? "TRUE" : "FALSE"); - g_variant_unref (value); - g_variant_unref (result); } else { @@ -801,11 +770,9 @@ handle_command (const gchar *command) g_string_append_printf (status, " ERROR=SERVICE_UNKNOWN"); else g_string_append_printf (status, " ERROR=%s", error->message); - g_clear_error (&error); } check_status (status->str); - g_string_free (status, TRUE); } else if (strcmp (name, "SWITCH-TO-GREETER") == 0) { @@ -864,7 +831,7 @@ handle_command (const gchar *command) gchar **argv; GPid pid; Process *process; - GError *error = NULL; + g_autoptr(GError) error = NULL; xserver_args = g_hash_table_lookup (params, "ARGS"); if (!xserver_args) @@ -889,7 +856,7 @@ handle_command (const gchar *command) gchar **argv; GPid pid; Process *process; - GError *error = NULL; + g_autoptr(GError) error = NULL; vnc_client_args = g_hash_table_lookup (params, "ARGS"); if (!vnc_client_args) @@ -910,8 +877,9 @@ handle_command (const gchar *command) } else if (strcmp (name, "ADD-USER") == 0) { - gchar *status_text, *username; + const gchar *username; AccountsUser *user; + g_autofree gchar *status_text = NULL; username = g_hash_table_lookup (params, "USERNAME"); user = get_accounts_user_by_name (username); @@ -922,14 +890,12 @@ handle_command (const gchar *command) status_text = g_strdup_printf ("RUNNER ADD-USER USERNAME=%s", username); check_status (status_text); - g_free (status_text); } else if (strcmp (name, "UPDATE-USER") == 0) { - GString *status_text; gchar *username; AccountsUser *user; - GError *error = NULL; + g_autoptr(GString) status_text = NULL; status_text = g_string_new ("RUNNER UPDATE-USER USERNAME="); @@ -938,6 +904,8 @@ handle_command (const gchar *command) user = get_accounts_user_by_name (username); if (user) { + g_autoptr(GError) error = NULL; + if (g_hash_table_lookup (params, "NAME")) { user->user_name = g_strdup (g_hash_table_lookup (params, "NAME")); @@ -985,28 +953,25 @@ handle_command (const gchar *command) g_string_append_printf (status_text, " SESSION=%s", user->xsession); } - g_dbus_connection_emit_signal (accounts_connection, - NULL, - user->path, - "org.freedesktop.Accounts.User", - "Changed", - g_variant_new ("()"), - &error); + if (!g_dbus_connection_emit_signal (accounts_connection, + NULL, + user->path, + "org.freedesktop.Accounts.User", + "Changed", + g_variant_new ("()"), + &error)) + g_warning ("Failed to emit Changed: %s", error->message); } else g_warning ("Unknown user %s", username); - if (error) - g_warning ("Failed to emit Changed: %s", error->message); - g_clear_error (&error); - check_status (status_text->str); - g_string_free (status_text, TRUE); } else if (strcmp (name, "DELETE-USER") == 0) { - gchar *status_text, *username; + const gchar *username; AccountsUser *user; + g_autofree gchar *status_text = NULL; username = g_hash_table_lookup (params, "USERNAME"); user = get_accounts_user_by_name (username); @@ -1017,12 +982,12 @@ handle_command (const gchar *command) status_text = g_strdup_printf ("RUNNER DELETE-USER USERNAME=%s", username); check_status (status_text); - g_free (status_text); } else if (strcmp (name, "UNLOCK-SESSION") == 0) { - gchar *status_text, *id; + const gchar *id; Login1Session *session; + g_autofree gchar *status_text = NULL; id = g_hash_table_lookup (params, "SESSION"); session = find_login1_session (id); @@ -1037,7 +1002,6 @@ handle_command (const gchar *command) status_text = g_strdup_printf ("RUNNER UNLOCK-SESSION SESSION=%s", id); check_status (status_text); - g_free (status_text); } /* Forward to external processes */ else if (g_str_has_prefix (name, "SESSION-") || @@ -1052,13 +1016,12 @@ handle_command (const gchar *command) { StatusClient *client = link->data; int length; - GError *error = NULL; + g_autoptr(GError) error = NULL; length = strlen (command); if (g_socket_send (client->socket, (gchar *) &length, sizeof (length), NULL, &error) < 0 || g_socket_send (client->socket, command, strlen (command), NULL, &error) < 0) g_printerr ("Failed to write to client socket: %s\n", error->message); - g_clear_error (&error); } } else @@ -1066,9 +1029,6 @@ handle_command (const gchar *command) g_printerr ("Unknown command '%s'\n", name); quit (EXIT_FAILURE); } - - g_free (name); - g_hash_table_unref (params); } static void @@ -1113,7 +1073,7 @@ check_status (const gchar *status) { ScriptLine *line; gboolean result = FALSE; - gchar *prefix; + g_autofree gchar *prefix = NULL; if (stop) return; @@ -1126,12 +1086,10 @@ check_status (const gchar *status) /* Try and match against expected */ prefix = get_prefix (status); line = get_script_line (prefix); - g_free (prefix); if (line) { - gchar *full_pattern = g_strdup_printf ("^%s$", line->text); + g_autofree gchar *full_pattern = g_strdup_printf ("^%s$", line->text); result = g_regex_match_simple (full_pattern, status, 0, 0); - g_free (full_pattern); } if (!result) @@ -1158,19 +1116,18 @@ status_message_cb (GSocket *socket, GIOCondition condition, StatusClient *client int length; gchar buffer[1024]; ssize_t n_read; - GError *error = NULL; + g_autoptr(GError) error = NULL; n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error); if (n_read > 0) n_read = g_socket_receive (socket, buffer, length, NULL, &error); - if (error) + if (n_read < 0) { if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED)) n_read = 0; else g_warning ("Error reading from socket: %s", error->message); } - g_clear_error (&error); if (n_read == 0) { status_clients = g_list_remove (status_clients, client); @@ -1191,12 +1148,9 @@ static gboolean status_connect_cb (gpointer data) { GSocket *socket; - GError *error = NULL; + g_autoptr(GError) error = NULL; socket = g_socket_accept (status_socket, NULL, &error); - if (error) - g_warning ("Failed to accept status connection: %s", error->message); - g_clear_error (&error); if (socket) { StatusClient *client; @@ -1209,6 +1163,8 @@ status_connect_cb (gpointer data) g_source_set_callback (client->source, (GSourceFunc) status_message_cb, client, NULL); g_source_attach (client->source, NULL); } + else + g_warning ("Failed to accept status connection: %s", error->message); return TRUE; } @@ -1217,7 +1173,8 @@ static void load_script (const gchar *filename) { int i; - gchar *data, **lines; + g_autofree gchar *data = NULL; + g_auto(GStrv) lines = NULL; if (!g_file_get_contents (filename, &data, NULL, NULL)) { @@ -1226,7 +1183,6 @@ load_script (const gchar *filename) } lines = g_strsplit (data, "\n", -1); - g_free (data); /* Load lines with #? prefix as expected behaviour */ for (i = 0; lines[i]; i++) @@ -1241,7 +1197,6 @@ load_script (const gchar *filename) script = g_list_append (script, line); } } - g_strfreev (lines); } static void @@ -1300,25 +1255,22 @@ upower_name_acquired_cb (GDBusConnection *connection, { handle_upower_call, }; - GDBusNodeInfo *upower_info; - GError *error = NULL; + g_autoptr(GDBusNodeInfo) upower_info = NULL; + g_autoptr(GError) error = NULL; upower_info = g_dbus_node_info_new_for_xml (upower_interface, &error); - if (error) - g_warning ("Failed to parse D-Bus interface: %s", error->message); - g_clear_error (&error); if (!upower_info) + { + g_warning ("Failed to parse D-Bus interface: %s", error->message); return; - g_dbus_connection_register_object (connection, - "/org/freedesktop/UPower", - upower_info->interfaces[0], - &upower_vtable, - NULL, NULL, - &error); - if (error) + } + if (g_dbus_connection_register_object (connection, + "/org/freedesktop/UPower", + upower_info->interfaces[0], + &upower_vtable, + NULL, NULL, + &error) == 0) g_warning ("Failed to register UPower service: %s", error->message); - g_clear_error (&error); - g_dbus_node_info_unref (upower_info); service_count--; if (service_count == 0) @@ -1343,11 +1295,11 @@ static CKSession * open_ck_session (GDBusConnection *connection, GVariant *params) { CKSession *session; - GString *cookie; + g_autoptr(GString) cookie = NULL; GVariantIter *iter; const gchar *name; GVariant *value; - GError *error = NULL; + g_autoptr(GError) error = NULL; session = g_malloc0 (sizeof (CKSession)); ck_sessions = g_list_append (ck_sessions, session); @@ -1364,8 +1316,7 @@ open_ck_session (GDBusConnection *connection, GVariant *params) } } - session->cookie = cookie->str; - g_string_free (cookie, FALSE); + session->cookie = g_strdup (cookie->str); session->path = g_strdup_printf ("/org/freedesktop/ConsoleKit/Session%d", ck_session_index++); session->id = g_dbus_connection_register_object (connection, session->path, @@ -1374,9 +1325,8 @@ open_ck_session (GDBusConnection *connection, GVariant *params) session, NULL, &error); - if (error) + if (session->id == 0) g_warning ("Failed to register CK Session: %s", error->message); - g_clear_error (&error); return session; } @@ -1500,9 +1450,8 @@ handle_ck_session_call (GDBusConnection *connection, } else if (strcmp (method_name, "Activate") == 0) { - gchar *status = g_strdup_printf ("CONSOLE-KIT ACTIVATE-SESSION SESSION=%s", session->cookie); + g_autofree gchar *status = g_strdup_printf ("CONSOLE-KIT ACTIVATE-SESSION SESSION=%s", session->cookie); check_status (status); - g_free (status); g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); } @@ -1584,34 +1533,31 @@ ck_name_acquired_cb (GDBusConnection *connection, " <method name='Activate'/>" " </interface>" "</node>"; - GDBusNodeInfo *ck_info; - GError *error = NULL; + g_autoptr(GDBusNodeInfo) ck_info = NULL; + g_autoptr(GError) error = NULL; ck_info = g_dbus_node_info_new_for_xml (ck_interface, &error); - if (error) - g_warning ("Failed to parse D-Bus interface: %s", error->message); - g_clear_error (&error); if (!ck_info) + { + g_warning ("Failed to parse D-Bus interface: %s", error->message); return; + } if (g_key_file_get_boolean (config, "test-runner-config", "ck-no-xdg-runtime", NULL)) ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface_old, &error); else ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface, &error); - if (error) - g_warning ("Failed to parse D-Bus interface: %s", error->message); - g_clear_error (&error); if (!ck_session_info) + { + g_warning ("Failed to parse D-Bus interface: %s", error->message); return; - g_dbus_connection_register_object (connection, - "/org/freedesktop/ConsoleKit/Manager", - ck_info->interfaces[0], - &ck_vtable, - NULL, NULL, - &error); - if (error) + } + if (g_dbus_connection_register_object (connection, + "/org/freedesktop/ConsoleKit/Manager", + ck_info->interfaces[0], + &ck_vtable, + NULL, NULL, + &error) == 0) g_warning ("Failed to register console kit service: %s", error->message); - g_clear_error (&error); - g_dbus_node_info_unref (ck_info); service_count--; if (service_count == 0) @@ -1666,14 +1612,8 @@ handle_login1_seat_get_property (GDBusConnection *connection, { if (seat->active_session) { - gchar *path; - GVariant *ret; - - path = g_strdup_printf ("/org/freedesktop/login1/session/%s", seat->active_session); - ret = g_variant_new ("(so)", seat->active_session, path); - g_free (path); - - return ret; + g_autofree gchar *path = g_strdup_printf ("/org/freedesktop/login1/session/%s", seat->active_session); + return g_variant_new ("(so)", seat->active_session, path); } else return NULL; @@ -1686,8 +1626,8 @@ static Login1Seat * add_login1_seat (GDBusConnection *connection, const gchar *id, gboolean emit_signal) { Login1Seat *seat; - GError *error = NULL; - GDBusNodeInfo *login1_seat_info; + g_autoptr(GError) error = NULL; + g_autoptr(GDBusNodeInfo) login1_seat_info = NULL; const gchar *login1_seat_interface = "<node>" @@ -1713,36 +1653,32 @@ add_login1_seat (GDBusConnection *connection, const gchar *id, gboolean emit_sig seat->active_session = NULL; login1_seat_info = g_dbus_node_info_new_for_xml (login1_seat_interface, &error); - if (error) - g_warning ("Failed to parse login1 seat D-Bus interface: %s", error->message); - g_clear_error (&error); if (!login1_seat_info) + { + g_warning ("Failed to parse login1 seat D-Bus interface: %s", error->message); return NULL; + } - g_dbus_connection_register_object (connection, - seat->path, - login1_seat_info->interfaces[0], - &login1_seat_vtable, - seat, - NULL, - &error); - if (error) + if (g_dbus_connection_register_object (connection, + seat->path, + login1_seat_info->interfaces[0], + &login1_seat_vtable, + seat, + NULL, + &error) == 0) g_warning ("Failed to register login1 seat: %s", error->message); - g_clear_error (&error); - g_dbus_node_info_unref (login1_seat_info); if (emit_signal) { - g_dbus_connection_emit_signal (connection, - NULL, - "/org/freedesktop/login1", - "org.freedesktop.login1.Manager", - "SeatNew", - g_variant_new ("(so)", seat->id, seat->path), - &error); - if (error) - g_warning ("Failed to emit SeatNew: %s", error->message); - g_clear_error (&error); + g_autoptr(GError) e = NULL; + if (!g_dbus_connection_emit_signal (connection, + NULL, + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + "SeatNew", + g_variant_new ("(so)", seat->id, seat->path), + &e)) + g_warning ("Failed to emit SeatNew: %s", e->message); } return seat; @@ -1768,22 +1704,20 @@ static void remove_login1_seat (GDBusConnection *connection, const gchar *id) { Login1Seat *seat; - GError *error = NULL; + g_autoptr(GError) error = NULL; seat = find_login1_seat (id); if (!seat) return; - g_dbus_connection_emit_signal (connection, - NULL, - "/org/freedesktop/login1", - "org.freedesktop.login1.Manager", - "SeatRemoved", - g_variant_new ("(so)", seat->id, seat->path), - &error); - if (error) + if (!g_dbus_connection_emit_signal (connection, + NULL, + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + "SeatRemoved", + g_variant_new ("(so)", seat->id, seat->path), + &error)) g_warning ("Failed to emit SeatNew: %s", error->message); - g_clear_error (&error); login1_seats = g_list_remove (login1_seats, seat); g_free (seat->id); @@ -1810,8 +1744,8 @@ static Login1Session * create_login1_session (GDBusConnection *connection) { Login1Session *session; - GError *error = NULL; - GDBusNodeInfo *login1_session_info; + g_autoptr(GError) error = NULL; + g_autoptr(GDBusNodeInfo) login1_session_info = NULL; const gchar *login1_session_interface = "<node>" @@ -1830,23 +1764,20 @@ create_login1_session (GDBusConnection *connection) session->path = g_strdup_printf ("/org/freedesktop/login1/Session/%s", session->id); login1_session_info = g_dbus_node_info_new_for_xml (login1_session_interface, &error); - if (error) - g_warning ("Failed to parse login1 session D-Bus interface: %s", error->message); - g_clear_error (&error); if (!login1_session_info) + { + g_warning ("Failed to parse login1 session D-Bus interface: %s", error->message); return NULL; + } - g_dbus_connection_register_object (connection, - session->path, - login1_session_info->interfaces[0], - &login1_session_vtable, - session, - NULL, - &error); - if (error) + if (g_dbus_connection_register_object (connection, + session->path, + login1_session_info->interfaces[0], + &login1_session_vtable, + session, + NULL, + &error) == 0) g_warning ("Failed to register login1 session: %s", error->message); - g_clear_error (&error); - g_dbus_node_info_unref (login1_session_info); return session; } @@ -1912,9 +1843,8 @@ handle_login1_call (GDBusConnection *connection, if (!session->locked) { - gchar *status = g_strdup_printf ("LOGIN1 LOCK-SESSION SESSION=%s", id); + g_autofree gchar *status = g_strdup_printf ("LOGIN1 LOCK-SESSION SESSION=%s", id); check_status (status); - g_free (status); } session->locked = TRUE; g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); @@ -1934,9 +1864,8 @@ handle_login1_call (GDBusConnection *connection, if (session->locked) { - gchar *status = g_strdup_printf ("LOGIN1 UNLOCK-SESSION SESSION=%s", id); + g_autofree gchar *status = g_strdup_printf ("LOGIN1 UNLOCK-SESSION SESSION=%s", id); check_status (status); - g_free (status); } session->locked = FALSE; g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); @@ -1945,6 +1874,7 @@ handle_login1_call (GDBusConnection *connection, { const gchar *id; Login1Session *session; + g_autofree gchar *status = NULL; g_variant_get (parameters, "(&s)", &id); session = find_login1_session (id); @@ -1954,9 +1884,8 @@ handle_login1_call (GDBusConnection *connection, return; } - gchar *status = g_strdup_printf ("LOGIN1 ACTIVATE-SESSION SESSION=%s", id); + status = g_strdup_printf ("LOGIN1 ACTIVATE-SESSION SESSION=%s", id); check_status (status); - g_free (status); g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); } @@ -1975,9 +1904,8 @@ handle_login1_call (GDBusConnection *connection, if (g_key_file_get_boolean (config, "test-runner-config", "log-login1-terminate", NULL)) { - gchar *status = g_strdup_printf ("LOGIN1 TERMINATE-SESSION SESSION=%s", id); + g_autofree gchar *status = g_strdup_printf ("LOGIN1 TERMINATE-SESSION SESSION=%s", id); check_status (status); - g_free (status); } g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); @@ -2097,26 +2025,23 @@ login1_name_acquired_cb (GDBusConnection *connection, { handle_login1_call, }; - GDBusNodeInfo *login1_info; + g_autoptr(GDBusNodeInfo) login1_info = NULL; Login1Seat *seat0; - GError *error = NULL; + g_autoptr(GError) error = NULL; login1_info = g_dbus_node_info_new_for_xml (login1_interface, &error); - if (error) - g_warning ("Failed to parse login1 D-Bus interface: %s", error->message); - g_clear_error (&error); if (!login1_info) + { + g_warning ("Failed to parse login1 D-Bus interface: %s", error->message); return; - g_dbus_connection_register_object (connection, - "/org/freedesktop/login1", - login1_info->interfaces[0], - &login1_vtable, - NULL, NULL, - &error); - if (error) + } + if (g_dbus_connection_register_object (connection, + "/org/freedesktop/login1", + login1_info->interfaces[0], + &login1_vtable, + NULL, NULL, + &error) == 0) g_warning ("Failed to register login1 service: %s", error->message); - g_clear_error (&error); - g_dbus_node_info_unref (login1_info); /* We always have seat0 */ seat0 = add_login1_seat (connection, "seat0", FALSE); @@ -2177,87 +2102,79 @@ get_accounts_user_by_name (const gchar *username) static void accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_signal) { - GError *error = NULL; - user->hidden = hidden; if (user->hidden && user->id != 0) { + g_autoptr(GError) error = NULL; + g_dbus_connection_unregister_object (accounts_connection, user->id); - g_dbus_connection_emit_signal (accounts_connection, - NULL, - "/org/freedesktop/Accounts", - "org.freedesktop.Accounts", - "UserDeleted", - g_variant_new ("(o)", user->path), - &error); - if (error) + if (!g_dbus_connection_emit_signal (accounts_connection, + NULL, + "/org/freedesktop/Accounts", + "org.freedesktop.Accounts", + "UserDeleted", + g_variant_new ("(o)", user->path), + &error)) g_warning ("Failed to emit UserDeleted: %s", error->message); - g_clear_error (&error); user->id = 0; } if (!user->hidden && user->id == 0) { + g_autoptr(GError) register_error = NULL; + g_autoptr(GError) emit_error = NULL; + user->id = g_dbus_connection_register_object (accounts_connection, user->path, user_info->interfaces[0], &user_vtable, user, NULL, - &error); - if (error) - g_warning ("Failed to register user: %s", error->message); - g_clear_error (&error); + ®ister_error); + if (user->id == 0) + g_warning ("Failed to register user: %s", register_error->message); - g_dbus_connection_emit_signal (accounts_connection, - NULL, - "/org/freedesktop/Accounts", - "org.freedesktop.Accounts", - "UserAdded", - g_variant_new ("(o)", user->path), - &error); - if (error) - g_warning ("Failed to emit UserAdded: %s", error->message); - g_clear_error (&error); + if (!g_dbus_connection_emit_signal (accounts_connection, + NULL, + "/org/freedesktop/Accounts", + "org.freedesktop.Accounts", + "UserAdded", + g_variant_new ("(o)", user->path), + &emit_error)) + g_warning ("Failed to emit UserAdded: %s", emit_error->message); } } static void load_passwd_file (void) { - gchar *path, *data, **lines; + g_autofree gchar *path = NULL; + g_autofree gchar *data = NULL; + g_auto(GStrv) lines = NULL; gchar **user_filter = NULL; int i; if (g_key_file_has_key (config, "test-runner-config", "accounts-service-user-filter", NULL)) { - gchar *filter; - - filter = g_key_file_get_string (config, "test-runner-config", "accounts-service-user-filter", NULL); + g_autofree gchar *filter = g_key_file_get_string (config, "test-runner-config", "accounts-service-user-filter", NULL); user_filter = g_strsplit (filter, " ", -1); - g_free (filter); } path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL); g_file_get_contents (path, &data, NULL, NULL); - g_free (path); lines = g_strsplit (data, "\n", -1); - g_free (data); for (i = 0; lines[i]; i++) { - gchar **fields; + g_auto(GStrv) fields = NULL; guint uid; - gchar *user_name, *real_name; + const gchar *user_name, *real_name; AccountsUser *user = NULL; fields = g_strsplit (lines[i], ":", -1); if (fields == NULL || g_strv_length (fields) < 7) - { - g_strfreev (fields); continue; - } user_name = fields[0]; uid = atoi (fields[2]); @@ -2266,8 +2183,8 @@ load_passwd_file (void) user = get_accounts_user_by_uid (uid); if (!user) { - gchar *path; - GKeyFile *dmrc_file; + g_autofree gchar *path = NULL; + g_autoptr(GKeyFile) dmrc_file = NULL; user = g_malloc0 (sizeof (AccountsUser)); accounts_users = g_list_append (accounts_users, user); @@ -2287,7 +2204,6 @@ load_passwd_file (void) dmrc_file = g_key_file_new (); path = g_build_filename (temp_dir, "home", user_name, ".dmrc", NULL); g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_NONE, NULL); - g_free (path); user->uid = uid; user->user_name = g_strdup (user_name); @@ -2312,14 +2228,8 @@ load_passwd_file (void) user->has_messages = g_key_file_get_boolean (dmrc_file, "X-Accounts", "HasMessages", NULL); user->path = g_strdup_printf ("/org/freedesktop/Accounts/User%d", uid); accounts_user_set_hidden (user, user->hidden, FALSE); - - g_key_file_free (dmrc_file); } - - g_strfreev (fields); } - - g_strfreev (lines); } static void @@ -2499,32 +2409,30 @@ accounts_name_acquired_cb (GDBusConnection *connection, " <signal name='Changed' />" " </interface>" "</node>"; - GError *error = NULL; + g_autoptr(GError) error = NULL; accounts_connection = connection; accounts_info = g_dbus_node_info_new_for_xml (accounts_interface, &error); - if (error) - g_warning ("Failed to parse D-Bus interface: %s", error->message); - g_clear_error (&error); if (!accounts_info) + { + g_warning ("Failed to parse D-Bus interface: %s", error->message); return; + } user_info = g_dbus_node_info_new_for_xml (user_interface, &error); - if (error) - g_warning ("Failed to parse D-Bus interface: %s", error->message); - g_clear_error (&error); if (!user_info) + { + g_warning ("Failed to parse D-Bus interface: %s", error->message); return; - g_dbus_connection_register_object (connection, - "/org/freedesktop/Accounts", - accounts_info->interfaces[0], - &accounts_vtable, - NULL, - NULL, - &error); - if (error) + } + if (g_dbus_connection_register_object (connection, + "/org/freedesktop/Accounts", + accounts_info->interfaces[0], + &accounts_vtable, + NULL, + NULL, + &error) == 0) g_warning ("Failed to register accounts service: %s", error->message); - g_clear_error (&error); g_dbus_node_info_unref (accounts_info); service_count--; @@ -2570,7 +2478,7 @@ properties_changed_cb (GDBusConnection *connection, gpointer user_data) { const gchar *interface, *name; - GString *status; + g_autoptr(GString) status = NULL; GVariant *value; GVariantIter *changed_properties, *invalidated_properties; int i; @@ -2607,7 +2515,6 @@ properties_changed_cb (GDBusConnection *connection, } check_status (status->str); - g_string_free (status, TRUE); } static void @@ -2619,7 +2526,7 @@ dbus_signal_cb (GDBusConnection *connection, GVariant *parameters, gpointer user_data) { - GString *status; + g_autoptr(GString) status = NULL; status = g_string_new ("RUNNER DBUS-SIGNAL"); g_string_append_printf (status, " PATH=%s", object_path); @@ -2627,7 +2534,6 @@ dbus_signal_cb (GDBusConnection *connection, g_string_append_printf (status, " NAME=%s", signal_name); check_status (status->str); - g_string_free (status, TRUE); } int @@ -2635,12 +2541,27 @@ main (int argc, char **argv) { GMainLoop *loop; int i; - gchar *greeter = NULL, *script_name, *config_file, *additional_system_config; - gchar *additional_config, *path, *path1, *path2, *ld_preload, *ld_library_path, *home_dir; - GString *passwd_data, *group_data; + g_autofree gchar *greeter = NULL; + const gchar *script_name; + g_autofree gchar *config_file = NULL; + g_autofree gchar *additional_system_config = NULL; + g_autofree gchar *additional_config = NULL; + g_autofree gchar *greeter_path = NULL; + g_autofree gchar *ld_library_path = NULL; + g_autofree gchar *home_dir = NULL; + g_autofree gchar *ld_preload = NULL; + g_autofree gchar *path = NULL; + g_autofree gchar *lightdm_gobject_path = NULL; + g_autofree gchar *lightdm_qt_path = NULL; + g_autofree gchar *gi_typelib_path = NULL; + g_autofree gchar *passwd_path = NULL; + g_autofree gchar *group_path = NULL; + g_autoptr(GString) passwd_data = NULL; + g_autoptr(GString) group_data = NULL; + g_autoptr(GSocketAddress) address = NULL; GSource *status_source; gchar cwd[1024]; - GError *error = NULL; + g_autoptr(GError) error = NULL; #if !defined(GLIB_VERSION_2_36) g_type_init (); @@ -2661,7 +2582,6 @@ main (int argc, char **argv) script_name = argv[1]; config_file = g_strdup_printf ("%s.conf", script_name); config_path = g_build_filename (SRCDIR, "tests", "scripts", config_file, NULL); - g_free (config_file); config = g_key_file_new (); g_key_file_load_from_file (config, config_path, G_KEY_FILE_NONE, NULL); @@ -2684,35 +2604,26 @@ main (int argc, char **argv) /* Override system calls */ ld_preload = g_build_filename (BUILDDIR, "tests", "src", ".libs", "libsystem.so", NULL); g_setenv ("LD_PRELOAD", ld_preload, TRUE); - g_free (ld_preload); /* Run test programs */ path = g_strdup_printf ("%s/tests/src/.libs:%s/tests/src:%s/tests/src:%s/src:%s", BUILDDIR, BUILDDIR, SRCDIR, BUILDDIR, g_getenv ("PATH")); g_setenv ("PATH", path, TRUE); - g_free (path); /* Use locally built libraries */ - path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", ".libs", NULL); - path2 = g_build_filename (BUILDDIR, "liblightdm-qt", ".libs", NULL); - ld_library_path = g_strdup_printf ("%s:%s", path1, path2); - g_free (path1); - g_free (path2); + lightdm_gobject_path = g_build_filename (BUILDDIR, "liblightdm-gobject", ".libs", NULL); + lightdm_qt_path = g_build_filename (BUILDDIR, "liblightdm-qt", ".libs", NULL); + ld_library_path = g_strdup_printf ("%s:%s", lightdm_gobject_path, lightdm_qt_path); g_setenv ("LD_LIBRARY_PATH", ld_library_path, TRUE); - g_free (ld_library_path); - path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", NULL); - g_setenv ("GI_TYPELIB_PATH", path1, TRUE); - g_free (path1); + gi_typelib_path = g_build_filename (BUILDDIR, "liblightdm-gobject", NULL); + g_setenv ("GI_TYPELIB_PATH", gi_typelib_path, TRUE); /* Run in a temporary directory inside the build directory */ /* Note we have to pick a name that is short since Unix sockets in this directory have a 108 character limit on their paths */ i = 0; while (TRUE) { - gchar *name; - - name = g_strdup_printf (".r%d", i); + g_autofree gchar *name = g_strdup_printf (".r%d", i); g_free (temp_dir); temp_dir = g_build_filename ("/tmp", name, NULL); - g_free (name); if (!g_file_test (temp_dir, G_FILE_TEST_EXISTS)) break; i++; @@ -2725,35 +2636,19 @@ main (int argc, char **argv) status_socket_name = g_build_filename (temp_dir, ".s", NULL); unlink (status_socket_name); status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); - if (error) - g_warning ("Error creating status socket %s: %s", status_socket_name, error->message); - g_clear_error (&error); - if (status_socket) - { - GSocketAddress *address; - gboolean result; - - address = g_unix_socket_address_new (status_socket_name); - result = g_socket_bind (status_socket, address, FALSE, &error); - g_object_unref (address); - if (error) - g_warning ("Error binding status socket %s: %s", status_socket_name, error->message); - g_clear_error (&error); - if (result) - { - result = g_socket_listen (status_socket, &error); - if (error) - g_warning ("Error listening on status socket %s: %s", status_socket_name, error->message); - g_clear_error (&error); - } - if (!result) - { - g_object_unref (status_socket); - status_socket = NULL; - } - } if (!status_socket) + { + g_warning ("Error creating status socket %s: %s", status_socket_name, error->message); quit (EXIT_FAILURE); + } + + address = g_unix_socket_address_new (status_socket_name); + if (!g_socket_bind (status_socket, address, FALSE, &error) || + !g_socket_listen (status_socket, &error)) + { + g_warning ("Error binding/listening status socket %s: %s", status_socket_name, error->message); + g_clear_object (&status_socket); + } status_source = g_socket_create_source (status_socket, G_IO_IN, NULL); g_source_set_callback (status_source, status_connect_cb, NULL, NULL); g_source_attach (status_source, NULL); @@ -2781,7 +2676,7 @@ main (int argc, char **argv) additional_system_config = g_key_file_get_string (config, "test-runner-config", "additional-system-config", NULL); if (additional_system_config) { - gchar **files; + g_auto(GStrv) files = NULL; g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/lightdm.conf.d", temp_dir), 0755); @@ -2789,13 +2684,12 @@ main (int argc, char **argv) for (i = 0; files[i]; i++) if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/usr/share/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir))) perror ("Failed to copy configuration"); - g_strfreev (files); } additional_config = g_key_file_get_string (config, "test-runner-config", "additional-config", NULL); if (additional_config) { - gchar **files; + g_auto(GStrv) files = NULL; g_mkdir_with_parents (g_strdup_printf ("%s/etc/xdg/lightdm/lightdm.conf.d", temp_dir), 0755); @@ -2803,25 +2697,23 @@ main (int argc, char **argv) for (i = 0; files[i]; i++) if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/etc/xdg/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir))) perror ("Failed to copy configuration"); - g_strfreev (files); } if (g_key_file_has_key (config, "test-runner-config", "shared-data-dirs", NULL)) { - gchar *dir_string; - gchar **dirs; + g_autofree gchar *dir_string = NULL; + g_auto(GStrv) dirs = NULL; gint i; dir_string = g_key_file_get_string (config, "test-runner-config", "shared-data-dirs", NULL); dirs = g_strsplit (dir_string, " ", -1); - g_free (dir_string); for (i = 0; dirs[i]; i++) { - gchar **fields = g_strsplit (dirs[i], ":", -1); + g_auto(GStrv) fields = g_strsplit (dirs[i], ":", -1); if (g_strv_length (fields) == 4) { - gchar *path = g_strdup_printf ("%s/var/lib/lightdm-data/%s", temp_dir, fields[0]); + g_autofree gchar *path = g_strdup_printf ("%s/var/lib/lightdm-data/%s", temp_dir, fields[0]); int uid = g_ascii_strtoll (fields[1], NULL, 10); int gid = g_ascii_strtoll (fields[2], NULL, 10); int mode = g_ascii_strtoll (fields[3], NULL, 8); @@ -2829,12 +2721,8 @@ main (int argc, char **argv) g_chmod (path, mode); /* mkdir filters by umask, so make sure we have what we want */ if (chown (path, uid, gid) < 0) g_warning ("chown (%s) failed: %s", path, strerror (errno)); - g_free (path); } - g_strfreev (fields); } - - g_strfreev (dirs); } /* Always copy the script */ @@ -2850,15 +2738,13 @@ main (int argc, char **argv) perror ("Failed to copy greeters"); /* Set up the default greeter */ - path = g_build_filename (temp_dir, "usr", "share", "lightdm", "greeters", "default.desktop", NULL); + greeter_path = g_build_filename (temp_dir, "usr", "share", "lightdm", "greeters", "default.desktop", NULL); greeter = g_strdup_printf ("%s.desktop", argv[2]); - if (symlink (greeter, path) < 0) + if (symlink (greeter, greeter_path) < 0) { - g_printerr ("Failed to make greeter symlink %s->%s: %s\n", path, greeter, strerror (errno)); + g_printerr ("Failed to make greeter symlink %s->%s: %s\n", greeter_path, greeter, strerror (errno)); quit (EXIT_FAILURE); } - g_free (path); - g_free (greeter); home_dir = g_build_filename (temp_dir, "home", NULL); @@ -2942,16 +2828,15 @@ main (int argc, char **argv) group_data = g_string_new (""); for (i = 0; users[i].user_name; i++) { - GKeyFile *dmrc_file; + g_autoptr(GKeyFile) dmrc_file = NULL; gboolean save_dmrc = FALSE; if (strcmp (users[i].user_name, "mount-home-dir") != 0 && strcmp (users[i].user_name, "make-home-dir") != 0) { - path = g_build_filename (home_dir, users[i].user_name, NULL); + g_autofree gchar *path = g_build_filename (home_dir, users[i].user_name, NULL); g_mkdir_with_parents (path, 0755); if (chown (path, users[i].uid, users[i].uid) < 0) g_debug ("chown (%s) failed: %s", path, strerror (errno)); - g_free (path); } dmrc_file = g_key_file_new (); @@ -2979,26 +2864,19 @@ main (int argc, char **argv) if (save_dmrc) { - gchar *data; - - path = g_build_filename (home_dir, users[i].user_name, ".dmrc", NULL); - data = g_key_file_to_data (dmrc_file, NULL, NULL); + g_autofree gchar *path = g_build_filename (home_dir, users[i].user_name, ".dmrc", NULL); + g_autofree gchar *data = g_key_file_to_data (dmrc_file, NULL, NULL); g_file_set_contents (path, data, -1, NULL); - g_free (data); - g_free (path); } - g_key_file_free (dmrc_file); - /* Write corrupt X authority file */ if (strcmp (users[i].user_name, "corrupt-xauth") == 0) { gchar data[1] = { 0xFF }; - path = g_build_filename (home_dir, users[i].user_name, ".Xauthority", NULL); + g_autofree gchar *path = g_build_filename (home_dir, users[i].user_name, ".Xauthority", NULL); g_file_set_contents (path, data, 1, NULL); chmod (path, S_IRUSR | S_IWUSR); - g_free (path); } /* Add passwd file entry */ @@ -3007,18 +2885,14 @@ main (int argc, char **argv) /* Add group file entry */ g_string_append_printf (group_data, "%s:x:%d:%s\n", users[i].user_name, users[i].uid, users[i].user_name); } - path = g_build_filename (temp_dir, "etc", "passwd", NULL); - g_file_set_contents (path, passwd_data->str, -1, NULL); - g_free (path); - g_string_free (passwd_data, TRUE); + passwd_path = g_build_filename (temp_dir, "etc", "passwd", NULL); + g_file_set_contents (passwd_path, passwd_data->str, -1, NULL); /* Add an extra test group */ g_string_append_printf (group_data, "test-group:x:111:\n"); - path = g_build_filename (temp_dir, "etc", "group", NULL); - g_file_set_contents (path, group_data->str, -1, NULL); - g_free (path); - g_string_free (group_data, TRUE); + group_path = g_build_filename (temp_dir, "etc", "group", NULL); + g_file_set_contents (group_path, group_data->str, -1, NULL); if (g_key_file_has_key (config, "test-runner-config", "timeout", NULL)) status_timeout_ms = g_key_file_get_integer (config, "test-runner-config", "timeout", NULL) * 1000; diff --git a/tests/src/test-script-hook.c b/tests/src/test-script-hook.c index e0d72084..abaf8543 100644 --- a/tests/src/test-script-hook.c +++ b/tests/src/test-script-hook.c @@ -9,7 +9,7 @@ static GKeyFile *config; int main (int argc, char **argv) { - GString *status_text; + g_autoptr(GString) status_text = NULL; #if !defined(GLIB_VERSION_2_36) g_type_init (); @@ -31,7 +31,6 @@ main (int argc, char **argv) if (g_getenv ("USER")) g_string_append_printf (status_text, " USER=%s", g_getenv ("USER")); status_notify ("%s", status_text->str); - g_string_free (status_text, TRUE); if (argc > 2) return atoi (argv[2]); diff --git a/tests/src/test-session.c b/tests/src/test-session.c index 8323b2fc..0e368164 100644 --- a/tests/src/test-session.c +++ b/tests/src/test-session.c @@ -72,8 +72,6 @@ authentication_complete_cb (LightDMGreeter *greeter) static void request_cb (const gchar *name, GHashTable *params) { - GError *error = NULL; - if (!name) { g_main_loop_quit (loop); @@ -122,7 +120,7 @@ request_cb (const gchar *name, GHashTable *params) { int n_groups, i; gid_t *groups; - GString *group_list; + g_autoptr(GString) group_list = NULL; n_groups = getgroups (0, NULL); if (n_groups < 0) @@ -146,7 +144,6 @@ request_cb (const gchar *name, GHashTable *params) g_string_append_printf (group_list, "%d", groups[i]); } status_notify ("%s LIST-GROUPS GROUPS=%s", session_id, group_list->str); - g_string_free (group_list, TRUE); free (groups); } @@ -166,15 +163,13 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "READ") == 0) { const gchar *name = g_hash_table_lookup (params, "FILE"); - gchar *contents = NULL; - GError *error = NULL; + g_autofree gchar *contents = NULL; + g_autoptr(GError) error = NULL; if (g_file_get_contents (name, &contents, NULL, &error)) status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents); else status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message); - g_free (contents); - g_clear_error (&error); } else if (strcmp (name, "LIST-UNKNOWN-FILE-DESCRIPTORS") == 0) @@ -182,16 +177,15 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "CHECK-X-AUTHORITY") == 0) { - gchar *xauthority; + g_autofree gchar *xauthority = NULL; GStatBuf file_info; - GString *mode_string; + g_autoptr(GString) mode_string = NULL; xauthority = g_strdup (g_getenv ("XAUTHORITY")); if (!xauthority) xauthority = g_build_filename (g_get_home_dir (), ".Xauthority", NULL); g_stat (xauthority, &file_info); - g_free (xauthority); mode_string = g_string_new (""); g_string_append_c (mode_string, file_info.st_mode & S_IRUSR ? 'r' : '-'); @@ -204,18 +198,17 @@ request_cb (const gchar *name, GHashTable *params) g_string_append_c (mode_string, file_info.st_mode & S_IWOTH ? 'w' : '-'); g_string_append_c (mode_string, file_info.st_mode & S_IXOTH ? 'x' : '-'); status_notify ("%s CHECK-X-AUTHORITY MODE=%s", session_id, mode_string->str); - g_string_free (mode_string, TRUE); } else if (strcmp (name, "WRITE-SHARED-DATA") == 0) { const gchar *data = g_hash_table_lookup (params, "DATA"); - gchar *dir; + const gchar *dir; dir = getenv ("XDG_GREETER_DATA_DIR"); if (dir) { - gchar *path; + g_autofree gchar *path = NULL; FILE *f; path = g_build_filename (dir, "data", NULL); @@ -226,7 +219,6 @@ request_cb (const gchar *name, GHashTable *params) if (f) fclose (f); - g_free (path); } else status_notify ("%s WRITE-SHARED-DATA ERROR=NO_XDG_GREETER_DATA_DIR", session_id); @@ -234,23 +226,20 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "READ-SHARED-DATA") == 0) { - gchar *dir; + const gchar *dir; dir = getenv ("XDG_GREETER_DATA_DIR"); if (dir) { - gchar *path; - gchar *contents = NULL; - GError *error = NULL; + g_autofree gchar *path = NULL; + g_autofree gchar *contents = NULL; + g_autoptr(GError) error = NULL; path = g_build_filename (dir, "data", NULL); if (g_file_get_contents (path, &contents, NULL, &error)) status_notify ("%s READ-SHARED-DATA DATA=%s", session_id, contents); else status_notify ("%s WRITE-SHARED-DATA ERROR=%s", session_id, error->message); - g_free (path); - g_free (contents); - g_clear_error (&error); } else status_notify ("%s WRITE-SHARED-DATA ERROR=NO_XDG_GREETER_DATA_DIR", session_id); @@ -259,7 +248,7 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "GREETER-START") == 0) { int timeout; - GError *error = NULL; + g_autoptr(GError) error = NULL; g_assert (greeter == NULL); greeter = lightdm_greeter_new (); @@ -269,10 +258,7 @@ request_cb (const gchar *name, GHashTable *params) if (lightdm_greeter_connect_to_daemon_sync (greeter, &error)) status_notify ("%s GREETER-STARTED", session_id); else - { status_notify ("%s GREETER-FAILED ERROR=%s", session_id, error->message); - g_clear_error (&error); - } if (lightdm_greeter_get_select_user_hint (greeter)) status_notify ("%s GREETER-SELECT-USER-HINT USERNAME=%s", session_id, lightdm_greeter_get_select_user_hint (greeter)); @@ -310,29 +296,26 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "GREETER-AUTHENTICATE") == 0) { + g_autoptr(GError) error = NULL; + if (!lightdm_greeter_authenticate (greeter, g_hash_table_lookup (params, "USERNAME"), &error)) - { status_notify ("%s FAIL-AUTHENTICATE ERROR=%s", session_id, error->message); - g_clear_error (&error); - } } else if (strcmp (name, "GREETER-RESPOND") == 0) { + g_autoptr(GError) error = NULL; + if (!lightdm_greeter_respond (greeter, g_hash_table_lookup (params, "TEXT"), &error)) - { status_notify ("%s FAIL-RESPOND ERROR=%s", session_id, error->message); - g_clear_error (&error); - } } else if (strcmp (name, "GREETER-START-SESSION") == 0) { + g_autoptr(GError) error = NULL; + if (!lightdm_greeter_start_session_sync (greeter, g_hash_table_lookup (params, "SESSION"), &error)) - { status_notify ("%s FAIL-START-SESSION ERROR=%s", session_id, error->message); - g_clear_error (&error); - } } else if (strcmp (name, "GREETER-STOP") == 0) @@ -346,7 +329,7 @@ int main (int argc, char **argv) { gchar *display, *xdg_seat, *xdg_vtnr, *xdg_current_desktop, *xdg_greeter_data_dir, *xdg_session_cookie, *xdg_session_class, *xdg_session_type, *xdg_session_desktop, *mir_server_host_socket, *mir_vt, *mir_id; - GString *status_text; + g_autoptr(GString) status_text = NULL; int fd, open_max; display = getenv ("DISPLAY"); @@ -422,7 +405,6 @@ main (int argc, char **argv) g_string_append_printf (status_text, " NAME=%s", argv[1]); g_string_append_printf (status_text, " USER=%s", getenv ("USER")); status_notify ("%s", status_text->str); - g_string_free (status_text, TRUE); config = g_key_file_new (); g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL); diff --git a/tests/src/unity-system-compositor.c b/tests/src/unity-system-compositor.c index f138aaa3..49da86d0 100644 --- a/tests/src/unity-system-compositor.c +++ b/tests/src/unity-system-compositor.c @@ -71,7 +71,7 @@ read_message_cb (GIOChannel *channel, GIOCondition condition, gpointer data) gsize n_read; guint16 id; guint16 payload_length; - GError *error = NULL; + g_autoptr(GError) error = NULL; if (g_io_channel_read_chars (channel, header, 4, &n_read, &error) != G_IO_STATUS_NORMAL) { @@ -144,7 +144,7 @@ int main (int argc, char **argv) { int i; - GString *status_text; + g_autoptr(GString) status_text = NULL; gboolean test = FALSE, container = FALSE; int vt_number = -1; const gchar *file = NULL; @@ -206,7 +206,6 @@ main (int argc, char **argv) if (container) g_string_append (status_text, " CONTAINER=TRUE"); status_notify ("%s", status_text->str); - g_string_free (status_text, TRUE); config = g_key_file_new (); g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL); diff --git a/tests/src/vnc-client.c b/tests/src/vnc-client.c index f70cd568..be94315b 100644 --- a/tests/src/vnc-client.c +++ b/tests/src/vnc-client.c @@ -12,9 +12,9 @@ static GKeyFile *config; int main (int argc, char **argv) { - GError *error = NULL; - GSocket *socket; - GSocketAddress *address; + g_autoptr(GError) error = NULL; + g_autoptr(GSocket) socket = NULL; + g_autoptr(GSocketAddress) address = NULL; gboolean result; gchar buffer[1024]; gssize n_read, n_sent; @@ -33,27 +33,26 @@ main (int argc, char **argv) status_notify ("VNC-CLIENT CONNECT"); socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, &error); - if (error) - g_warning ("Unable to make VNC socket: %s", error->message); - g_clear_error (&error); if (!socket) + { + g_warning ("Unable to make VNC socket: %s", error->message); return EXIT_FAILURE; + } address = g_inet_socket_address_new (g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4), 5900); result = g_socket_connect (socket, address, NULL, &error); - g_object_unref (address); - if (error) - g_warning ("Unable to connect VNC socket: %s", error->message); - g_clear_error (&error); if (!result) + { + g_warning ("Unable to connect VNC socket: %s", error->message); return EXIT_FAILURE; + } n_read = g_socket_receive (socket, buffer, 1023, NULL, &error); - if (error) - g_warning ("Unable to receive on VNC socket: %s", error->message); - g_clear_error (&error); if (n_read <= 0) + { + g_warning ("Unable to receive on VNC socket: %s", error->message); return EXIT_FAILURE; + } buffer[n_read] = '\0'; if (g_str_has_suffix (buffer, "\n")) @@ -62,11 +61,11 @@ main (int argc, char **argv) snprintf (buffer, 1024, "RFB 003.003\n"); n_sent = g_socket_send (socket, buffer, strlen (buffer), NULL, &error); - if (error) - g_warning ("Unable to send on VNC socket: %s", error->message); - g_clear_error (&error); if (n_sent != strlen (buffer)) + { + g_warning ("Unable to send on VNC socket: %s", error->message); return EXIT_FAILURE; + } return EXIT_SUCCESS; } diff --git a/tests/src/x-server.c b/tests/src/x-server.c index d756c58c..57ec6ecf 100644 --- a/tests/src/x-server.c +++ b/tests/src/x-server.c @@ -51,25 +51,19 @@ static guint x_client_signals[X_CLIENT_LAST_SIGNAL] = { 0 }; void x_client_send_failed (XClient *client, const gchar *reason) { - gchar *message; - - message = g_strdup_printf ("FAILED:%s", reason); + g_autofree gchar *message = g_strdup_printf ("FAILED:%s", reason); errno = 0; if (send (g_io_channel_unix_get_fd (client->priv->channel), message, strlen (message), 0) != strlen (message)) g_printerr ("Failed to send FAILED: %s\n", strerror (errno)); - g_free (message); } void x_client_send_success (XClient *client) { - gchar *message; - - message = g_strdup ("SUCCESS"); + g_autofree gchar *message = g_strdup ("SUCCESS"); errno = 0; if (send (g_io_channel_unix_get_fd (client->priv->channel), message, strlen (message), 0) != strlen (message)) g_printerr ("Failed to send SUCCESS: %s\n", strerror (errno)); - g_free (message); } void @@ -121,12 +115,11 @@ socket_connect_cb (GIOChannel *channel, GIOCondition condition, gpointer data) XServer *server = data; GSocket *data_socket; XClient *client; - GError *error = NULL; + g_autoptr(GError) error = NULL; data_socket = g_socket_accept (server->priv->socket, NULL, &error); if (error) g_warning ("Error accepting connection: %s", strerror (errno)); - g_clear_error (&error); if (!data_socket) return FALSE; @@ -145,12 +138,11 @@ socket_connect_cb (GIOChannel *channel, GIOCondition condition, gpointer data) gboolean x_server_start (XServer *server) { - gchar *name; - GError *error = NULL; + g_autofree gchar *name = NULL; + g_autoptr(GError) error = NULL; name = g_strdup_printf (".x:%d", server->priv->display_number); server->priv->socket_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), name, NULL); - g_free (name); server->priv->socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); if (!server->priv->socket || diff --git a/tests/src/xdmcp-client.c b/tests/src/xdmcp-client.c index 84b0b599..275cdc4d 100644 --- a/tests/src/xdmcp-client.c +++ b/tests/src/xdmcp-client.c @@ -57,14 +57,13 @@ static void xdmcp_write (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) { gssize n_written; - GError *error = NULL; + g_autoptr(GError) error = NULL; n_written = g_socket_send (client->priv->socket, (const gchar *) buffer, buffer_length, NULL, &error); if (n_written < 0) g_warning ("Failed to send XDMCP request: %s", error->message); else if (n_written != buffer_length) g_warning ("Partial write for XDMCP request, wrote %zi, expected %zi", n_written, buffer_length); - g_clear_error (&error); } static void @@ -292,8 +291,7 @@ xdmcp_client_start (XDMCPClient *client) { GSocketConnectable *address; GSocketAddressEnumerator *enumerator; - gboolean result; - GError *error = NULL; + g_autoptr(GError) error = NULL; if (client->priv->socket) return TRUE; @@ -301,41 +299,32 @@ xdmcp_client_start (XDMCPClient *client) client->priv->socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &error); if (error) g_warning ("Error creating XDMCP socket: %s", error->message); - g_clear_error (&error); if (!client->priv->socket) return FALSE; address = g_network_address_new (client->priv->host, client->priv->port); enumerator = g_socket_connectable_enumerate (address); - result = FALSE; while (TRUE) { - GSocketAddress *socket_address; - GError *e = NULL; + g_autoptr(GSocketAddress) socket_address = NULL; + g_autoptr(GError) e = NULL; socket_address = g_socket_address_enumerator_next (enumerator, NULL, &e); if (e) g_warning ("Failed to get socket address: %s", e->message); - g_clear_error (&e); if (!socket_address) - break; + return FALSE; - result = g_socket_connect (client->priv->socket, socket_address, NULL, error ? NULL : &error); - g_object_unref (socket_address); - if (result) + if (!g_socket_connect (client->priv->socket, socket_address, NULL, &e)) { - g_clear_error (&error); - break; + g_warning ("Unable to connect XDMCP socket: %s", error->message); + continue; } - } - if (error) - g_warning ("Unable to connect XDMCP socket: %s", error->message); - if (!result) - return FALSE; - g_io_add_watch (g_io_channel_unix_new (g_socket_get_fd (client->priv->socket)), G_IO_IN, xdmcp_data_cb, client); + g_io_add_watch (g_io_channel_unix_new (g_socket_get_fd (client->priv->socket)), G_IO_IN, xdmcp_data_cb, client); - return TRUE; + return TRUE; + } } GInetAddress * @@ -500,11 +489,10 @@ static void xdmcp_client_finalize (GObject *object) { XDMCPClient *client = (XDMCPClient *) object; - g_free (client->priv->host); - if (client->priv->socket) - g_object_unref (client->priv->socket); - g_free (client->priv->authorization_name); - g_free (client->priv->authorization_data); + g_clear_pointer (&client->priv->host, g_free); + g_clear_object (&client->priv->socket); + g_clear_pointer (&client->priv->authorization_name, g_free); + g_clear_pointer (&client->priv->authorization_data, g_free); } static void |