diff options
author | Robert Ancell <robert.ancell@canonical.com> | 2017-12-20 11:59:51 +1300 |
---|---|---|
committer | Robert Ancell <robert.ancell@canonical.com> | 2017-12-20 11:59:51 +1300 |
commit | 7c2e0a6db1427027f799a71b08443151535f6809 (patch) | |
tree | 08691025bccc7563b610ca4d10eff1ae7756df3a /src | |
parent | edb7395bd742a35c70acc1ba5bfd418df71bcacb (diff) |
Use g_auto to reduce chances of memory leaks
Diffstat (limited to 'src')
-rw-r--r-- | src/daemon.c | 108 |
1 files changed, 33 insertions, 75 deletions
diff --git a/src/daemon.c b/src/daemon.c index e8c786f..7bd7502 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -145,15 +145,14 @@ error_get_type (void) static void remove_cache_files (const gchar *user_name) { - gchar *filename; + g_autofree gchar *user_filename = NULL; + g_autofree gchar *icon_filename = NULL; - filename = g_build_filename (USERDIR, user_name, NULL); - g_remove (filename); - g_free (filename); + user_filename = g_build_filename (USERDIR, user_name, NULL); + g_remove (user_filename); - filename = g_build_filename (ICONDIR, user_name, NULL); - g_remove (filename); - g_free (filename); + icon_filename = g_build_filename (ICONDIR, user_name, NULL); + g_remove (icon_filename); } static struct passwd * @@ -257,7 +256,7 @@ entry_generator_cachedir (Daemon *daemon, struct spwd **shadow_entry) { struct passwd *pwent; - GError *error = NULL; + g_autoptr(GError) error = NULL; gboolean regular; GHashTableIter iter; const gchar *name; @@ -270,7 +269,6 @@ entry_generator_cachedir (Daemon *daemon, if (error != NULL) { if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) g_warning ("couldn't list user cache directory: %s", USERDIR); - g_error_free (error); return NULL; } } @@ -554,17 +552,14 @@ static gboolean reload_autologin_timeout (Daemon *daemon) { gboolean enabled; - gchar *name = NULL; - GError *error = NULL; + g_autofree gchar *name = NULL; + g_autoptr(GError) error = NULL; User *user = NULL; daemon->priv->autologin_id = 0; if (!load_autologin (daemon, &name, &enabled, &error)) { g_debug ("failed to load gdms custom.conf: %s", error->message); - g_error_free (error); - g_free (name); - return FALSE; } @@ -589,8 +584,6 @@ reload_autologin_timeout (Daemon *daemon) g_debug ("automatic login is disabled"); } - g_free (name); - return FALSE; } @@ -748,42 +741,32 @@ daemon_finalize (GObject *object) static gboolean register_accounts_daemon (Daemon *daemon) { - GError *error = NULL; + g_autoptr(GError) error = NULL; daemon->priv->authority = polkit_authority_get_sync (NULL, &error); - if (daemon->priv->authority == NULL) { - if (error != NULL) { + if (error != NULL) g_critical ("error getting polkit authority: %s", error->message); - g_error_free (error); - } - goto error; + return FALSE; } daemon->priv->bus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (daemon->priv->bus_connection == NULL) { - if (error != NULL) { + if (error != NULL) g_critical ("error getting system bus: %s", error->message); - g_error_free (error); - } - goto error; + return FALSE; } if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (daemon), daemon->priv->bus_connection, "/org/freedesktop/Accounts", &error)) { - if (error != NULL) { + if (error != NULL) g_critical ("error exporting interface: %s", error->message); - g_error_free (error); - } - goto error; + return FALSE; } return TRUE; - - error: - return FALSE; } Daemon * @@ -807,15 +790,13 @@ throw_error (GDBusMethodInvocation *context, ...) { va_list args; - gchar *message; + g_autofree gchar *message = NULL; va_start (args, format); message = g_strdup_vprintf (format, args); va_end (args); g_dbus_method_invocation_return_error (context, ERROR, error_code, "%s", message); - - g_free (message); } static User * @@ -969,7 +950,7 @@ static gboolean finish_list_cached_users (gpointer user_data) { ListUserData *data = user_data; - GPtrArray *object_paths; + g_autoptr(GPtrArray) object_paths = NULL; GHashTableIter iter; const gchar *name; User *user; @@ -1000,8 +981,6 @@ finish_list_cached_users (gpointer user_data) accounts_accounts_complete_list_cached_users (NULL, data->context, (const gchar * const *) object_paths->pdata); - g_ptr_array_free (object_paths, TRUE); - list_user_data_free (data); return FALSE; @@ -1037,8 +1016,8 @@ static void cache_user (Daemon *daemon, User *user) { - gchar *filename; - const char *user_name; + g_autofree gchar *filename = NULL; + const gchar *user_name; /* Always use the canonical user name looked up */ user_name = user_get_user_name (user); @@ -1047,8 +1026,6 @@ cache_user (Daemon *daemon, if (!g_file_test (filename, G_FILE_TEST_EXISTS)) { user_save (user); } - - g_free (filename); } typedef struct { @@ -1076,12 +1053,11 @@ daemon_create_user_authorized_cb (Daemon *daemon, { CreateUserData *cd = data; User *user; - GError *error; + g_autoptr(GError) error = NULL; const gchar *argv[9]; if (getpwnam (cd->user_name) != NULL) { throw_error (context, ERROR_USER_EXISTS, "A user with name '%s' already exists", cd->user_name); - return; } @@ -1092,7 +1068,7 @@ daemon_create_user_authorized_cb (Daemon *daemon, argv[2] = "-c"; argv[3] = cd->real_name; if (cd->account_type == ACCOUNT_TYPE_ADMINISTRATOR) { - char *admin_groups; + g_autofree gchar *admin_groups = NULL; admin_groups = g_strdup (ADMIN_GROUP); if (EXTRA_ADMIN_GROUPS != NULL && EXTRA_ADMIN_GROUPS[0] != '\0') @@ -1104,8 +1080,6 @@ daemon_create_user_authorized_cb (Daemon *daemon, argv[6] = "--"; argv[7] = cd->user_name; argv[8] = NULL; - - g_free (admin_groups); } else if (cd->account_type == ACCOUNT_TYPE_STANDARD) { argv[4] = "--"; @@ -1120,7 +1094,6 @@ daemon_create_user_authorized_cb (Daemon *daemon, error = NULL; if (!spawn_with_login_uid (context, argv, &error)) { throw_error (context, ERROR_FAILED, "running '%s' failed: %s", argv[0], error->message); - g_error_free (error); return; } @@ -1274,7 +1247,7 @@ daemon_delete_user_authorized_cb (Daemon *daemon, { DeleteUserData *ud = data; - GError *error; + g_autoptr(GError) error = NULL; struct passwd *pwent; const gchar *argv[6]; User *user; @@ -1283,7 +1256,6 @@ daemon_delete_user_authorized_cb (Daemon *daemon, if (pwent == NULL) { throw_error (context, ERROR_USER_DOES_NOT_EXIST, "No user with uid %d found", ud->uid); - return; } @@ -1294,7 +1266,7 @@ daemon_delete_user_authorized_cb (Daemon *daemon, if (user != NULL) { user_set_cached (user, FALSE); - if (daemon->priv->autologin == user) { + if (daemon->priv->autologin == user) { daemon_local_set_automatic_login (daemon, user, FALSE, NULL); } } @@ -1319,7 +1291,6 @@ daemon_delete_user_authorized_cb (Daemon *daemon, error = NULL; if (!spawn_with_login_uid (context, argv, &error)) { throw_error (context, ERROR_FAILED, "running '%s' failed: %s", argv[0], error->message); - g_error_free (error); return; } @@ -1387,7 +1358,7 @@ check_auth_cb (PolkitAuthority *authority, { CheckAuthData *cad = data; PolkitAuthorizationResult *result; - GError *error; + g_autoptr(GError) error = NULL; gboolean is_authorized; is_authorized = FALSE; @@ -1396,7 +1367,6 @@ check_auth_cb (PolkitAuthority *authority, result = polkit_authority_check_authorization_finish (authority, res, &error); if (error) { throw_error (cad->context, ERROR_PERMISSION_DENIED, "Not authorized: %s", error->message); - g_error_free (error); } else { if (polkit_authorization_result_get_is_authorized (result)) { @@ -1468,25 +1438,21 @@ load_autologin (Daemon *daemon, gboolean *enabled, GError **error) { - GKeyFile *keyfile; - GError *local_error; - gchar *string; + g_autoptr(GKeyFile) keyfile = NULL; + GError *local_error = NULL; + g_autofree gchar *string = NULL; keyfile = g_key_file_new (); if (!g_key_file_load_from_file (keyfile, PATH_GDM_CUSTOM, G_KEY_FILE_KEEP_COMMENTS, error)) { - g_key_file_free (keyfile); return FALSE; } - local_error = NULL; string = g_key_file_get_string (keyfile, "daemon", "AutomaticLoginEnable", &local_error); if (local_error) { g_propagate_error (error, local_error); - g_key_file_free (keyfile); - g_free (string); return FALSE; } if (string != NULL && (g_ascii_strcasecmp (string, "true") == 0 || strcmp (string, "1") == 0)) { @@ -1495,17 +1461,13 @@ load_autologin (Daemon *daemon, else { *enabled = FALSE; } - g_free (string); *name = g_key_file_get_string (keyfile, "daemon", "AutomaticLogin", &local_error); if (local_error) { g_propagate_error (error, local_error); - g_key_file_free (keyfile); return FALSE; } - g_key_file_free (keyfile); - return TRUE; } @@ -1515,8 +1477,8 @@ save_autologin (Daemon *daemon, gboolean enabled, GError **error) { - GKeyFile *keyfile; - gchar *data; + g_autoptr(GKeyFile) keyfile = NULL; + g_autofree gchar *data = NULL; gboolean result; keyfile = g_key_file_new (); @@ -1524,7 +1486,6 @@ save_autologin (Daemon *daemon, PATH_GDM_CUSTOM, G_KEY_FILE_KEEP_COMMENTS, error)) { - g_key_file_free (keyfile); return FALSE; } @@ -1534,9 +1495,6 @@ save_autologin (Daemon *daemon, data = g_key_file_to_data (keyfile, NULL, NULL); result = g_file_set_contents (PATH_GDM_CUSTOM, data, -1, error); - g_key_file_free (keyfile); - g_free (data); - return result; } @@ -1577,7 +1535,7 @@ daemon_local_set_automatic_login (Daemon *daemon, GHashTable * daemon_get_extension_ifaces (Daemon *daemon) { - return daemon->priv->extension_ifaces; + return daemon->priv->extension_ifaces; } static void @@ -1586,7 +1544,7 @@ get_property (GObject *object, GValue *value, GParamSpec *pspec) { - switch (prop_id) { + switch (prop_id) { case PROP_DAEMON_VERSION: g_value_set_string (value, VERSION); break; @@ -1603,7 +1561,7 @@ set_property (GObject *object, const GValue *value, GParamSpec *pspec) { - switch (prop_id) { + switch (prop_id) { case PROP_DAEMON_VERSION: g_assert_not_reached (); break; |