mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-27 07:56:14 +01:00
Add a missing check to g_utf8_get_char_validated()
g_utf8_get_char_validated() was not exactly matching its documentation. The function was not checking if the sequence of unicode characters was free of null bytes before performing a more in-depth validation. Fix issue #1052
This commit is contained in:
parent
86c282cd78
commit
568720006c
@ -685,6 +685,11 @@ g_utf8_get_char_validated (const gchar *p,
|
|||||||
|
|
||||||
result = g_utf8_get_char_extended (p, max_len);
|
result = g_utf8_get_char_extended (p, max_len);
|
||||||
|
|
||||||
|
/* Disallow codepoint U+0000 as it’s a nul byte,
|
||||||
|
* and all string handling in GLib is nul-terminated */
|
||||||
|
if (result == 0 && max_len > 0)
|
||||||
|
return (gunichar) -2;
|
||||||
|
|
||||||
if (result & 0x80000000)
|
if (result & 0x80000000)
|
||||||
return result;
|
return result;
|
||||||
else if (!UNICODE_VALID (result))
|
else if (!UNICODE_VALID (result))
|
||||||
|
@ -316,6 +316,11 @@ test_utf8_get_char_validated (void)
|
|||||||
* that’s how it’s documented: */
|
* that’s how it’s documented: */
|
||||||
{ "", 0, (gunichar) -2 },
|
{ "", 0, (gunichar) -2 },
|
||||||
{ "", -1, (gunichar) 0 },
|
{ "", -1, (gunichar) 0 },
|
||||||
|
{ "\0", 1, (gunichar) -2 },
|
||||||
|
{ "AB\0", 3, 'A' },
|
||||||
|
{ "A\0B", 3, 'A' },
|
||||||
|
{ "\0AB", 3, (gunichar) -2 },
|
||||||
|
{ "\xD8\0", 2, (gunichar) -2 },
|
||||||
/* Normal inputs: */
|
/* Normal inputs: */
|
||||||
{ "hello", 5, (gunichar) 'h' },
|
{ "hello", 5, (gunichar) 'h' },
|
||||||
{ "hello", -1, (gunichar) 'h' },
|
{ "hello", -1, (gunichar) 'h' },
|
||||||
@ -323,6 +328,7 @@ test_utf8_get_char_validated (void)
|
|||||||
{ "\xD8\x9F", -1, 0x061F },
|
{ "\xD8\x9F", -1, 0x061F },
|
||||||
{ "\xD8\x9Fmore", 6, 0x061F },
|
{ "\xD8\x9Fmore", 6, 0x061F },
|
||||||
{ "\xD8\x9Fmore", -1, 0x061F },
|
{ "\xD8\x9Fmore", -1, 0x061F },
|
||||||
|
{ "\xD8\x9F\0", 3, 0x061F },
|
||||||
{ "\xE2\x96\xB3", 3, 0x25B3 },
|
{ "\xE2\x96\xB3", 3, 0x25B3 },
|
||||||
{ "\xE2\x96\xB3", -1, 0x25B3 },
|
{ "\xE2\x96\xB3", -1, 0x25B3 },
|
||||||
{ "\xE2\x96\xB3more", 7, 0x25B3 },
|
{ "\xE2\x96\xB3more", 7, 0x25B3 },
|
||||||
|
Loading…
Reference in New Issue
Block a user