summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2023-08-29 11:22:43 +0100
committerPhilip Withnall <philip@tecnocode.co.uk>2023-08-29 16:42:00 +0100
commit14b96540df5f078b92f39ac4f499f4437c143ac6 (patch)
tree7d20fe718086c560ede7904c63162833dfb41ad6
parent488445d312a300525650468f9ac8e7f526e5ebf0 (diff)
downloadglib-14b96540df5f078b92f39ac4f499f4437c143ac6.tar.gz
gresourcefile: Fix crash if called with a badly escaped URI
Return an invalid `GFile` instead, as is the custom for VFS functions. Signed-off-by: Philip Withnall <philip@tecnocode.co.uk> Fixes: #3090
-rw-r--r--gio/gresourcefile.c4
-rw-r--r--gio/tests/vfs.c28
2 files changed, 29 insertions, 3 deletions
diff --git a/gio/gresourcefile.c b/gio/gresourcefile.c
index 488fa34e2..1621839cf 100644
--- a/gio/gresourcefile.c
+++ b/gio/gresourcefile.c
@@ -238,6 +238,7 @@ g_resource_file_new_for_path (const char *path)
return G_FILE (resource);
}
+/* Will return %NULL if @uri is malformed */
GFile *
_g_resource_file_new (const char *uri)
{
@@ -245,6 +246,9 @@ _g_resource_file_new (const char *uri)
char *path;
path = g_uri_unescape_string (uri + strlen ("resource:"), NULL);
+ if (path == NULL)
+ return NULL;
+
resource = g_resource_file_new_for_path (path);
g_free (path);
diff --git a/gio/tests/vfs.c b/gio/tests/vfs.c
index edfa1b7ee..c94b40701 100644
--- a/gio/tests/vfs.c
+++ b/gio/tests/vfs.c
@@ -108,18 +108,39 @@ test_local (void)
gchar **schemes;
vfs = g_vfs_get_local ();
- g_assert (g_vfs_is_active (vfs));
+ g_assert_true (g_vfs_is_active (vfs));
file = g_vfs_get_file_for_uri (vfs, "not a good uri");
- g_assert (G_IS_FILE (file));
+ g_assert_true (G_IS_FILE (file));
g_object_unref (file);
schemes = (gchar **)g_vfs_get_supported_uri_schemes (vfs);
- g_assert (g_strv_length (schemes) > 0);
+ g_assert_cmpuint (g_strv_length (schemes), >, 0);
g_assert_cmpstr (schemes[0], ==, "file");
}
+static void
+test_resource_malformed_escaping (void)
+{
+ GVfs *vfs;
+ GFile *file;
+
+ g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3090");
+ g_test_summary ("Test that g_vfs_get_file_for_uri() returns an invalid file for an invalid URI");
+
+ vfs = g_vfs_get_local ();
+ g_assert_true (g_vfs_is_active (vfs));
+
+ file = g_vfs_get_file_for_uri (vfs, "resource:///%not-valid-escaping/gtk.css");
+ g_assert_true (G_IS_FILE (file));
+
+ /* This only returns %NULL if the file was constructed with an invalid URI: */
+ g_assert_null (g_file_get_uri_scheme (file));
+
+ g_object_unref (file);
+}
+
int
main (int argc, char *argv[])
{
@@ -127,6 +148,7 @@ main (int argc, char *argv[])
g_test_add_func ("/gvfs/local", test_local);
g_test_add_func ("/gvfs/register-scheme", test_register_scheme);
+ g_test_add_func ("/gvfs/resource/malformed-escaping", test_resource_malformed_escaping);
return g_test_run ();
}