From 40c8b41c6780e02b233ab934625c419b64f86074 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 4 Oct 2018 13:22:13 +0100 Subject: [PATCH] glib: Port various callers to use g_utf8_validate_len() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- gio/glocalfileinfo.c | 2 +- glib/giochannel.c | 3 ++- glib/gmarkup.c | 5 +++-- glib/gvariant-serialiser.c | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/gio/glocalfileinfo.c b/gio/glocalfileinfo.c index c245924fe..6d4cbc69c 100644 --- a/gio/glocalfileinfo.c +++ b/gio/glocalfileinfo.c @@ -1067,7 +1067,7 @@ make_valid_utf8 (const char *name) while (remaining_bytes != 0) { - if (g_utf8_validate (remainder, remaining_bytes, &invalid)) + if (g_utf8_validate (remainder, remaining_bytes, &invalid)) break; valid_bytes = invalid - remainder; diff --git a/glib/giochannel.c b/glib/giochannel.c index a9567c90d..f01817a83 100644 --- a/glib/giochannel.c +++ b/glib/giochannel.c @@ -39,6 +39,7 @@ #include "gstrfuncs.h" #include "gtestutils.h" #include "glibintl.h" +#include "gunicodeprivate.h" /** @@ -2323,7 +2324,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; diff --git a/glib/gmarkup.c b/glib/gmarkup.c index d0b4823a9..a757fd0af 100644 --- a/glib/gmarkup.c +++ b/glib/gmarkup.c @@ -35,6 +35,7 @@ #include "gtestutils.h" #include "glibintl.h" #include "gthread.h" +#include "gunicodeprivate.h" /** * SECTION:markup @@ -455,7 +456,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 +539,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); diff --git a/glib/gvariant-serialiser.c b/glib/gvariant-serialiser.c index ccf96b4aa..c6e400b53 100644 --- a/glib/gvariant-serialiser.c +++ b/glib/gvariant-serialiser.c @@ -22,6 +22,7 @@ #include "config.h" #include "gvariant-serialiser.h" +#include "gunicodeprivate.h" #include #include @@ -1652,7 +1653,7 @@ g_variant_serialiser_is_string (gconstpointer data, if (*expected_end != '\0') return FALSE; - g_utf8_validate_len (data, size, &end); + _g_utf8_validate_len (data, size, &end); return end == expected_end; }