mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-24 11:12:11 +01:00
g_get_host_name: ensure hostname has UTF8 encoding on Windows
Ensures that the hostname returned by g_get_host_name is always UTF8 encoded. Previously, on Windows, the returned string would be encoded in the current codepage, if it contained non-ASCII characters. The unit test for g_get_host_name was updated with a check to ensure that the hostname is indeed at UTF-8 string. https://bugzilla.gnome.org/show_bug.cgi?id=789755
This commit is contained in:
parent
41112ef00d
commit
d011223085
@ -970,6 +970,8 @@ g_get_tmp_dir (void)
|
||||
* name can be determined, a default fixed string "localhost" is
|
||||
* returned.
|
||||
*
|
||||
* The encoding of the returned string is UTF-8.
|
||||
*
|
||||
* Returns: the host name of the machine.
|
||||
*
|
||||
* Since: 2.8
|
||||
@ -982,16 +984,23 @@ g_get_host_name (void)
|
||||
if (g_once_init_enter (&hostname))
|
||||
{
|
||||
gboolean failed;
|
||||
gchar tmp[100];
|
||||
gchar *utmp;
|
||||
|
||||
#ifndef G_OS_WIN32
|
||||
failed = (gethostname (tmp, sizeof (tmp)) == -1);
|
||||
gchar *tmp = g_malloc (sizeof (gchar) * 100);
|
||||
failed = (gethostname (tmp, sizeof (gchar) * 100) == -1);
|
||||
utmp = tmp;
|
||||
#else
|
||||
DWORD size = sizeof (tmp);
|
||||
failed = (!GetComputerName (tmp, &size));
|
||||
wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
|
||||
DWORD size = sizeof (tmp) / sizeof (tmp[0]);
|
||||
failed = (!GetComputerNameW (tmp, &size));
|
||||
if (!failed)
|
||||
utmp = g_utf16_to_utf8 (tmp, size, NULL, NULL, NULL);
|
||||
if (utmp == NULL)
|
||||
failed = TRUE;
|
||||
#endif
|
||||
|
||||
g_once_init_leave (&hostname, g_strdup (failed ? "localhost" : tmp));
|
||||
g_once_init_leave (&hostname, failed ? g_strdup ("localhost") : utmp);
|
||||
}
|
||||
|
||||
return hostname;
|
||||
|
@ -391,6 +391,7 @@ test_hostname (void)
|
||||
name = g_get_host_name ();
|
||||
|
||||
g_assert (name != NULL);
|
||||
g_assert_true (g_utf8_validate (name, -1, NULL));
|
||||
}
|
||||
|
||||
#ifdef G_OS_UNIX
|
||||
|
Loading…
x
Reference in New Issue
Block a user