From 2c0c03ad27eb972b15798b86b21561afed55fd64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Tue, 29 Apr 2025 00:29:20 +0200 Subject: [PATCH] shell: Handle empty comment gracefully Parsing scripts using g_shell_parse_argv() line by line, it's useful to have empty comments, so one can write pragraphs etc, e.g. # This is a comment with multiple paragraphs. # # It's useful to split things up like this at times. # # The empty comment characters makes it clear the paragraphs form a # single comment. --- glib/gshell.c | 5 +++++ glib/tests/shell.c | 1 + 2 files changed, 6 insertions(+) diff --git a/glib/gshell.c b/glib/gshell.c index 040e2e46b..f59267eb8 100644 --- a/glib/gshell.c +++ b/glib/gshell.c @@ -580,6 +580,11 @@ tokenize_command_line (const gchar *command_line, _("Text ended just after a ā€œ\\ā€ character." " (The text was ā€œ%sā€)"), command_line); + else if (current_quote == '#') + g_set_error (error, + G_SHELL_ERROR, + G_SHELL_ERROR_EMPTY_STRING, + _("Text was empty (or contained only whitespace)")); else g_set_error (error, G_SHELL_ERROR, diff --git a/glib/tests/shell.c b/glib/tests/shell.c index e00228351..80781dbbc 100644 --- a/glib/tests/shell.c +++ b/glib/tests/shell.c @@ -67,6 +67,7 @@ static CmdlineTest cmdline_tests[] = { "", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING }, { " ", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING }, { "# foo bar", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING }, + { "#", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING }, {"foo '/bar/summer'\\''09 tours.pdf'", 2, {"foo", "/bar/summer'09 tours.pdf", NULL}, -1} };