summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuel Fleury <emmanuel.fleury@u-bordeaux.fr>2019-07-03 16:21:01 +0200
committerEmmanuel Fleury <emmanuel.fleury@u-bordeaux.fr>2019-09-14 18:01:22 +0200
commit568720006cd1da3390c239915337ed0a56a23f2e (patch)
tree86597ec57bf5ee3d346f02df0bff06e0dc404596
parent86c282cd7869e52de3fa462663751592800242c4 (diff)
downloadglib-568720006cd1da3390c239915337ed0a56a23f2e.tar.gz
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
-rw-r--r--glib/gutf8.c5
-rw-r--r--glib/tests/utf8-validate.c6
2 files changed, 11 insertions, 0 deletions
diff --git a/glib/gutf8.c b/glib/gutf8.c
index cba97db7d..b67d09362 100644
--- a/glib/gutf8.c
+++ b/glib/gutf8.c
@@ -685,6 +685,11 @@ g_utf8_get_char_validated (const gchar *p,
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)
return result;
else if (!UNICODE_VALID (result))
diff --git a/glib/tests/utf8-validate.c b/glib/tests/utf8-validate.c
index 5806b29a0..51543f4b2 100644
--- a/glib/tests/utf8-validate.c
+++ b/glib/tests/utf8-validate.c
@@ -316,6 +316,11 @@ test_utf8_get_char_validated (void)
* that’s how it’s documented: */
{ "", 0, (gunichar) -2 },
{ "", -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: */
{ "hello", 5, (gunichar) 'h' },
{ "hello", -1, (gunichar) 'h' },
@@ -323,6 +328,7 @@ test_utf8_get_char_validated (void)
{ "\xD8\x9F", -1, 0x061F },
{ "\xD8\x9Fmore", 6, 0x061F },
{ "\xD8\x9Fmore", -1, 0x061F },
+ { "\xD8\x9F\0", 3, 0x061F },
{ "\xE2\x96\xB3", 3, 0x25B3 },
{ "\xE2\x96\xB3", -1, 0x25B3 },
{ "\xE2\x96\xB3more", 7, 0x25B3 },