mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-09-28 10:07:13 +02:00
ghostutils: Treat 0x80 (and above) as non-ASCII
Any ASCII character above 0x7F should be treated as UTF-8 in ghostutils functions because GLib expects host names to be either punycode encoded or in valid UTF-8 format. The checks in gutf8.c already treat 0x80 as non-ASCII, but two checks in ghostutils.c erroneously check for "great than" not "greater than or equal to". Clarify this by adding a new macro which is reused by PUNICODE_IS_BASIC for better documentation in code.
This commit is contained in:
@@ -56,7 +56,8 @@
|
||||
#define PUNYCODE_INITIAL_BIAS 72
|
||||
#define PUNYCODE_INITIAL_N 0x80
|
||||
|
||||
#define PUNYCODE_IS_BASIC(cp) ((guint)(cp) < 0x80)
|
||||
#define IS_ASCII(cp) ((guint) (cp) < 0x80)
|
||||
#define PUNYCODE_IS_BASIC(cp) IS_ASCII (cp)
|
||||
|
||||
/* Encode/decode a single base-36 digit */
|
||||
static inline gchar
|
||||
@@ -258,8 +259,8 @@ contains_non_ascii (const gchar *str,
|
||||
|
||||
for (p = str; len == -1 ? *p : p < str + len; p++)
|
||||
{
|
||||
if ((guchar)*p > 0x80)
|
||||
return TRUE;
|
||||
if (!IS_ASCII (*p))
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
@@ -505,9 +506,9 @@ g_hostname_to_ascii (const gchar *hostname)
|
||||
unicode = FALSE;
|
||||
for (p = label; *p && !idna_is_dot (p); p++)
|
||||
{
|
||||
if ((guchar)*p > 0x80)
|
||||
unicode = TRUE;
|
||||
}
|
||||
if (!IS_ASCII (*p))
|
||||
unicode = TRUE;
|
||||
}
|
||||
|
||||
oldlen = out->len;
|
||||
llen = p - label;
|
||||
|
@@ -72,6 +72,7 @@ static const gint num_non_round_trip_names = G_N_ELEMENTS (non_round_trip_names)
|
||||
static const gchar *bad_names[] = {
|
||||
"disallowed\xef\xbf\xbd" "character",
|
||||
"non-utf\x88",
|
||||
"smallest-non-utf-char\x80",
|
||||
"xn--mixed-\xc3\xbcp",
|
||||
"verylongverylongverylongverylongverylongverylongverylongverylongverylong"
|
||||
"verylongverylongverylongverylongverylongverylongverylongverylongverylong"
|
||||
@@ -331,6 +332,30 @@ test_is_ip_addr (void)
|
||||
}
|
||||
}
|
||||
|
||||
static const gchar *ascii_names[] = {
|
||||
"ascii-\x7F",
|
||||
};
|
||||
|
||||
static const gchar *non_ascii_names[] = {
|
||||
"disallowed\x80" "character",
|
||||
};
|
||||
|
||||
static void
|
||||
test_hostname_is_non_ascii (void)
|
||||
{
|
||||
for (size_t i = 0; i < G_N_ELEMENTS (ascii_names); i++)
|
||||
g_assert_false (g_hostname_is_non_ascii (ascii_names[i]));
|
||||
|
||||
for (size_t i = 0; i < G_N_ELEMENTS (idn_test_domains); i++)
|
||||
g_assert_false (g_hostname_is_non_ascii (idn_test_domains[i].ascii_name));
|
||||
|
||||
for (size_t i = 0; i < G_N_ELEMENTS (non_ascii_names); i++)
|
||||
g_assert_true (g_hostname_is_non_ascii (non_ascii_names[i]));
|
||||
|
||||
for (size_t i = 0; i < G_N_ELEMENTS (idn_test_domains); i++)
|
||||
g_assert_true (g_hostname_is_non_ascii (idn_test_domains[i].unicode_name));
|
||||
}
|
||||
|
||||
/* FIXME: test names with both unicode and ACE-encoded labels */
|
||||
/* FIXME: test invalid unicode names */
|
||||
|
||||
@@ -362,6 +387,7 @@ main (int argc,
|
||||
return 0;
|
||||
}
|
||||
|
||||
g_test_add_func ("/hostutils/hostname_is_non_ascii", test_hostname_is_non_ascii);
|
||||
g_test_add_func ("/hostutils/to_ascii", test_to_ascii);
|
||||
g_test_add_func ("/hostutils/to_unicode", test_to_unicode);
|
||||
g_test_add_func ("/hostutils/is_ip_addr", test_is_ip_addr);
|
||||
|
Reference in New Issue
Block a user