From 511627b7356af527c85c049e2020a36694d7de54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 2 Sep 2022 18:56:35 +0200 Subject: [PATCH 1/2] tests/dbus-appinfo: Add test case for flatpak opening an invalid file We were testing the case in which we were opening an actual file, and so potentially using a fd-list, however we were missing the case in which a file was not existent. And in such case we are incidentally hitting a leak now. --- gio/tests/dbus-appinfo.c | 79 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/gio/tests/dbus-appinfo.c b/gio/tests/dbus-appinfo.c index 2017e02df..91e76403c 100644 --- a/gio/tests/dbus-appinfo.c +++ b/gio/tests/dbus-appinfo.c @@ -360,6 +360,84 @@ test_flatpak_doc_export (void) g_object_unref (flatpak_appinfo); } +static void +on_flatpak_launch_invalid_uri_finish (GObject *object, + GAsyncResult *result, + gpointer user_data) +{ + GApplication *app = user_data; + GError *error = NULL; + + g_app_info_launch_uris_finish (G_APP_INFO (object), result, &error); + g_assert_no_error (error); + + g_application_release (app); +} + +static void +on_flatpak_activate_invalid_uri (GApplication *app, + gpointer user_data) +{ + GDesktopAppInfo *flatpak_appinfo = user_data; + GList *uris; + + /* The app will be released in on_flatpak_launch_uris_finish */ + g_application_hold (app); + + uris = g_list_prepend (NULL, "file:///hopefully/an/invalid/path.desktop"); + g_app_info_launch_uris_async (G_APP_INFO (flatpak_appinfo), uris, NULL, + NULL, on_flatpak_launch_invalid_uri_finish, app); + g_list_free (uris); +} + +static void +on_flatpak_open_invalid_uri (GApplication *app, + GFile **files, + gint n_files, + const char *hint) +{ + GFile *f; + + g_assert_cmpint (n_files, ==, 1); + g_test_message ("on_flatpak_open received file '%s'", g_file_peek_path (files[0])); + + /* The file has been exported via the document portal */ + f = g_file_new_for_uri ("file:///hopefully/an/invalid/path.desktop"); + g_assert_true (g_file_equal (files[0], f)); + g_object_unref (f); +} + +static void +test_flatpak_missing_doc_export (void) +{ + const gchar *argv[] = { "myapp", NULL }; + gchar *desktop_file = NULL; + GDesktopAppInfo *flatpak_appinfo; + GApplication *app; + int status; + + g_test_summary ("Test that files launched via Flatpak apps are made available via the document portal."); + + desktop_file = g_test_build_filename (G_TEST_DIST, + "org.gtk.test.dbusappinfo.flatpak.desktop", + NULL); + flatpak_appinfo = g_desktop_app_info_new_from_filename (desktop_file); + g_assert_nonnull (flatpak_appinfo); + + app = g_application_new ("org.gtk.test.dbusappinfo.flatpak", + G_APPLICATION_HANDLES_OPEN); + g_signal_connect (app, "activate", G_CALLBACK (on_flatpak_activate_invalid_uri), + flatpak_appinfo); + g_signal_connect (app, "open", G_CALLBACK (on_flatpak_open_invalid_uri), NULL); + + status = g_application_run (app, 1, (gchar **) argv); + g_assert_cmpint (status, ==, 0); + + g_object_unref (app); + g_object_unref (flatpak_appinfo); + g_free (desktop_file); +} + int main (int argc, char **argv) { @@ -367,6 +445,7 @@ main (int argc, char **argv) g_test_add_func ("/appinfo/dbusappinfo", test_dbus_appinfo); g_test_add_func ("/appinfo/flatpak-doc-export", test_flatpak_doc_export); + g_test_add_func ("/appinfo/flatpak-missing-doc-export", test_flatpak_missing_doc_export); return session_bus_run (); } From 27203e48c91ab8b55033dcf1773cb60c0aaed3fa Mon Sep 17 00:00:00 2001 From: Sebastian Keller Date: Tue, 30 Aug 2022 21:39:36 +0200 Subject: [PATCH 2/2] documentportal: Fix small leak in add_documents with empty URI list When called with an empty URI list (or only inaccessible files), g_document_portal_add_documents would not call g_variant_builder_end, leaking the memory allocated by the variant builder. Closes: https://gitlab.gnome.org/GNOME/glib/-/issues/2733 --- gio/gdocumentportal.c | 1 + 1 file changed, 1 insertion(+) diff --git a/gio/gdocumentportal.c b/gio/gdocumentportal.c index c08c36c58..382e2aab6 100644 --- a/gio/gdocumentportal.c +++ b/gio/gdocumentportal.c @@ -203,6 +203,7 @@ g_document_portal_add_documents (GList *uris, else { ruris = g_list_copy_deep (uris, (GCopyFunc)g_strdup, NULL); + g_variant_builder_clear (&builder); } out: