Skip to content
This repository was archived by the owner on Oct 25, 2018. It is now read-only.

Commit f39af8d

Browse files
committed
Avoid unnecessary escapes in desktop file exports
Strings that are regular can be used as-is rather than escaping. This is estetically nicer, but it also allows apps that don't fully unescape things to work better. For instance, gnome-shell didn't like if %U is escaped.
1 parent 9ae639a commit f39af8d

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

xdg-app-dir.c

+32-4
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,32 @@ read_fd (int fd,
878878
return TRUE;
879879
}
880880

881+
/* This is conservative, but lets us avoid escaping most
882+
regular Exec= lines, which is nice as that can sometimes
883+
cause problems for apps launching desktop files. */
884+
static gboolean
885+
need_quotes (const char *str)
886+
{
887+
const char *p;
888+
889+
for (p = str; *p; p++)
890+
{
891+
if (!g_ascii_isalnum (*p) &&
892+
strchr ("-_%.=:/@", *p) == NULL)
893+
return TRUE;
894+
}
895+
896+
return FALSE;
897+
}
898+
899+
static char *
900+
maybe_quote (const char *str)
901+
{
902+
if (need_quotes (str))
903+
return g_shell_quote (str);
904+
return g_strdup (str);
905+
}
906+
881907
static gboolean
882908
export_desktop_file (const char *app,
883909
const char *branch,
@@ -903,7 +929,9 @@ export_desktop_file (const char *app,
903929
glnx_strfreev gchar **old_argv = NULL;
904930
glnx_strfreev gchar **groups = NULL;
905931
GString *new_exec = NULL;
906-
g_autofree char *escaped_app = g_shell_quote (app);
932+
g_autofree char *escaped_app = maybe_quote (app);
933+
g_autofree char *escaped_branch = maybe_quote (branch);
934+
g_autofree char *escaped_arch = maybe_quote (arch);
907935
int i;
908936

909937
if (!gs_file_openat_noatime (parent_fd, name, &desktop_fd, cancellable, error))
@@ -940,13 +968,13 @@ export_desktop_file (const char *app,
940968
g_key_file_remove_key (keyfile, groups[i], "X-GNOME-Bugzilla-ExtraInfoScript", NULL);
941969

942970
new_exec = g_string_new ("");
943-
g_string_append_printf (new_exec, XDG_APP_BINDIR"/xdg-app run --branch='%s' --arch='%s'", branch, arch);
971+
g_string_append_printf (new_exec, XDG_APP_BINDIR"/xdg-app run --branch=%s --arch=%s", escaped_branch, escaped_arch);
944972

945973
old_exec = g_key_file_get_string (keyfile, groups[i], "Exec", NULL);
946974
if (old_exec && g_shell_parse_argv (old_exec, &old_argc, &old_argv, NULL) && old_argc >= 1)
947975
{
948976
int i;
949-
g_autofree char *command = g_shell_quote (old_argv[0]);
977+
g_autofree char *command = maybe_quote (old_argv[0]);
950978

951979
g_string_append_printf (new_exec, " --command=%s", command);
952980

@@ -955,7 +983,7 @@ export_desktop_file (const char *app,
955983

956984
for (i = 1; i < old_argc; i++)
957985
{
958-
g_autofree char *arg = g_shell_quote (old_argv[i]);
986+
g_autofree char *arg = maybe_quote (old_argv[i]);
959987
g_string_append (new_exec, " ");
960988
g_string_append (new_exec, arg);
961989
}

0 commit comments

Comments
 (0)