summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Bainbridge <jamie.bainbridge@gmail.com>2021-09-08 12:08:17 +1000
committerMichael Catanzaro <mcatanzaro@redhat.com>2022-01-27 10:14:14 -0600
commitffc00caa7391e602fd1ee18f2b1522b313942b05 (patch)
tree580965a51519a70f59e2a71a58749118931d1657
parent63d0e9750e16e62b1c928ba15eac22cb9d03ed43 (diff)
downloadglib-ffc00caa7391e602fd1ee18f2b1522b313942b05.tar.gz
gutils: Avoid segfault in g_get_user_database_entry
g_get_user_database_entry() uses variable pwd to store the contents of the call to getpwnam_r(), then capitalises the first letter of pw_name with g_ascii_toupper (pw->pw_name[0]). However, as per the getpwnam manpage, the result of that call "may point to a static area". When this happens, GLib is trying to edit static memory which belongs to a shared library, so segfaults. Instead, copy pw_name off to a temporary variable, set uppercase on that variable, and use the variable to join into the desired string. Free the new variable after it is no longer needed. Signed-off-by: Jamie Bainbridge <jamie.bainbridge@gmail.com>
-rw-r--r--glib/gutils.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/glib/gutils.c b/glib/gutils.c
index dad162528..7503c09ea 100644
--- a/glib/gutils.c
+++ b/glib/gutils.c
@@ -685,14 +685,17 @@ g_get_user_database_entry (void)
{
gchar **gecos_fields;
gchar **name_parts;
+ gchar *uppercase_pw_name;
/* split the gecos field and substitute '&' */
gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
name_parts = g_strsplit (gecos_fields[0], "&", 0);
- pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
- e.real_name = g_strjoinv (pw->pw_name, name_parts);
+ uppercase_pw_name = g_strdup (pw->pw_name);
+ uppercase_pw_name[0] = g_ascii_toupper (uppercase_pw_name[0]);
+ e.real_name = g_strjoinv (uppercase_pw_name, name_parts);
g_strfreev (gecos_fields);
g_strfreev (name_parts);
+ g_free (uppercase_pw_name);
}
#endif