summaryrefslogtreecommitdiffhomepage
path: root/src/parser.c
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 /src/parser.c
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 'src/parser.c')
-rw-r--r--src/parser.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/src/parser.c b/src/parser.c
index 70bc96a..98d4c0b 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -112,14 +112,14 @@ static heredoc *here_head;
/**
* @brief Where the next pending here-document is linked.
*/
+static heredoc **here_tail = &here_head;
+
/**
* @brief The lexer is inside `[[ ]]`, where `<` and `>` are comparison
* operators, not redirections.
*/
static bool in_cond;
-static heredoc **here_tail = &here_head;
-
/**
* @brief Set by input_fill() when an alias whose value ends in a blank runs
* out: the next word is then subject to alias substitution too.
@@ -2139,6 +2139,8 @@ typedef struct parser_state {
bool tok_pushed;
heredoc *here_head;
heredoc **here_tail;
+ bool in_cond;
+ bool alias_ended_blank;
} parser_state;
static_assert(sizeof(parser_state) <= PARSER_STATE_SIZE,
@@ -2150,7 +2152,8 @@ static_assert(sizeof(parser_state) <= PARSER_STATE_SIZE,
*/
void parser_save(void *state) {
parser_state *ps = state;
- *ps = (parser_state){pa, tok, tok_pushed, here_head, here_tail};
+ *ps = (parser_state){pa, tok, tok_pushed, here_head, here_tail, in_cond,
+ alias_ended_blank};
if (here_tail == &here_head)
ps->here_tail = nullptr;
parser_reset();
@@ -2166,4 +2169,6 @@ void parser_restore(const void *state) {
tok_pushed = ps->tok_pushed;
here_head = ps->here_head;
here_tail = ps->here_tail ? ps->here_tail : &here_head;
+ in_cond = ps->in_cond;
+ alias_ended_blank = ps->alias_ended_blank;
}