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.
This commit is contained in:
Krzesimir Nowak 2021-01-09 15:29:03 +01:00 committed by Philip Withnall
parent 13732f800c
commit 6f6dfc9487
3 changed files with 45 additions and 44 deletions

View File

@ -326,6 +326,34 @@ validate_xdg_desktop (const gchar *desktop)
return TRUE; 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 * static const gchar * const *
get_lowercase_current_desktops (void) get_lowercase_current_desktops (void)
{ {
@ -333,33 +361,15 @@ get_lowercase_current_desktops (void)
if (g_once_init_enter (&result)) if (g_once_init_enter (&result))
{ {
const gchar *envvar; char **tmp = get_valid_current_desktops (NULL);
gchar **tmp; gsize i, j;
envvar = g_getenv ("XDG_CURRENT_DESKTOP");
if (envvar)
{
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++) 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. */ /* Convert to lowercase. */
for (j = 0; tmp[i][j]; j++) for (j = 0; tmp[i][j]; j++)
tmp[i][j] = g_ascii_tolower (tmp[i][j]); tmp[i][j] = g_ascii_tolower (tmp[i][j]);
} }
}
else
tmp = g_new0 (gchar *, 0 + 1);
g_once_init_leave (&result, tmp); g_once_init_leave (&result, tmp);
} }
@ -374,25 +384,7 @@ get_current_desktops (const gchar *value)
if (g_once_init_enter (&result)) if (g_once_init_enter (&result))
{ {
gchar **tmp; char **tmp = get_valid_current_desktops (value);
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);
}
g_once_init_leave (&result, tmp); g_once_init_leave (&result, tmp);
} }

View File

@ -571,7 +571,8 @@ assert_implementations (const gchar *interface,
"gnome-terminal.desktop nautilus-autorun-software.desktop gcr-viewer.desktop " \ "gnome-terminal.desktop nautilus-autorun-software.desktop gcr-viewer.desktop " \
"nautilus-connect-server.desktop kde4-dolphin.desktop gnome-music.desktop " \ "nautilus-connect-server.desktop kde4-dolphin.desktop gnome-music.desktop " \
"kde4-konqbrowser.desktop gucharmap.desktop kde4-okular.desktop nautilus.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 HOME_APPS "epiphany-weather-for-toronto-island-9c6a4e022b17686306243dada811d550d25eb1fb.desktop"
#define ALL_HOME_APPS HOME_APPS " eog.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");
assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:KDE"); assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:KDE");
assert_shown ("gcr-prompter.desktop", TRUE, "KDE:GNOME-Classic"); 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 /* Test g_desktop_app_info_launch_uris_as_manager() and

View File

@ -0,0 +1,5 @@
[Desktop Entry]
Type=Application
Name=appinfo-test
OnlyShowIn=../invalid/desktop;GNOME
NotShowIn=ROX;