diff --git a/fuzzing/fuzz_uri_escape.c b/fuzzing/fuzz_uri_escape.c index ca14973f3..6a3d19750 100644 --- a/fuzzing/fuzz_uri_escape.c +++ b/fuzzing/fuzz_uri_escape.c @@ -4,15 +4,20 @@ static void test_bytes (const guint8 *data, gsize size) { + GError *error = NULL; GBytes *unescaped_bytes = NULL; gchar *escaped_string = NULL; if (size > G_MAXSSIZE) return; - unescaped_bytes = g_uri_unescape_bytes ((const gchar *) data, (gssize) size, NULL); + unescaped_bytes = g_uri_unescape_bytes ((const gchar *) data, (gssize) size, NULL, &error); if (unescaped_bytes == NULL) - return; + { + g_assert_nonnull (error); + g_clear_error (&error); + return; + } escaped_string = g_uri_escape_bytes (g_bytes_get_data (unescaped_bytes, NULL), g_bytes_get_size (unescaped_bytes), diff --git a/glib/guri.c b/glib/guri.c index 7a4fc5b18..62d0fa718 100644 --- a/glib/guri.c +++ b/glib/guri.c @@ -2220,6 +2220,7 @@ g_uri_escape_string (const gchar *unescaped, * is NUL-terminated. * @illegal_characters: (nullable): a string of illegal characters * not to be allowed, or %NULL. + * @error: #GError for error reporting, or %NULL to ignore. * * Unescapes a segment of an escaped string as binary data. * @@ -2232,21 +2233,23 @@ g_uri_escape_string (const gchar *unescaped, * want to avoid for instance having a slash being expanded in an * escaped path element, which might confuse pathname handling. * - * Returns: (transfer full): an unescaped version of @escaped_string - * or %NULL on error. The returned #GBytes should be unreffed when no - * longer needed. + * Returns: (transfer full): an unescaped version of @escaped_string or %NULL on + * error (if decoding failed, using %G_URI_ERROR_MISC error code). The returned + * #GBytes should be unreffed when no longer needed. * * Since: 2.66 **/ GBytes * g_uri_unescape_bytes (const gchar *escaped_string, gssize length, - const char *illegal_characters) + const char *illegal_characters, + GError **error) { gchar *buf; gssize unescaped_length; g_return_val_if_fail (escaped_string != NULL, NULL); + g_return_val_if_fail (error == NULL || *error == NULL, NULL); if (length == -1) length = strlen (escaped_string); @@ -2257,7 +2260,7 @@ g_uri_unescape_bytes (const gchar *escaped_string, FALSE, FALSE, G_URI_FLAGS_PARSE_STRICT|G_URI_FLAGS_ENCODED, - 0, NULL); + G_URI_ERROR_MISC, error); if (unescaped_length == -1) return NULL; diff --git a/glib/guri.h b/glib/guri.h index 3c061c9ba..a9a4cd57e 100644 --- a/glib/guri.h +++ b/glib/guri.h @@ -358,7 +358,9 @@ char * g_uri_escape_string (const char *unescaped, GLIB_AVAILABLE_IN_2_66 GBytes * g_uri_unescape_bytes (const char *escaped_string, gssize length, - const char *illegal_characters); + const char *illegal_characters, + GError **error); + GLIB_AVAILABLE_IN_2_66 char * g_uri_escape_bytes (const guchar *unescaped, gsize length, diff --git a/glib/tests/uri.c b/glib/tests/uri.c index 7d3082637..191fa4274 100644 --- a/glib/tests/uri.c +++ b/glib/tests/uri.c @@ -367,6 +367,7 @@ test_uri_unescape_string (void) static void test_uri_unescape_bytes (gconstpointer test_data) { + GError *error = NULL; gboolean use_nul_terminated = GPOINTER_TO_INT (test_data); const struct { @@ -410,14 +411,17 @@ test_uri_unescape_bytes (gconstpointer test_data) escaped = g_memdup (tests[i].escaped, escaped_len); } - bytes = g_uri_unescape_bytes (escaped, escaped_len, tests[i].illegal); + bytes = g_uri_unescape_bytes (escaped, escaped_len, tests[i].illegal, &error); if (tests[i].expected_unescaped_len < 0) { g_assert_null (bytes); + g_assert_error (error, G_URI_ERROR, G_URI_ERROR_MISC); + g_clear_error (&error); } else { + g_assert_no_error (error); g_assert_cmpmem (g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), tests[i].expected_unescaped,