diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:37:39 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:37:39 -0600 |
| commit | d538cc8ce9c32c932e12134b7bd59a88e88d748c (patch) | |
| tree | 835e1db938c5a67465ffbe556d7e58683619a54e | |
| parent | 662f3b37dcfad0fd2f193762464c1b9701c96db9 (diff) | |
| download | zash-d538cc8ce9c32c932e12134b7bd59a88e88d748c.tar.gz | |
fix(edit): quote the temporary file name for the editor
`v` and `fc -e` handed the editor "%s %s" of the editor and the mkstemp
file name, unquoted: with a TMPDIR holding a space or a `$` the editor saw
a path cut at the space, wrote nothing, and `fc` ran the original commands
again. The name now goes through out_quote() into an OUT_MEM buffer, so
the shell reads it back as one word; `sprintf` is gone.
Both commands also left the file behind when the editor command raised an
exception (a syntax error in $EDITOR, for one), because the raise jumps
past unlink(). They now catch it, unlink, and re-raise, the same way the
builtin wrapper in exec.c does.
fc_edit() also ignored a write error on the temporary file and went on to
run the editor; it now reports it and removes the file.
Test: tests/linux/i-fc-tmpdir.sh runs fc and `v` with TMPDIR="tmp dir$x"
through ptydrive, including a raising editor command, and then checks the
directory is empty. Without the change the editor is handed the broken
path and no file appears.
Verified on Linux with make check and the test under ASan+UBSan.
| -rw-r--r-- | src/edit_builtin.c | 26 | ||||
| -rw-r--r-- | src/history.c | 31 | ||||
| -rw-r--r-- | tests/linux/i-fc-tmpdir.exp | 11 | ||||
| -rw-r--r-- | tests/linux/i-fc-tmpdir.sh | 35 |
4 files changed, 93 insertions, 10 deletions
diff --git a/src/edit_builtin.c b/src/edit_builtin.c index 75dd31e..b0558f4 100644 --- a/src/edit_builtin.c +++ b/src/edit_builtin.c @@ -994,13 +994,31 @@ static void edit_external(void) { editor = var_get("EDITOR"); if (!editor || !*editor) editor = "vi"; - char *cmd = xmalloc(strlen(editor) + sizeof file + 2); - sprintf(cmd, "%s %s", editor, file); + /* The editor is a shell command, the file name is a word: quote it, so + * a TMPDIR with a space or a `$` in it still names the file. */ + output cmd = {.fd = OUT_MEM}; + out_puts(&cmd, editor); + out_putc(&cmd, ' '); + out_quote(&cmd, file); + out_write(&cmd, "", 1); /* the NUL */ raw_off(); ob_flush(); - int status = eval_string(cmd, 0); + jmploc jl; + jmploc *prev = exc_handler; + int status; + if (setjmp(jl.buf)) { + /* The editor command raised: the file must not be left behind. */ + exc_handler = prev; + raw_on(); + free(cmd.buf); + unlink(file); + exc_raise(exc_code); + } + exc_catch(&jl); + status = eval_string(cmd.buf, 0); + exc_handler = prev; raw_on(); - free(cmd); + free(cmd.buf); char *text = nullptr; size_t nread = 0; if (status == 0 && wrote && (fd = open(file, O_RDONLY)) >= 0) { diff --git a/src/history.c b/src/history.c index 011e0db..b15d6b8 100644 --- a/src/history.c +++ b/src/history.c @@ -291,14 +291,33 @@ static int fc_edit(const char *editor, int lo, int hi) { break; } out_flush(&o); + if (o.err) { + close(fd); + unlink(file); + sh_warn("fc: write error: %s", strerror(o.err)); + return 1; + } close(fd); - strbuf cmd; - sb_init(&cmd, &scratch); - sb_puts(&cmd, editor); - sb_putc(&cmd, ' '); - sb_puts(&cmd, file); - int status = eval_string(sb_cstr(&cmd), 0); + output cmd = {.fd = OUT_MEM}; + out_puts(&cmd, editor); + out_putc(&cmd, ' '); + out_quote(&cmd, file); /* TMPDIR may hold a space or a `$` */ + out_write(&cmd, "", 1); + jmploc jl; + jmploc *prev = exc_handler; + int status; + if (setjmp(jl.buf)) { + /* The editor command raised: the file must not be left behind. */ + exc_handler = prev; + free(cmd.buf); + unlink(file); + exc_raise(exc_code); + } + exc_catch(&jl); + status = eval_string(cmd.buf, 0); + exc_handler = prev; + free(cmd.buf); size_t len = 0; char *text = status == 0 ? read_file(file, &len) : nullptr; unlink(file); diff --git a/tests/linux/i-fc-tmpdir.exp b/tests/linux/i-fc-tmpdir.exp new file mode 100644 index 0000000..f0b9bc0 --- /dev/null +++ b/tests/linux/i-fc-tmpdir.exp @@ -0,0 +1,11 @@ +$ echo hello +hello +$ fc -e "$PWD/ed.sh" +echo fc-edited +fc-edited +$ echo vi-edited +vi-edited +$ fc -e 'echo (' +SHELL: syntax error: unexpected 'WORK/tmp dir$x/zash-fc.XXXX' +$ exit +[status 0] diff --git a/tests/linux/i-fc-tmpdir.sh b/tests/linux/i-fc-tmpdir.sh new file mode 100644 index 0000000..a6177b0 --- /dev/null +++ b/tests/linux/i-fc-tmpdir.sh @@ -0,0 +1,35 @@ +# The editor commands fc and vi's `v` put the temporary file name on a +# command line, so a TMPDIR with a space and a `$` in it has to survive +# quoting. Unquoted, the editor got a path cut at the space, and fc ran the +# original commands again. +command -v python3 >/dev/null 2>&1 || exit 77 +mkdir -p 'tmp dir$x' +cat >ed.sh <<'INS' +#!/bin/sh +printf 'echo fc-edited\n' >"$1" +INS +cat >vis.sh <<'INS' +#!/bin/sh +printf 'echo vi-edited\n' >"$1" +INS +chmod +x ed.sh vis.sh +# The syntax error names the temporary file, whose name has the work +# directory and a random suffix in it: fold both, or the .exp could not be +# compared. +{ +TMPDIR="$PWD/tmp dir\$x" PS1='$ ' ENV='' HISTFILE='' VISUAL="$PWD/vis.sh" \ + python3 "$TESTS/tools/ptydrive.py" "$TSH" -o vi <<'END' +send echo hello\r +expect hello +send fc -e "$PWD/ed.sh"\r +expect fc-edited +send echo hi\x1bv\r +expect vi-edited +# An editor command that raises an exception must not leave the file +# behind either: `echo (` is a syntax error, raised past the unlink(). +send fc -e 'echo ('\r +send exit\r +END +} | sed -e "s|$PWD|WORK|g" -e 's/zash-fc\.[A-Za-z0-9]*/zash-fc.XXXX/' +# Neither editor may leave its file behind. +ls -A 'tmp dir$x' |
