Quote cwd for glob in a way that actually works, from Pavel Roskin in

GitHub issue 4623.
This commit is contained in:
nicm
2025-09-07 14:17:25 +00:00
parent b53cf70262
commit 7cc15969ff

View File

@@ -18,6 +18,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <glob.h> #include <glob.h>
#include <stdlib.h> #include <stdlib.h>
@@ -150,6 +151,21 @@ cmd_source_file_add(struct cmd_source_file_data *cdata, const char *path)
cdata->files[cdata->nfiles++] = xstrdup(path); cdata->files[cdata->nfiles++] = xstrdup(path);
} }
static char *
cmd_source_file_quote_for_glob(const char *path)
{
char *quoted = xmalloc(2 * strlen(path) + 1), *q = quoted;
const char *p = path;
while (*p != '\0') {
if ((u_char)*p < 128 && !isalnum((u_char)*p) && *p != '/')
*q++ = '\\';
*q++ = *p++;
}
*q = '\0';
return (quoted);
}
static enum cmd_retval static enum cmd_retval
cmd_source_file_exec(struct cmd *self, struct cmdq_item *item) cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
{ {
@@ -189,7 +205,7 @@ cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
if (args_has(args, 'v')) if (args_has(args, 'v'))
cdata->flags |= CMD_PARSE_VERBOSE; cdata->flags |= CMD_PARSE_VERBOSE;
utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB); cwd = cmd_source_file_quote_for_glob(server_client_get_cwd(c, NULL));
for (i = 0; i < args_count(args); i++) { for (i = 0; i < args_count(args); i++) {
path = args_string(args, i); path = args_string(args, i);