[gdm/wip/wayland-rebase: 12/16] session: Prevent memory leaks by removing strdup accessors



commit 6d521bb05a93cea8677f5edf5277a009785272ff
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Feb 11 13:20:28 2014 -0500

    session: Prevent memory leaks by removing strdup accessors
    
    gdm_session_get_* dup the strings they return, but not a lot of code
    frees the resulting strings. It's a lot easier to not dup the return
    values and make them const char *, as they won't ever change while
    some code holds onto them...

 daemon/gdm-manager.c        |   31 ++++++++-----------------------
 daemon/gdm-session-worker.c |    3 +--
 daemon/gdm-session.c        |   16 ++++++++--------
 daemon/gdm-session.h        |   10 +++++-----
 daemon/gdm-simple-slave.c   |   11 ++++-------
 5 files changed, 26 insertions(+), 45 deletions(-)
---
diff --git a/daemon/gdm-manager.c b/daemon/gdm-manager.c
index 9dfb788..2f03248 100644
--- a/daemon/gdm-manager.c
+++ b/daemon/gdm-manager.c
@@ -449,7 +449,7 @@ find_session_for_user_on_seat (GdmManager *manager,
 
         for (node = manager->priv->user_sessions; node != NULL; node = node->next) {
                 GdmSession *candidate_session = node->data;
-                char *candidate_username, *candidate_seat_id;
+                const char *candidate_username, *candidate_seat_id;
 
                 if (candidate_session == dont_count_session)
                         continue;
@@ -462,14 +462,8 @@ find_session_for_user_on_seat (GdmManager *manager,
 
                 if (g_strcmp0 (candidate_username, username) == 0 &&
                     g_strcmp0 (candidate_seat_id, seat_id) == 0) {
-                        g_free (candidate_username);
-                        g_free (candidate_seat_id);
-
                         return candidate_session;
                 }
-
-                g_free (candidate_username);
-                g_free (candidate_seat_id);
         }
 
         return NULL;
@@ -482,16 +476,12 @@ find_session_with_id (GdmManager *manager,
         GList *node;
         for (node = manager->priv->user_sessions; node != NULL; node = node->next) {
                 GdmSession *candidate_session = node->data;
-                char *candidate_id;
+                const char *candidate_id;
 
                 candidate_id = gdm_session_get_session_id (candidate_session);
                 if (g_strcmp0 (id, candidate_id) == 0) {
-                        g_free (candidate_id);
-
                         return candidate_session;
                 }
-
-                g_free (candidate_id);
         }
 
         return NULL;
@@ -571,9 +561,9 @@ switch_to_compatible_user_session (GdmManager *manager,
 {
         gboolean    res;
         gboolean    ret;
-        char       *username;
-        char       *seat_id;
-        char       *ssid_to_activate = NULL;
+        const char *username;
+        const char *seat_id;
+        const char *ssid_to_activate = NULL;
         GdmSession *existing_session;
 
         ret = FALSE;
@@ -607,8 +597,6 @@ switch_to_compatible_user_session (GdmManager *manager,
         ret = TRUE;
 
  out:
-        g_free (ssid_to_activate);
-
         return ret;
 }
 
@@ -621,7 +609,7 @@ gdm_manager_handle_open_session (GdmDBusManager        *manager,
         GError           *error = NULL;
         GDBusConnection  *connection;
         GdmSession       *session;
-        char             *address;
+        const char       *address;
         GPid              pid;
         uid_t             uid, allowed_user;
 
@@ -667,7 +655,6 @@ gdm_manager_handle_open_session (GdmDBusManager        *manager,
         gdm_dbus_manager_complete_open_session (GDM_DBUS_MANAGER (manager),
                                                 invocation,
                                                 address);
-        g_free (address);
 
         return TRUE;
 }
@@ -810,7 +797,7 @@ start_user_session (GdmManager *manager,
 {
         if (display != NULL) {
                 char *auth_file;
-                char *username;
+                const char *username;
 
                 auth_file = NULL;
                 username = gdm_session_get_username (operation->session);
@@ -818,7 +805,6 @@ start_user_session (GdmManager *manager,
                                                     username,
                                                     &auth_file,
                                                     NULL);
-                g_free (username);
 
                 g_assert (auth_file != NULL);
 
@@ -858,13 +844,12 @@ on_start_user_session (StartUserSessionOperation *operation)
                 destroy_start_user_session_operation (operation);
         } else {
                 GdmDisplay *display;
-                char *username;
+                const char *username;
 
                 display = get_display_for_session (operation->manager, operation->session);
                 username = gdm_session_get_username (operation->session);
 
                 gdm_display_stop_initial_session (display, username);
-                g_free (username);
 
                 start_user_session (operation->manager, display, operation);
         }
diff --git a/daemon/gdm-session-worker.c b/daemon/gdm-session-worker.c
index 8ea5b77..772179a 100644
--- a/daemon/gdm-session-worker.c
+++ b/daemon/gdm-session-worker.c
@@ -2770,7 +2770,7 @@ reauthentication_request_new (GdmSessionWorker      *worker,
 {
         ReauthenticationRequest *request;
         const char * const * environment;
-        char *address;
+        const char *address;
 
         environment = gdm_session_worker_get_environment (worker);
 
@@ -2819,7 +2819,6 @@ reauthentication_request_new (GdmSessionWorker      *worker,
         gdm_dbus_worker_complete_start_reauthentication (GDM_DBUS_WORKER (worker),
                                                          invocation,
                                                          address);
-        g_free (address);
 
         return request;
 }
diff --git a/daemon/gdm-session.c b/daemon/gdm-session.c
index 7beb8a3..7b6b5e2 100644
--- a/daemon/gdm-session.c
+++ b/daemon/gdm-session.c
@@ -2606,31 +2606,31 @@ gdm_session_start_reauthentication (GdmSession *session,
                                                      conversation);
 }
 
-char *
+const char *
 gdm_session_get_server_address (GdmSession *self)
 {
         g_return_val_if_fail (GDM_IS_SESSION (self), NULL);
 
-        return g_strdup (g_dbus_server_get_client_address (self->priv->outside_server));
+        return g_dbus_server_get_client_address (self->priv->outside_server);
 }
 
-char *
+const char *
 gdm_session_get_username (GdmSession *self)
 {
         g_return_val_if_fail (GDM_IS_SESSION (self), NULL);
 
-        return g_strdup (self->priv->selected_user);
+        return self->priv->selected_user;
 }
 
-char *
+const char *
 gdm_session_get_display_device (GdmSession *self)
 {
         g_return_val_if_fail (GDM_IS_SESSION (self), NULL);
 
-        return g_strdup (self->priv->display_device);
+        return self->priv->display_device;
 }
 
-char *
+const char *
 gdm_session_get_display_seat_id (GdmSession *self)
 {
         g_return_val_if_fail (GDM_IS_SESSION (self), NULL);
@@ -2638,7 +2638,7 @@ gdm_session_get_display_seat_id (GdmSession *self)
         return g_strdup (self->priv->display_seat_id);
 }
 
-char *
+const char *
 gdm_session_get_session_id (GdmSession *self)
 {
         GdmSessionConversation *conversation;
diff --git a/daemon/gdm-session.h b/daemon/gdm-session.h
index 33ff43a..fb190bf 100644
--- a/daemon/gdm-session.h
+++ b/daemon/gdm-session.h
@@ -105,11 +105,11 @@ void              gdm_session_start_reauthentication (GdmSession *session,
                                                       GPid        pid_of_caller,
                                                       uid_t       uid_of_caller);
 
-char             *gdm_session_get_server_address          (GdmSession     *session);
-char             *gdm_session_get_username                (GdmSession     *session);
-char             *gdm_session_get_display_device          (GdmSession     *session);
-char             *gdm_session_get_display_seat_id         (GdmSession     *session);
-char             *gdm_session_get_session_id              (GdmSession     *session);
+const char       *gdm_session_get_server_address          (GdmSession     *session);
+const char       *gdm_session_get_username                (GdmSession     *session);
+const char       *gdm_session_get_display_device          (GdmSession     *session);
+const char       *gdm_session_get_display_seat_id         (GdmSession     *session);
+const char       *gdm_session_get_session_id              (GdmSession     *session);
 gboolean          gdm_session_bypasses_xsession           (GdmSession     *session);
 
 void              gdm_session_start_conversation          (GdmSession *session,
diff --git a/daemon/gdm-simple-slave.c b/daemon/gdm-simple-slave.c
index 15390ba..6775710 100644
--- a/daemon/gdm-simple-slave.c
+++ b/daemon/gdm-simple-slave.c
@@ -226,8 +226,8 @@ out:
 static void
 gdm_simple_slave_grant_console_permissions (GdmSimpleSlave *slave)
 {
-        char *username;
-        char *display_device;
+        const char *username;
+        const char *display_device;
         struct passwd *passwd_entry;
 
         username = gdm_session_get_username (slave->priv->session);
@@ -262,8 +262,8 @@ gdm_simple_slave_grant_console_permissions (GdmSimpleSlave *slave)
 static void
 gdm_simple_slave_revoke_console_permissions (GdmSimpleSlave *slave)
 {
-        char *username;
-        char *display_device;
+        const char *username;
+        const char *display_device;
 
         username = gdm_session_get_username (slave->priv->session);
         display_device = gdm_session_get_display_device (slave->priv->session);
@@ -286,9 +286,6 @@ gdm_simple_slave_revoke_console_permissions (GdmSimpleSlave *slave)
                 g_debug ("Not calling di_devperm_logout logout for user %s, device %s",
                          username, display_device);
         }
-
-        g_free (username);
-        g_free (display_device);
 }
 #endif  /* HAVE_LOGINDEVPERM */
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]