summaryrefslogtreecommitdiffhomepage
path: root/tests/linux
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:13:39 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:13:39 -0600
commit0fc285274b7dacbba311bdbd4b4697b7ebcac09c (patch)
tree27a25e7f7386a188ac39a1fcd83500a3f7d65017 /tests/linux
parent8ac2777c82668c46c24bb0f3047c1128f9553760 (diff)
downloadzash-0fc285274b7dacbba311bdbd4b4697b7ebcac09c.tar.gz
fix(parser): save and restore the parser state in run_input
A trap that fires while a command is half parsed, and vi's `v` command, both run eval_string() from inside the outer parse. The nested parse_command() points the static arena at its own arena, and freeing that arena leaves the outer parse allocating from freed memory: silent corruption in a release build, heap-use-after-free under ASan. run_input() now brackets its whole body with parser_save()/parser_restore() on every way out - the normal return, the error unwind and the EXIT unwind of the recover loop - so any nested eval, present or future, is safe. in_cond and alias_ended_blank join parser_state because a nested parse changes them too; the static_assert keeps the snapshot buffer in step. Also move here_tail's doc comment back above here_tail. Tests: - tests/posix/trap-midtoken.sh: a trap delivered mid-token from a pipe. It reports heap-use-after-free under `make asan` before the change and passes after it. - tests/linux/i-vi-edit.sh: `v` with $VISUAL, $EDITOR and a failing editor through ptydrive; the edited line runs. Verified on Linux (clang 20, -O3 -g) with `make check` and ASan+UBSan.
Diffstat (limited to 'tests/linux')
-rw-r--r--tests/linux/i-vi-edit.exp10
-rw-r--r--tests/linux/i-vi-edit.sh44
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/linux/i-vi-edit.exp b/tests/linux/i-vi-edit.exp
new file mode 100644
index 0000000..fb8d9c1
--- /dev/null
+++ b/tests/linux/i-vi-edit.exp
@@ -0,0 +1,10 @@
+$ echo visual 'echo one'
+visual echo one
+$ exit
+$ echo edited 'echo two'
+edited echo two
+$ exit
+$ echo keep me
+keep me
+$ exit
+[status 0]
diff --git a/tests/linux/i-vi-edit.sh b/tests/linux/i-vi-edit.sh
new file mode 100644
index 0000000..c78756a
--- /dev/null
+++ b/tests/linux/i-vi-edit.sh
@@ -0,0 +1,44 @@
+# Interactive: vi's `v` puts the line in $VISUAL, else $EDITOR.
+#
+# The editor here is a script that writes the line it was given back as a
+# new command, so the test sees that the editor ran and what it was
+# handed: the line being edited, on its own.
+#
+# `v` runs the editor through eval_string() while the outer command is
+# half parsed, so under `make asan` this is also a regression test for a
+# nested eval that frees the parse arena (see posix/trap-midtoken).
+command -v python3 >/dev/null 2>&1 || exit 77
+cat >ed.sh <<'INS'
+#!/bin/sh
+printf "echo edited '%s'\n" "$(cat "$1")" >"$1"
+INS
+cat >vis.sh <<'INS'
+#!/bin/sh
+printf "echo visual '%s'\n" "$(cat "$1")" >"$1"
+INS
+chmod +x ed.sh vis.sh
+# $VISUAL wins over $EDITOR, as POSIX says.
+PS1='$ ' ENV='' HISTFILE='' EDITOR="$PWD/ed.sh" VISUAL="$PWD/vis.sh" \
+ python3 "$TESTS/tools/ptydrive.py" "$TSH" -o vi <<'END'
+send echo one\x1bv
+send \r
+expect visual echo one
+send exit\r
+END
+# With no $VISUAL, $EDITOR is used.
+PS1='$ ' ENV='' HISTFILE='' VISUAL='' EDITOR="$PWD/ed.sh" \
+ python3 "$TESTS/tools/ptydrive.py" "$TSH" -o vi <<'END'
+send echo two\x1b0wv
+send \r
+expect edited echo two
+send exit\r
+END
+# A failed editor leaves the line alone.
+PS1='$ ' ENV='' HISTFILE='' VISUAL='' EDITOR=false \
+ python3 "$TESTS/tools/ptydrive.py" "$TSH" -o vi <<'END'
+send echo keep me\x1bv
+expect echo keep me
+send \r
+expect keep me
+send exit\r
+END