From 6f6dfc9487422fd2361030c77635c386647ec8fe Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Sat, 9 Jan 2021 15:29:03 +0100 Subject: [PATCH 1/2] gdesktopappinfo: Fix validation of XDG_CURRENT_DESKTOP Split out XDG_CURRENT_DESKTOP handling to a separate function and make sure that it drops all the invalid entries properly. Earlier a bad entry could slip through the checks by sitting just after another bad entry, like in env being set to `invalid1!:invalid2!`, where `invalid2!` could slip the checks. --- gio/gdesktopappinfo.c | 78 +++++++++---------- gio/tests/desktop-app-info.c | 6 +- .../usr/applications/invalid-desktop.desktop | 5 ++ 3 files changed, 45 insertions(+), 44 deletions(-) create mode 100644 gio/tests/desktop-files/usr/applications/invalid-desktop.desktop diff --git a/gio/gdesktopappinfo.c b/gio/gdesktopappinfo.c index 6124d61a8..2b139a01c 100644 --- a/gio/gdesktopappinfo.c +++ b/gio/gdesktopappinfo.c @@ -326,6 +326,34 @@ validate_xdg_desktop (const gchar *desktop) return TRUE; } +static char ** +get_valid_current_desktops (const char *value) +{ + char **tmp; + gsize i; + GPtrArray *valid_desktops; + + if (value == NULL) + value = g_getenv ("XDG_CURRENT_DESKTOP"); + if (value == NULL) + value = ""; + + tmp = g_strsplit (value, G_SEARCHPATH_SEPARATOR_S, 0); + valid_desktops = g_ptr_array_new_full (g_strv_length (tmp) + 1, g_free); + for (i = 0; tmp[i]; i++) + { + if (validate_xdg_desktop (tmp[i])) + g_ptr_array_add (valid_desktops, tmp[i]); + else + g_free (tmp[i]); + } + g_ptr_array_add (valid_desktops, NULL); + g_free (tmp); + tmp = (char **) g_ptr_array_steal (valid_desktops, NULL); + g_ptr_array_unref (valid_desktops); + return tmp; +} + static const gchar * const * get_lowercase_current_desktops (void) { @@ -333,33 +361,15 @@ get_lowercase_current_desktops (void) if (g_once_init_enter (&result)) { - const gchar *envvar; - gchar **tmp; + char **tmp = get_valid_current_desktops (NULL); + gsize i, j; - envvar = g_getenv ("XDG_CURRENT_DESKTOP"); - - if (envvar) + for (i = 0; tmp[i]; i++) { - gint i, j; - gsize tmp_len; - - tmp = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0); - tmp_len = g_strv_length (tmp); - - for (i = 0; tmp[i]; i++) - { - /* If the desktop is invalid, drop it and shift the following - * ones (and trailing %NULL) up. */ - if (!validate_xdg_desktop (tmp[i])) - memmove (tmp + i, tmp + i + 1, tmp_len - i); - - /* Convert to lowercase. */ - for (j = 0; tmp[i][j]; j++) - tmp[i][j] = g_ascii_tolower (tmp[i][j]); - } + /* Convert to lowercase. */ + for (j = 0; tmp[i][j]; j++) + tmp[i][j] = g_ascii_tolower (tmp[i][j]); } - else - tmp = g_new0 (gchar *, 0 + 1); g_once_init_leave (&result, tmp); } @@ -374,25 +384,7 @@ get_current_desktops (const gchar *value) if (g_once_init_enter (&result)) { - gchar **tmp; - gsize tmp_len, i; - - if (!value) - value = g_getenv ("XDG_CURRENT_DESKTOP"); - - if (!value) - value = ""; - - tmp = g_strsplit (value, ":", 0); - tmp_len = g_strv_length (tmp); - - for (i = 0; tmp[i]; i++) - { - /* If the desktop is invalid, drop it and shift the following - * ones (and trailing %NULL) up. */ - if (!validate_xdg_desktop (tmp[i])) - memmove (tmp + i, tmp + i + 1, tmp_len - i); - } + char **tmp = get_valid_current_desktops (value); g_once_init_leave (&result, tmp); } diff --git a/gio/tests/desktop-app-info.c b/gio/tests/desktop-app-info.c index f4e509a59..bc50ff491 100644 --- a/gio/tests/desktop-app-info.c +++ b/gio/tests/desktop-app-info.c @@ -571,7 +571,8 @@ assert_implementations (const gchar *interface, "gnome-terminal.desktop nautilus-autorun-software.desktop gcr-viewer.desktop " \ "nautilus-connect-server.desktop kde4-dolphin.desktop gnome-music.desktop " \ "kde4-konqbrowser.desktop gucharmap.desktop kde4-okular.desktop nautilus.desktop " \ - "gedit.desktop evince.desktop file-roller.desktop dconf-editor.desktop glade.desktop" + "gedit.desktop evince.desktop file-roller.desktop dconf-editor.desktop glade.desktop " \ + "invalid-desktop.desktop" #define HOME_APPS "epiphany-weather-for-toronto-island-9c6a4e022b17686306243dada811d550d25eb1fb.desktop" #define ALL_HOME_APPS HOME_APPS " eog.desktop" @@ -726,6 +727,9 @@ test_show_in (void) assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic"); assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:KDE"); assert_shown ("gcr-prompter.desktop", TRUE, "KDE:GNOME-Classic"); + assert_shown ("invalid-desktop.desktop", TRUE, "GNOME"); + assert_shown ("invalid-desktop.desktop", FALSE, "../invalid/desktop"); + assert_shown ("invalid-desktop.desktop", FALSE, "../invalid/desktop:../invalid/desktop"); } /* Test g_desktop_app_info_launch_uris_as_manager() and diff --git a/gio/tests/desktop-files/usr/applications/invalid-desktop.desktop b/gio/tests/desktop-files/usr/applications/invalid-desktop.desktop new file mode 100644 index 000000000..dffaa2469 --- /dev/null +++ b/gio/tests/desktop-files/usr/applications/invalid-desktop.desktop @@ -0,0 +1,5 @@ +[Desktop Entry] +Type=Application +Name=appinfo-test +OnlyShowIn=../invalid/desktop;GNOME +NotShowIn=ROX; From 4963b48a8482e504c224ac828d31b74099999b56 Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Sat, 9 Jan 2021 16:00:46 +0100 Subject: [PATCH 2/2] tests: Update the expected count in file test We added another desktop file, so update the file count. --- gio/tests/file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gio/tests/file.c b/gio/tests/file.c index c3877af4b..d8769656c 100644 --- a/gio/tests/file.c +++ b/gio/tests/file.c @@ -1041,7 +1041,7 @@ test_measure (void) if (size > 0) g_assert_cmpuint (num_bytes, ==, size); g_assert_cmpuint (num_dirs, ==, 6); - g_assert_cmpuint (num_files, ==, 31); + g_assert_cmpuint (num_files, ==, 32); g_object_unref (file); g_free (path); @@ -1131,7 +1131,7 @@ test_measure_async (void) g_free (path); data->expected_dirs = 6; - data->expected_files = 31; + data->expected_files = 32; g_file_measure_disk_usage_async (file, G_FILE_MEASURE_APPARENT_SIZE,