gutf8: Fix length handling in g_utf8_make_valid()

We cannot blindly append the remainder when a length was provided
because the string isn't nul-terminated.

https://bugzilla.gnome.org/show_bug.cgi?id=789444
This commit is contained in:
Ole André Vadla Ravnås 2017-10-25 00:30:34 +02:00 committed by Philip Withnall
parent c5202bc5e9
commit b829b762fd
2 changed files with 6 additions and 1 deletions

View File

@ -1803,7 +1803,7 @@ g_utf8_make_valid (const gchar *str,
if (string == NULL)
return g_strndup (str, len);
g_string_append (string, remainder);
g_string_append_len (string, remainder, remaining_bytes);
g_string_append_c (string, '\0');
g_assert (g_utf8_validate (string->str, -1, NULL));

View File

@ -145,6 +145,11 @@ test_utf8_make_valid (void)
g_assert_cmpstr (r, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl");
g_free (r);
/* invalid UTF8 without nul terminator followed by something unfortunate */
r = g_utf8_make_valid ("Bj\xc3\xb8", 3);
g_assert_cmpstr (r, ==, "Bj\xef\xbf\xbd");
g_free (r);
/* invalid UTF8 with embedded nul */
r = g_utf8_make_valid ("\xe2\x82\xa0gh\xe2\x00jl", 9);
g_assert_cmpstr (r, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl");