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:
Emmanuel Fleury 2019-07-03 16:21:01 +02:00
parent 86c282cd78
commit 568720006c
2 changed files with 11 additions and 0 deletions

View File

@ -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 its 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))

View File

@ -316,6 +316,11 @@ test_utf8_get_char_validated (void)
* thats how its documented: */ * thats how its 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 },