Skip to content

Commit 2939ec6

Browse files
committed
escape browse path in cmd_browse
previously, if a directory contained a space (or other special shell characters), the shell command would fail.
1 parent eb8c4d6 commit 2939ec6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

cmd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,11 @@ int cmd_browse(cmd_context_t *ctx) {
850850
bview_t *menu;
851851
aproc_t *aproc;
852852
char *cmd;
853-
char *browse_path;
853+
char *browse_path, *browse_path_esc;
854854
browse_path = ctx->static_param ? ctx->static_param : "./";
855-
asprintf(&cmd, "cd %s && tree --charset C -n -f -L 2 2>/dev/null", browse_path);
855+
browse_path_esc = util_escape_shell_arg(browse_path, strlen(browse_path));
856+
asprintf(&cmd, "cd %s && tree --charset C -n -f -L 2 2>/dev/null", browse_path_esc);
857+
free(browse_path_esc);
856858
aproc = aproc_new(ctx->editor, ctx->bview, &(ctx->bview->aproc), cmd, 0, _cmd_aproc_bview_passthru_cb);
857859
free(cmd);
858860
if (!aproc) return MLE_ERR;

tests/func/test_browse.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,20 @@ trap finish EXIT
2222
mkdir -p adir/bdir
2323
touch adir/bdir/hi
2424

25+
# setup tmpdir with space in it (should be escaped)
26+
mkdir -p 'a sp/b sp'
27+
touch 'a sp/b sp/h i'
28+
2529
# ensure that we can browse into adir, bdir, then select hi
2630
# ensure path is relative to where we started
2731
macro='C-b C-f a d i r enter enter C-f b d i r enter enter C-f h i enter enter'
2832
declare -A expected
2933
expected[hi]='^bview.[[:digit:]]+.buffer.path=adir/bdir/hi$'
3034
source "$this_dir/test.sh"
35+
36+
# ensure that we can browse into 'a sp', 'b sp', then select 'h i'
37+
# ensure path is relative to where we started
38+
macro='C-b C-f a space s p enter enter C-f b space s p enter enter C-f h space i enter enter'
39+
declare -A expected
40+
expected[hi_space]='^bview.[[:digit:]]+.buffer.path=a sp/b sp/h i$'
41+
source "$this_dir/test.sh"

0 commit comments

Comments
 (0)