Merge branch 'timezone-relative-link-target' into 'main'

gtimezone: Fix symlink checks on relative link targets

See merge request GNOME/glib!3009
This commit is contained in:
Philip Withnall 2022-10-25 12:21:40 +00:00
commit 60ceaa77be
2 changed files with 27 additions and 1 deletions

View File

@ -2256,9 +2256,28 @@ g_build_filename (const gchar *first_element,
* @error: return location for a #GError * @error: return location for a #GError
* *
* Reads the contents of the symbolic link @filename like the POSIX * Reads the contents of the symbolic link @filename like the POSIX
* readlink() function. The returned string is in the encoding used * readlink() function.
*
* The returned string is in the encoding used
* for filenames. Use g_filename_to_utf8() to convert it to UTF-8. * for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
* *
* The returned string may also be a relative path. Use g_build_filename() to
* convert it to an absolute path:
* |[
* g_autoptr(GError) local_error = NULL;
* g_autofree gchar *link_target = g_file_read_link ("/etc/localtime", &local_error);
*
* if (local_error != NULL)
* g_error ("Error reading link: %s", local_error->message);
*
* if (!g_path_is_absolute (link_target))
* {
* g_autofree gchar *absolute_link_target = g_build_filename ("/etc", link_target, NULL);
* g_free (link_target);
* link_target = g_steal_pointer (&absolute_link_target);
* }
* ]|
*
* Returns: (type filename) (transfer full): A newly-allocated string with * Returns: (type filename) (transfer full): A newly-allocated string with
* the contents of the symbolic link, or %NULL if an error occurred. * the contents of the symbolic link, or %NULL if an error occurred.
* *

View File

@ -544,6 +544,13 @@ zone_identifier_unix (void)
if (resolved_identifier != NULL) if (resolved_identifier != NULL)
{ {
if (!g_path_is_absolute (resolved_identifier))
{
gchar *absolute_resolved_identifier = g_build_filename ("/etc", resolved_identifier, NULL);
g_free (resolved_identifier);
resolved_identifier = g_steal_pointer (&absolute_resolved_identifier);
}
if (g_lstat (resolved_identifier, &file_status) == 0) if (g_lstat (resolved_identifier, &file_status) == 0)
{ {
if ((file_status.st_mode & S_IFMT) != S_IFREG) if ((file_status.st_mode & S_IFMT) != S_IFREG)