glib: Port various callers to use g_utf8_validate_len()

These were callers which explicitly specified the string length to
g_utf8_validate(), when it couldn’t be negative, and hence should be
able to unconditionally benefit from the increased string handling
length.

At least one call site would have previously silently changed behaviour
if called with strings longer than G_MAXSSIZE in length.

Another call site was passing strlen(string) to g_utf8_validate(), which
seems pointless: just pass -1 instead, and let g_utf8_validate()
calculate the string length. Its behaviour on embedded nul bytes
wouldn’t change, as strlen() stops at the first one.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2018-10-04 13:22:13 +01:00
parent 7a4025cac1
commit 1c421b0158
3 changed files with 5 additions and 5 deletions

View File

@ -1063,7 +1063,7 @@ make_valid_utf8 (const char *name)
{
GString *string;
const gchar *remainder, *invalid;
gint remaining_bytes, valid_bytes;
gsize remaining_bytes, valid_bytes;
string = NULL;
remainder = name;
@ -1071,7 +1071,7 @@ make_valid_utf8 (const char *name)
while (remaining_bytes != 0)
{
if (g_utf8_validate (remainder, remaining_bytes, &invalid))
if (g_utf8_validate_len (remainder, remaining_bytes, &invalid))
break;
valid_bytes = invalid - remainder;

View File

@ -2323,7 +2323,7 @@ reconvert:
/* UTF-8, just validate, emulate g_iconv */
if (!g_utf8_validate (from_buf, try_len, &badchar))
if (!g_utf8_validate_len (from_buf, try_len, &badchar))
{
gunichar try_char;
gsize incomplete_len = from_buf + try_len - badchar;

View File

@ -455,7 +455,7 @@ slow_name_validate (GMarkupParseContext *context,
{
const gchar *p = name;
if (!g_utf8_validate (name, strlen (name), NULL))
if (!g_utf8_validate (name, -1, NULL))
{
set_error (context, error, G_MARKUP_ERROR_BAD_UTF8,
_("Invalid UTF-8 encoded text in name — not valid “%s”"), name);
@ -538,7 +538,7 @@ text_validate (GMarkupParseContext *context,
gint len,
GError **error)
{
if (!g_utf8_validate (p, len, NULL))
if (!g_utf8_validate_len (p, len, NULL))
{
set_error (context, error, G_MARKUP_ERROR_BAD_UTF8,
_("Invalid UTF-8 encoded text in name — not valid “%s”"), p);