uri: add a GError to the new g_uri_unescape_bytes()

Suggested-by: Matthias Clasen <mclasen@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2020-07-08 17:16:43 +04:00
parent d5513ef72c
commit ea395e3fdd
4 changed files with 23 additions and 9 deletions

View File

@ -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)
{
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),

View File

@ -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;

View File

@ -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,

View File

@ -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,