[gnome-control-center] sharing/remote-desktop: handle gracefully when unable to get username



commit 8809dbc5659b78622fb6ab6d17157bdc688ad842
Author: Hu Jialun <hujialun comp nus edu sg>
Date:   Mon Mar 28 14:35:21 2022 +0800

    sharing/remote-desktop: handle gracefully when unable to get username
    
    getlogin() can fail for several reasons as detailed in the man page, and
    the current behaviour is a segmentation fault when it fails with NULL,
    such as due to an unset loginuid.
    
    * Check return value for error and act accordingly.
    * Change to getpwuid(getuid())->pw_name, which is less likely to error.

 panels/sharing/cc-sharing-panel.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
---
diff --git a/panels/sharing/cc-sharing-panel.c b/panels/sharing/cc-sharing-panel.c
index 4c6d8b18f..6b2a0994b 100644
--- a/panels/sharing/cc-sharing-panel.c
+++ b/panels/sharing/cc-sharing-panel.c
@@ -45,6 +45,9 @@
 
 #include <config.h>
 
+#include <unistd.h>
+#include <pwd.h>
+
 static void cc_sharing_panel_setup_label_with_hostname (CcSharingPanel *self, GtkWidget *label);
 static GtkWidget *cc_sharing_panel_new_media_sharing_row (const char     *uri_or_path,
                                                           CcSharingPanel *self);
@@ -1378,10 +1381,20 @@ cc_sharing_panel_setup_remote_desktop_dialog (CcSharingPanel *self)
                             "notify::text",
                             G_CALLBACK (remote_desktop_credentials_changed),
                             self);
-
   if (username == NULL)
-    gtk_editable_set_text (GTK_EDITABLE (self->remote_desktop_username_entry),
-                           getlogin ());
+    {
+      struct passwd *pw = getpwuid (getuid ());
+      if (pw != NULL)
+        {
+          gtk_editable_set_text (GTK_EDITABLE (self->remote_desktop_username_entry),
+                                 pw->pw_name);
+        }
+      else
+        {
+          g_warning ("Failed to get username: %s", g_strerror (errno));
+        }
+    }
+
   if (password == NULL)
     gtk_editable_set_text (GTK_EDITABLE (self->remote_desktop_password_entry),
                            pw_generate ());


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