mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-24 19:22:11 +01:00
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:
parent
c5202bc5e9
commit
b829b762fd
@ -1803,7 +1803,7 @@ g_utf8_make_valid (const gchar *str,
|
|||||||
if (string == NULL)
|
if (string == NULL)
|
||||||
return g_strndup (str, len);
|
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_string_append_c (string, '\0');
|
||||||
|
|
||||||
g_assert (g_utf8_validate (string->str, -1, NULL));
|
g_assert (g_utf8_validate (string->str, -1, NULL));
|
||||||
|
@ -145,6 +145,11 @@ test_utf8_make_valid (void)
|
|||||||
g_assert_cmpstr (r, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl");
|
g_assert_cmpstr (r, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl");
|
||||||
g_free (r);
|
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 */
|
/* invalid UTF8 with embedded nul */
|
||||||
r = g_utf8_make_valid ("\xe2\x82\xa0gh\xe2\x00jl", 9);
|
r = g_utf8_make_valid ("\xe2\x82\xa0gh\xe2\x00jl", 9);
|
||||||
g_assert_cmpstr (r, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl");
|
g_assert_cmpstr (r, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user