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
This commit is contained in:
Philip Withnall 2023-08-29 11:22:43 +01:00
parent b35e70a701
commit ed03b1f3f6
2 changed files with 29 additions and 3 deletions

View File

@ -238,6 +238,7 @@ g_resource_file_new_for_path (const char *path)
return G_FILE (resource); return G_FILE (resource);
} }
/* Will return %NULL if @uri is malformed */
GFile * GFile *
_g_resource_file_new (const char *uri) _g_resource_file_new (const char *uri)
{ {
@ -245,6 +246,9 @@ _g_resource_file_new (const char *uri)
char *path; char *path;
path = g_uri_unescape_string (uri + strlen ("resource:"), NULL); path = g_uri_unescape_string (uri + strlen ("resource:"), NULL);
if (path == NULL)
return NULL;
resource = g_resource_file_new_for_path (path); resource = g_resource_file_new_for_path (path);
g_free (path); g_free (path);

View File

@ -108,18 +108,39 @@ test_local (void)
gchar **schemes; gchar **schemes;
vfs = g_vfs_get_local (); 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"); 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); g_object_unref (file);
schemes = (gchar **)g_vfs_get_supported_uri_schemes (vfs); 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"); 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 int
main (int argc, char *argv[]) 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/local", test_local);
g_test_add_func ("/gvfs/register-scheme", test_register_scheme); 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 (); return g_test_run ();
} }